From 93a247e0ce728b1edaf52cadd7aa495f6a0ffe8e Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 30 May 2022 11:15:23 +0100 Subject: [PATCH] converting if/else and try/catch to when and runCatching --- .../onboarding/OnboardingViewModel.kt | 117 ++++++++---------- 1 file changed, 51 insertions(+), 66 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt index 71093a6600..0321f97532 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt @@ -128,7 +128,7 @@ class OnboardingViewModel @AssistedInject constructor( val isRegistrationStarted: Boolean get() = authenticationService.isRegistrationStarted() - private val loginWizard: LoginWizard? + private val loginWizard: LoginWizard get() = authenticationService.getLoginWizard() private var loginConfig: LoginConfig? = null @@ -447,60 +447,51 @@ class OnboardingViewModel @AssistedInject constructor( private fun handleResetPassword(action: OnboardingAction.ResetPassword) { val safeLoginWizard = loginWizard - - if (safeLoginWizard == null) { - setState { copy(isLoading = false) } - _viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration"))) - } else { - setState { copy(isLoading = true) } - - currentJob = viewModelScope.launch { - try { - safeLoginWizard.resetPassword(action.email) - } catch (failure: Throwable) { - setState { copy(isLoading = false) } - _viewEvents.post(OnboardingViewEvents.Failure(failure)) - return@launch - } - - setState { - copy( - isLoading = false, - resetState = ResetState(email = action.email, newPassword = action.newPassword) - ) - } - - _viewEvents.post(OnboardingViewEvents.OnResetPasswordSendThreePidDone) - } + setState { copy(isLoading = true) } + currentJob = viewModelScope.launch { + runCatching { safeLoginWizard.resetPassword(action.email) }.fold( + onSuccess = { + setState { + copy( + isLoading = false, + resetState = ResetState(email = action.email, newPassword = action.newPassword) + ) + } + _viewEvents.post(OnboardingViewEvents.OnResetPasswordSendThreePidDone) + }, + onFailure = { + setState { copy(isLoading = false) } + _viewEvents.post(OnboardingViewEvents.Failure(it)) + } + ) } } private fun handleResetPasswordMailConfirmed() { - val safeLoginWizard = loginWizard - - if (safeLoginWizard == null) { - setState { copy(isLoading = false) } - _viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration"))) - } else { - setState { copy(isLoading = false) } - - currentJob = viewModelScope.launch { - try { - val state = awaitState() - safeLoginWizard.resetPasswordMailConfirmed(state.resetState.newPassword!!) - } catch (failure: Throwable) { + currentJob = viewModelScope.launch { + val resetState = awaitState().resetState + when (val newPassword = resetState.newPassword) { + null -> { setState { copy(isLoading = false) } - _viewEvents.post(OnboardingViewEvents.Failure(failure)) - return@launch + _viewEvents.post(OnboardingViewEvents.Failure(IllegalStateException("Developer error - No new password has been set"))) } - setState { - copy( - isLoading = false, - resetState = ResetState() + else -> { + runCatching { loginWizard.resetPasswordMailConfirmed(newPassword) }.fold( + onSuccess = { + setState { + copy( + isLoading = false, + resetState = ResetState() + ) + } + _viewEvents.post(OnboardingViewEvents.OnResetPasswordMailConfirmationSuccess) + }, + onFailure = { + setState { copy(isLoading = false) } + _viewEvents.post(OnboardingViewEvents.Failure(it)) + } ) } - - _viewEvents.post(OnboardingViewEvents.OnResetPasswordMailConfirmationSuccess) } } } @@ -520,25 +511,19 @@ class OnboardingViewModel @AssistedInject constructor( private fun handleLogin(action: AuthenticateAction.Login) { val safeLoginWizard = loginWizard - - if (safeLoginWizard == null) { - setState { copy(isLoading = false) } - _viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration"))) - } else { - setState { copy(isLoading = true) } - currentJob = viewModelScope.launch { - try { - val result = safeLoginWizard.login( - action.username, - action.password, - action.initialDeviceName - ) - reAuthHelper.data = action.password - onSessionCreated(result, authenticationDescription = AuthenticationDescription.Login) - } catch (failure: Throwable) { - setState { copy(isLoading = false) } - _viewEvents.post(OnboardingViewEvents.Failure(failure)) - } + setState { copy(isLoading = true) } + currentJob = viewModelScope.launch { + try { + val result = safeLoginWizard.login( + action.username, + action.password, + action.initialDeviceName + ) + reAuthHelper.data = action.password + onSessionCreated(result, authenticationDescription = AuthenticationDescription.Login) + } catch (failure: Throwable) { + setState { copy(isLoading = false) } + _viewEvents.post(OnboardingViewEvents.Failure(failure)) } } }