Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only display empty title error when user already entered content and then removed it #86

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ fun EditTextDialog(
properties = DialogProperties(dismissOnBackPress = false, dismissOnClickOutside = false)
) {
var newTitle by remember { mutableStateOf(initialText) }
// avoid displaying an error message when user didn't even started to write content
var alreadyHadSomeContent by remember { mutableStateOf(initialText.isNotBlank()) }
val hasError by remember(newTitle, allowBlank) {
derivedStateOf {
!allowBlank && newTitle.isBlank()
Expand All @@ -81,17 +83,20 @@ fun EditTextDialog(
}
OutlinedTextField(
newTitle,
onValueChange = { newTitle = it },
onValueChange = {
alreadyHadSomeContent = alreadyHadSomeContent || it.isNotBlank()
newTitle = it
},
label = { Text(stringResource(Res.string.edit_text_dialog_title)) },
maxLines = 1,
supportingText = if (allowBlank) null else {
{
AnimatedVisibility(visible = hasError) {
AnimatedVisibility(visible = hasError && alreadyHadSomeContent) {
Text(stringResource(Res.string.edit_text_dialog_empty_title_error))
}
}
},
isError = hasError,
isError = hasError && alreadyHadSomeContent,
)
Row(
Modifier.align(Alignment.End),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,11 @@ fun TaskListDetail(
) {
val sheetTitleRes = if (showEditTaskSheet) Res.string.task_editor_sheet_edit_title else Res.string.task_editor_sheet_new_title
var newTitle by remember { mutableStateOf(task?.title ?: "") }
// avoid displaying an error message when user didn't even started to write content
var alreadyHadSomeContent by remember { mutableStateOf((task?.title ?: "").isNotBlank()) }
val titleHasError by remember {
derivedStateOf {
showEditTaskSheet && newTitle.isBlank()
newTitle.isBlank()
}
}
var newNotes by remember { mutableStateOf(task?.notes ?: "") }
Expand All @@ -435,21 +437,24 @@ fun TaskListDetail(

OutlinedTextField(
newTitle,
onValueChange = { newTitle = it },
onValueChange = {
alreadyHadSomeContent = alreadyHadSomeContent || it.isNotBlank()
newTitle = it
},
modifier = Modifier.fillMaxWidth(),
label = { Text(stringResource(Res.string.task_editor_sheet_title_field_label)) },
placeholder = { Text(stringResource(Res.string.task_editor_sheet_title_field_placeholder)) },
maxLines = 1,
supportingText = {
AnimatedVisibility(visible = titleHasError) {
AnimatedVisibility(visible = titleHasError && alreadyHadSomeContent) {
Text(stringResource(Res.string.task_editor_sheet_title_field_empty_error))
}
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next,
),
isError = titleHasError,
isError = titleHasError && alreadyHadSomeContent,
)

OutlinedTextField(
Expand Down