converting if/else and try/catch to when and runCatching

This commit is contained in:
Adam Brown 2022-05-30 11:15:23 +01:00
parent cc8f17b786
commit 93a247e0ce

View File

@ -128,7 +128,7 @@ class OnboardingViewModel @AssistedInject constructor(
val isRegistrationStarted: Boolean val isRegistrationStarted: Boolean
get() = authenticationService.isRegistrationStarted() get() = authenticationService.isRegistrationStarted()
private val loginWizard: LoginWizard? private val loginWizard: LoginWizard
get() = authenticationService.getLoginWizard() get() = authenticationService.getLoginWizard()
private var loginConfig: LoginConfig? = null private var loginConfig: LoginConfig? = null
@ -447,60 +447,51 @@ class OnboardingViewModel @AssistedInject constructor(
private fun handleResetPassword(action: OnboardingAction.ResetPassword) { private fun handleResetPassword(action: OnboardingAction.ResetPassword) {
val safeLoginWizard = loginWizard val safeLoginWizard = loginWizard
if (safeLoginWizard == null) {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration")))
} else {
setState { copy(isLoading = true) } setState { copy(isLoading = true) }
currentJob = viewModelScope.launch { currentJob = viewModelScope.launch {
try { runCatching { safeLoginWizard.resetPassword(action.email) }.fold(
safeLoginWizard.resetPassword(action.email) onSuccess = {
} catch (failure: Throwable) {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(failure))
return@launch
}
setState { setState {
copy( copy(
isLoading = false, isLoading = false,
resetState = ResetState(email = action.email, newPassword = action.newPassword) resetState = ResetState(email = action.email, newPassword = action.newPassword)
) )
} }
_viewEvents.post(OnboardingViewEvents.OnResetPasswordSendThreePidDone) _viewEvents.post(OnboardingViewEvents.OnResetPasswordSendThreePidDone)
},
onFailure = {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(it))
} }
)
} }
} }
private fun handleResetPasswordMailConfirmed() { 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 { currentJob = viewModelScope.launch {
try { val resetState = awaitState().resetState
val state = awaitState() when (val newPassword = resetState.newPassword) {
safeLoginWizard.resetPasswordMailConfirmed(state.resetState.newPassword!!) null -> {
} catch (failure: Throwable) {
setState { copy(isLoading = false) } setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(failure)) _viewEvents.post(OnboardingViewEvents.Failure(IllegalStateException("Developer error - No new password has been set")))
return@launch
} }
else -> {
runCatching { loginWizard.resetPasswordMailConfirmed(newPassword) }.fold(
onSuccess = {
setState { setState {
copy( copy(
isLoading = false, isLoading = false,
resetState = ResetState() resetState = ResetState()
) )
} }
_viewEvents.post(OnboardingViewEvents.OnResetPasswordMailConfirmationSuccess) _viewEvents.post(OnboardingViewEvents.OnResetPasswordMailConfirmationSuccess)
},
onFailure = {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(it))
}
)
}
} }
} }
} }
@ -520,11 +511,6 @@ class OnboardingViewModel @AssistedInject constructor(
private fun handleLogin(action: AuthenticateAction.Login) { private fun handleLogin(action: AuthenticateAction.Login) {
val safeLoginWizard = loginWizard val safeLoginWizard = loginWizard
if (safeLoginWizard == null) {
setState { copy(isLoading = false) }
_viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration")))
} else {
setState { copy(isLoading = true) } setState { copy(isLoading = true) }
currentJob = viewModelScope.launch { currentJob = viewModelScope.launch {
try { try {
@ -541,7 +527,6 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
} }
}
private fun startAuthenticationFlow() { private fun startAuthenticationFlow() {
// Ensure Wizard is ready // Ensure Wizard is ready