diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt index f9068735d3..d3e66cd8de 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/vault/feature/addedit/VaultAddEditViewModel.kt @@ -318,28 +318,17 @@ class VaultAddEditViewModel @Inject constructor( @Suppress("LongMethod") private fun handleSaveClick() = onContent { content -> if (content.common.name.isBlank()) { - mutableStateFlow.update { - it.copy( - dialog = VaultAddEditState.DialogState.Generic( - title = R.string.an_error_has_occurred.asText(), - message = R.string.validation_field_required - .asText(R.string.name.asText()), - ), - ) - } + showGenericErrorDialog( + message = R.string.validation_field_required.asText(R.string.name.asText()), + ) return@onContent } else if ( content.common.selectedOwnerId != null && content.common.selectedOwner?.collections?.all { !it.isSelected } == true ) { - mutableStateFlow.update { - it.copy( - dialog = VaultAddEditState.DialogState.Generic( - title = R.string.an_error_has_occurred.asText(), - message = R.string.select_one_collection.asText(), - ), - ) - } + showGenericErrorDialog( + message = R.string.select_one_collection.asText(), + ) return@onContent } @@ -1147,14 +1136,7 @@ class VaultAddEditViewModel @Inject constructor( when (action.createCipherResult) { is CreateCipherResult.Error -> { - mutableStateFlow.update { - it.copy( - dialog = VaultAddEditState.DialogState.Generic( - title = R.string.an_error_has_occurred.asText(), - message = R.string.generic_error_message.asText(), - ), - ) - } + showGenericErrorDialog() } is CreateCipherResult.Success -> { @@ -1181,17 +1163,12 @@ class VaultAddEditViewModel @Inject constructor( clearDialogState() when (val result = action.updateCipherResult) { is UpdateCipherResult.Error -> { - mutableStateFlow.update { - it.copy( - dialog = VaultAddEditState.DialogState.Generic( - title = R.string.an_error_has_occurred.asText(), - message = result - .errorMessage - ?.asText() - ?: R.string.generic_error_message.asText(), - ), - ) - } + showGenericErrorDialog( + message = result + .errorMessage + ?.asText() + ?: R.string.generic_error_message.asText(), + ) } is UpdateCipherResult.Success -> { @@ -1206,13 +1183,7 @@ class VaultAddEditViewModel @Inject constructor( private fun handleDeleteCipherReceive(action: VaultAddEditAction.Internal.DeleteCipherReceive) { when (action.result) { DeleteCipherResult.Error -> { - mutableStateFlow.update { - it.copy( - dialog = VaultAddEditState.DialogState.Generic( - message = R.string.generic_error_message.asText(), - ), - ) - } + showErrorDialog(message = R.string.generic_error_message.asText()) } DeleteCipherResult.Success -> { @@ -1331,14 +1302,10 @@ class VaultAddEditViewModel @Inject constructor( } TotpCodeResult.CodeScanningError -> { - mutableStateFlow.update { - it.copy( - dialog = VaultAddEditState.DialogState.Generic( - title = R.string.an_error_has_occurred.asText(), - message = R.string.authenticator_key_read_error.asText(), - ), - ) - } + showErrorDialog( + title = R.string.an_error_has_occurred.asText(), + message = R.string.authenticator_key_read_error.asText(), + ) } } } @@ -1378,9 +1345,7 @@ class VaultAddEditViewModel @Inject constructor( } } } - mutableStateFlow.update { - it.copy(dialog = VaultAddEditState.DialogState.Generic(message = message)) - } + showErrorDialog(message = message) } private fun handleFido2RegisterCredentialResultReceive( @@ -1407,6 +1372,26 @@ class VaultAddEditViewModel @Inject constructor( mutableStateFlow.update { it.copy(dialog = null) } } + private fun showGenericErrorDialog( + message: Text = R.string.generic_error_message.asText(), + ) { + showErrorDialog( + title = R.string.an_error_has_occurred.asText(), + message = message, + ) + } + + private fun showErrorDialog(title: Text? = null, message: Text) { + mutableStateFlow.update { + it.copy( + dialog = VaultAddEditState.DialogState.Generic( + title = title, + message = message, + ), + ) + } + } + private inline fun onContent( crossinline block: (VaultAddEditState.ViewState.Content) -> Unit, ) {