From 2be47c5b0fe590cdc739f4d1047edde7205f6751 Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 30 Jan 2024 17:52:24 -0600 Subject: [PATCH] Minor cleanup of LoginWithDeviceViewModel (#881) --- .../LoginWithDeviceViewModel.kt | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/loginwithdevice/LoginWithDeviceViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/loginwithdevice/LoginWithDeviceViewModel.kt index 02971b469..9490c897b 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/loginwithdevice/LoginWithDeviceViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/auth/feature/loginwithdevice/LoginWithDeviceViewModel.kt @@ -50,16 +50,7 @@ class LoginWithDeviceViewModel @Inject constructor( } private fun handleErrorDialogDismissed() { - val viewState = mutableStateFlow.value.viewState as? LoginWithDeviceState.ViewState.Content - if (viewState != null) { - mutableStateFlow.update { - it.copy( - viewState = viewState.copy( - shouldShowErrorDialog = false, - ), - ) - } - } + updateContent { it.copy(shouldShowErrorDialog = false) } } private fun handleResendNotificationClicked() { @@ -87,7 +78,6 @@ class LoginWithDeviceViewModel @Inject constructor( } is AuthRequestResult.Error -> { - mutableStateFlow.update { it.copy( viewState = LoginWithDeviceState.ViewState.Content( @@ -115,18 +105,19 @@ class LoginWithDeviceViewModel @Inject constructor( } private fun setIsResendNotificationLoading(isLoading: Boolean) { - when (val viewState = mutableStateFlow.value.viewState) { - is LoginWithDeviceState.ViewState.Content -> { - mutableStateFlow.update { - it.copy( - viewState = viewState.copy( - isResendNotificationLoading = isLoading, - ), - ) - } - } - else -> Unit - } + updateContent { it.copy(isResendNotificationLoading = isLoading) } + } + + private inline fun updateContent( + crossinline block: ( + LoginWithDeviceState.ViewState.Content, + ) -> LoginWithDeviceState.ViewState.Content?, + ) { + val currentViewState = state.viewState + val updatedContent = (currentViewState as? LoginWithDeviceState.ViewState.Content) + ?.let(block) + ?: return + mutableStateFlow.update { it.copy(viewState = updatedContent) } } }