Introduce utility function for displaying VaultAddEdit error message (#3438)

This commit is contained in:
Patrick Honkonen 2024-07-10 17:56:36 -04:00 committed by GitHub
parent 0651494393
commit 9205dbef59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -318,28 +318,17 @@ class VaultAddEditViewModel @Inject constructor(
@Suppress("LongMethod") @Suppress("LongMethod")
private fun handleSaveClick() = onContent { content -> private fun handleSaveClick() = onContent { content ->
if (content.common.name.isBlank()) { if (content.common.name.isBlank()) {
mutableStateFlow.update { showGenericErrorDialog(
it.copy( message = R.string.validation_field_required.asText(R.string.name.asText()),
dialog = VaultAddEditState.DialogState.Generic( )
title = R.string.an_error_has_occurred.asText(),
message = R.string.validation_field_required
.asText(R.string.name.asText()),
),
)
}
return@onContent return@onContent
} else if ( } else if (
content.common.selectedOwnerId != null && content.common.selectedOwnerId != null &&
content.common.selectedOwner?.collections?.all { !it.isSelected } == true content.common.selectedOwner?.collections?.all { !it.isSelected } == true
) { ) {
mutableStateFlow.update { showGenericErrorDialog(
it.copy( message = R.string.select_one_collection.asText(),
dialog = VaultAddEditState.DialogState.Generic( )
title = R.string.an_error_has_occurred.asText(),
message = R.string.select_one_collection.asText(),
),
)
}
return@onContent return@onContent
} }
@ -1147,14 +1136,7 @@ class VaultAddEditViewModel @Inject constructor(
when (action.createCipherResult) { when (action.createCipherResult) {
is CreateCipherResult.Error -> { is CreateCipherResult.Error -> {
mutableStateFlow.update { showGenericErrorDialog()
it.copy(
dialog = VaultAddEditState.DialogState.Generic(
title = R.string.an_error_has_occurred.asText(),
message = R.string.generic_error_message.asText(),
),
)
}
} }
is CreateCipherResult.Success -> { is CreateCipherResult.Success -> {
@ -1181,17 +1163,12 @@ class VaultAddEditViewModel @Inject constructor(
clearDialogState() clearDialogState()
when (val result = action.updateCipherResult) { when (val result = action.updateCipherResult) {
is UpdateCipherResult.Error -> { is UpdateCipherResult.Error -> {
mutableStateFlow.update { showGenericErrorDialog(
it.copy( message = result
dialog = VaultAddEditState.DialogState.Generic( .errorMessage
title = R.string.an_error_has_occurred.asText(), ?.asText()
message = result ?: R.string.generic_error_message.asText(),
.errorMessage )
?.asText()
?: R.string.generic_error_message.asText(),
),
)
}
} }
is UpdateCipherResult.Success -> { is UpdateCipherResult.Success -> {
@ -1206,13 +1183,7 @@ class VaultAddEditViewModel @Inject constructor(
private fun handleDeleteCipherReceive(action: VaultAddEditAction.Internal.DeleteCipherReceive) { private fun handleDeleteCipherReceive(action: VaultAddEditAction.Internal.DeleteCipherReceive) {
when (action.result) { when (action.result) {
DeleteCipherResult.Error -> { DeleteCipherResult.Error -> {
mutableStateFlow.update { showErrorDialog(message = R.string.generic_error_message.asText())
it.copy(
dialog = VaultAddEditState.DialogState.Generic(
message = R.string.generic_error_message.asText(),
),
)
}
} }
DeleteCipherResult.Success -> { DeleteCipherResult.Success -> {
@ -1331,14 +1302,10 @@ class VaultAddEditViewModel @Inject constructor(
} }
TotpCodeResult.CodeScanningError -> { TotpCodeResult.CodeScanningError -> {
mutableStateFlow.update { showErrorDialog(
it.copy( title = R.string.an_error_has_occurred.asText(),
dialog = VaultAddEditState.DialogState.Generic( message = R.string.authenticator_key_read_error.asText(),
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 { showErrorDialog(message = message)
it.copy(dialog = VaultAddEditState.DialogState.Generic(message = message))
}
} }
private fun handleFido2RegisterCredentialResultReceive( private fun handleFido2RegisterCredentialResultReceive(
@ -1407,6 +1372,26 @@ class VaultAddEditViewModel @Inject constructor(
mutableStateFlow.update { it.copy(dialog = null) } 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( private inline fun onContent(
crossinline block: (VaultAddEditState.ViewState.Content) -> Unit, crossinline block: (VaultAddEditState.ViewState.Content) -> Unit,
) { ) {