Fix incorrect VM state access & unnecessary eventFlow usages (#1218)

This commit is contained in:
Caleb Derosier
2024-04-03 13:36:57 -06:00
committed by Álison Fernandes
parent ab55b5e535
commit 87254b4436
13 changed files with 381 additions and 430 deletions

View File

@@ -191,7 +191,7 @@ class CreateAccountViewModel @Inject constructor(
mutableStateFlow.update { it.copy(dialog = null) }
sendEvent(
CreateAccountEvent.NavigateToLogin(
email = mutableStateFlow.value.emailInput,
email = state.emailInput,
captchaToken = registerAccountResult.captchaToken,
),
)
@@ -251,7 +251,7 @@ class CreateAccountViewModel @Inject constructor(
} else {
passwordStrengthJob = viewModelScope.launch {
val result = authRepository.getPasswordStrength(
email = mutableStateFlow.value.emailInput,
email = state.emailInput,
password = action.input,
)
trySendAction(ReceivePasswordStrengthResult(result))
@@ -264,7 +264,7 @@ class CreateAccountViewModel @Inject constructor(
}
private fun handleSubmitClick() = when {
mutableStateFlow.value.emailInput.isBlank() -> {
state.emailInput.isBlank() -> {
val dialog = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(),
message = R.string.validation_field_required
@@ -273,7 +273,7 @@ class CreateAccountViewModel @Inject constructor(
mutableStateFlow.update { it.copy(dialog = CreateAccountDialog.Error(dialog)) }
}
!mutableStateFlow.value.emailInput.isValidEmail() -> {
!state.emailInput.isValidEmail() -> {
val dialog = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(),
message = R.string.invalid_email.asText(),
@@ -281,7 +281,7 @@ class CreateAccountViewModel @Inject constructor(
mutableStateFlow.update { it.copy(dialog = CreateAccountDialog.Error(dialog)) }
}
mutableStateFlow.value.passwordInput.length < MIN_PASSWORD_LENGTH -> {
state.passwordInput.length < MIN_PASSWORD_LENGTH -> {
val dialog = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(),
message = R.string.master_password_length_val_message_x.asText(MIN_PASSWORD_LENGTH),
@@ -289,7 +289,7 @@ class CreateAccountViewModel @Inject constructor(
mutableStateFlow.update { it.copy(dialog = CreateAccountDialog.Error(dialog)) }
}
mutableStateFlow.value.passwordInput != mutableStateFlow.value.confirmPasswordInput -> {
state.passwordInput != state.confirmPasswordInput -> {
val dialog = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(),
message = R.string.master_password_confirmation_val_message.asText(),
@@ -297,7 +297,7 @@ class CreateAccountViewModel @Inject constructor(
mutableStateFlow.update { it.copy(dialog = CreateAccountDialog.Error(dialog)) }
}
!mutableStateFlow.value.isAcceptPoliciesToggled -> {
!state.isAcceptPoliciesToggled -> {
val dialog = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(),
message = R.string.accept_policies_error.asText(),
@@ -307,7 +307,7 @@ class CreateAccountViewModel @Inject constructor(
else -> {
submitRegisterAccountRequest(
shouldCheckForDataBreaches = mutableStateFlow.value.isCheckDataBreachesToggled,
shouldCheckForDataBreaches = state.isCheckDataBreachesToggled,
captchaToken = null,
)
}
@@ -327,9 +327,9 @@ class CreateAccountViewModel @Inject constructor(
viewModelScope.launch {
val result = authRepository.register(
shouldCheckDataBreaches = shouldCheckForDataBreaches,
email = mutableStateFlow.value.emailInput,
masterPassword = mutableStateFlow.value.passwordInput,
masterPasswordHint = mutableStateFlow.value.passwordHintInput.ifBlank { null },
email = state.emailInput,
masterPassword = state.passwordInput,
masterPasswordHint = state.passwordHintInput.ifBlank { null },
captchaToken = captchaToken,
)
sendAction(

View File

@@ -72,8 +72,6 @@ class EnvironmentViewModel @Inject constructor(
}
private fun handleSaveClickAction() {
val state = mutableStateFlow.value
val urlsAreAllNullOrValid = listOf(
state.serverUrl,
state.webVaultServerUrl,

View File

@@ -128,7 +128,7 @@ class LandingViewModel @Inject constructor(
}
private fun handleContinueButtonClicked() {
if (!mutableStateFlow.value.emailInput.isValidEmail()) {
if (!state.emailInput.isValidEmail()) {
mutableStateFlow.update {
it.copy(
dialog = LandingState.DialogState.Error(
@@ -150,8 +150,8 @@ class LandingViewModel @Inject constructor(
return
}
val email = mutableStateFlow.value.emailInput
val isRememberMeEnabled = mutableStateFlow.value.isRememberMeEnabled
val email = state.emailInput
val isRememberMeEnabled = state.isRememberMeEnabled
// Update the remembered email address
authRepository.rememberedEmailAddress = email.takeUnless { !isRememberMeEnabled }

View File

@@ -232,9 +232,9 @@ class LoginViewModel @Inject constructor(
}
viewModelScope.launch {
val result = authRepository.login(
email = mutableStateFlow.value.emailAddress,
password = mutableStateFlow.value.passwordInput,
captchaToken = mutableStateFlow.value.captchaToken,
email = state.emailAddress,
password = state.passwordInput,
captchaToken = state.captchaToken,
)
sendAction(
LoginAction.Internal.ReceiveLoginResult(
@@ -245,7 +245,7 @@ class LoginViewModel @Inject constructor(
}
private fun handleMasterPasswordHintClicked() {
val email = mutableStateFlow.value.emailAddress
val email = state.emailAddress
sendEvent(LoginEvent.NavigateToMasterPasswordHint(email))
}
@@ -254,7 +254,7 @@ class LoginViewModel @Inject constructor(
}
private fun handleSingleSignOnClicked() {
val email = mutableStateFlow.value.emailAddress
val email = state.emailAddress
sendEvent(LoginEvent.NavigateToEnterpriseSignOn(email))
}

View File

@@ -160,13 +160,13 @@ class VaultUnlockViewModel @Inject constructor(
val vaultUnlockResult = when (state.vaultUnlockType) {
VaultUnlockType.MASTER_PASSWORD -> {
vaultRepo.unlockVaultWithMasterPassword(
mutableStateFlow.value.input,
state.input,
)
}
VaultUnlockType.PIN -> {
vaultRepo.unlockVaultWithPin(
mutableStateFlow.value.input,
state.input,
)
}
}

View File

@@ -97,9 +97,9 @@ class LoginApprovalViewModel @Inject constructor(
trySendAction(
LoginApprovalAction.Internal.ApproveRequestResultReceive(
result = authRepository.updateAuthRequest(
requestId = mutableStateFlow.value.requestId,
masterPasswordHash = mutableStateFlow.value.masterPasswordHash,
publicKey = mutableStateFlow.value.publicKey,
requestId = state.requestId,
masterPasswordHash = state.masterPasswordHash,
publicKey = state.publicKey,
isApproved = true,
),
),
@@ -116,9 +116,9 @@ class LoginApprovalViewModel @Inject constructor(
trySendAction(
LoginApprovalAction.Internal.DeclineRequestResultReceive(
result = authRepository.updateAuthRequest(
requestId = mutableStateFlow.value.requestId,
masterPasswordHash = mutableStateFlow.value.masterPasswordHash,
publicKey = mutableStateFlow.value.publicKey,
requestId = state.requestId,
masterPasswordHash = state.masterPasswordHash,
publicKey = state.publicKey,
isApproved = false,
),
),

View File

@@ -76,7 +76,7 @@ class PendingRequestsViewModel @Inject constructor(
viewState = PendingRequestsState.ViewState.Loading,
)
}
mutableStateFlow.value.authRequests.forEach { request ->
state.authRequests.forEach { request ->
authRepository.updateAuthRequest(
requestId = request.id,
masterPasswordHash = request.masterPasswordHash,

View File

@@ -121,7 +121,7 @@ class ExportVaultViewModel @Inject constructor(
@Suppress("ReturnCount")
private fun handleConfirmExportVaultClicked() {
// Display an error alert if the user hasn't entered a password.
if (mutableStateFlow.value.passwordInput.isBlank()) {
if (state.passwordInput.isBlank()) {
updateStateWithError(
message = R.string.validation_field_required.asText(
R.string.master_password.asText(),
@@ -149,7 +149,7 @@ class ExportVaultViewModel @Inject constructor(
sendAction(
ExportVaultAction.Internal.ReceiveValidatePasswordResult(
result = authRepository.validatePassword(
password = mutableStateFlow.value.passwordInput,
password = state.passwordInput,
),
),
)

View File

@@ -550,7 +550,7 @@ class GeneratorViewModel @Inject constructor(
private fun handleRegenerationClick() {
// Go through the update process with the current state to trigger a
// regeneration of the generated text for the same state.
updateGeneratorMainType(forceRegeneration = true) { mutableStateFlow.value.selectedType }
updateGeneratorMainType(forceRegeneration = true) { state.selectedType }
}
private fun handleCopyClick() {
@@ -1294,7 +1294,7 @@ class GeneratorViewModel @Inject constructor(
forceRegeneration: Boolean = false,
crossinline block: (GeneratorState.MainType) -> GeneratorState.MainType?,
) {
val currentSelectedType = mutableStateFlow.value.selectedType
val currentSelectedType = state.selectedType
val updatedMainType = block(currentSelectedType) ?: return
mutableStateFlow.update { it.copy(selectedType = updatedMainType) }

View File

@@ -434,7 +434,7 @@ class VaultAddEditViewModel @Inject constructor(
when (action.customFieldAction) {
CustomFieldAction.MOVE_UP -> {
val items =
(mutableStateFlow.value.viewState as VaultAddEditState.ViewState.Content)
(state.viewState as VaultAddEditState.ViewState.Content)
.common
.customFieldData
.toMutableList()
@@ -455,7 +455,7 @@ class VaultAddEditViewModel @Inject constructor(
CustomFieldAction.MOVE_DOWN -> {
val items =
(mutableStateFlow.value.viewState as VaultAddEditState.ViewState.Content)
(state.viewState as VaultAddEditState.ViewState.Content)
.common
.customFieldData
.toMutableList()