Minor cleanup of LoginWithDeviceViewModel (#881)

This commit is contained in:
David Perez 2024-01-30 17:52:24 -06:00 committed by Álison Fernandes
parent c88b01ea11
commit 2be47c5b0f

View file

@ -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) }
}
}