From 2168f2cf3baa12134e279a976137bd9dc104ae61 Mon Sep 17 00:00:00 2001 From: Paul Woitaschek Date: Sun, 10 Jul 2022 23:23:39 +0200 Subject: [PATCH] Only enable the edit title text if the trimmed text is not empty. --- .../voice/bookOverview/editTitle/EditBookTitleState.kt | 7 +++++-- .../bookOverview/editTitle/EditBookTitleViewModel.kt | 3 ++- .../main/kotlin/voice/bookOverview/views/BookOverview.kt | 2 +- .../voice/bookOverview/views/EditBookTitleDialog.kt | 9 ++++++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleState.kt b/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleState.kt index 597059b750..d7f80dd4af 100644 --- a/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleState.kt +++ b/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleState.kt @@ -4,5 +4,8 @@ import voice.data.Book internal data class EditBookTitleState( val title: String, - val bookId: Book.Id -) + val bookId: Book.Id, +) { + + val confirmButtonEnabled: Boolean = title.trim().isNotEmpty() +} diff --git a/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleViewModel.kt b/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleViewModel.kt index a69cf595f9..e52320d19a 100644 --- a/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleViewModel.kt +++ b/bookOverview/src/main/kotlin/voice/bookOverview/editTitle/EditBookTitleViewModel.kt @@ -24,7 +24,7 @@ constructor( val book = repo.get(id) ?: return@launch _state.value = EditBookTitleState( title = book.content.name, - bookId = id + bookId = id, ) } } @@ -40,6 +40,7 @@ constructor( internal fun onConfirmEditTitle() { val state = _state.value if (state != null) { + check(state.confirmButtonEnabled) scope.launch { repo.updateBook(state.bookId) { it.copy(name = state.title.trim()) diff --git a/bookOverview/src/main/kotlin/voice/bookOverview/views/BookOverview.kt b/bookOverview/src/main/kotlin/voice/bookOverview/views/BookOverview.kt index b2dda9e063..943898e583 100644 --- a/bookOverview/src/main/kotlin/voice/bookOverview/views/BookOverview.kt +++ b/bookOverview/src/main/kotlin/voice/bookOverview/views/BookOverview.kt @@ -117,7 +117,7 @@ fun BookOverviewScreen( EditBookTitleDialog( onDismissEditTitleClick = editBookTitleViewModel::onDismissEditTitle, onConfirmEditTitle = editBookTitleViewModel::onConfirmEditTitle, - editBookTitleState = editBookTitleState, + viewState = editBookTitleState, onUpdateEditTitle = editBookTitleViewModel::onUpdateEditTitle ) } diff --git a/bookOverview/src/main/kotlin/voice/bookOverview/views/EditBookTitleDialog.kt b/bookOverview/src/main/kotlin/voice/bookOverview/views/EditBookTitleDialog.kt index 7f2f13a93f..325229bc81 100644 --- a/bookOverview/src/main/kotlin/voice/bookOverview/views/EditBookTitleDialog.kt +++ b/bookOverview/src/main/kotlin/voice/bookOverview/views/EditBookTitleDialog.kt @@ -14,7 +14,7 @@ import voice.bookOverview.editTitle.EditBookTitleState internal fun EditBookTitleDialog( onDismissEditTitleClick: () -> Unit, onConfirmEditTitle: () -> Unit, - editBookTitleState: EditBookTitleState, + viewState: EditBookTitleState, onUpdateEditTitle: (String) -> Unit ) { AlertDialog( @@ -23,7 +23,10 @@ internal fun EditBookTitleDialog( Text(text = stringResource(R.string.edit_book_title)) }, confirmButton = { - Button(onClick = onConfirmEditTitle) { + Button( + onClick = onConfirmEditTitle, + enabled = viewState.confirmButtonEnabled + ) { Text(stringResource(id = R.string.dialog_confirm)) } }, @@ -34,7 +37,7 @@ internal fun EditBookTitleDialog( }, text = { TextField( - value = editBookTitleState.title, + value = viewState.title, onValueChange = onUpdateEditTitle, label = { Text(stringResource(R.string.change_book_name))