Skip to content

Commit

Permalink
Merge pull request #86 from opatry/title-error-feedback-improvement
Browse files Browse the repository at this point in the history
Only display empty title error when user already entered content and then removed it
  • Loading branch information
opatry authored Oct 29, 2024
2 parents 06329b9 + 892ea3c commit fe8b46a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
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

0 comments on commit fe8b46a

Please sign in to comment.