mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 10:25:35 +03:00
converting if/else and try/catch to when and runCatching
This commit is contained in:
parent
cc8f17b786
commit
93a247e0ce
1 changed files with 51 additions and 66 deletions
|
@ -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
|
||||
}
|
||||
|
||||
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) {
|
||||
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")))
|
||||
}
|
||||
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))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -520,11 +511,6 @@ 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 {
|
||||
|
@ -541,7 +527,6 @@ class OnboardingViewModel @AssistedInject constructor(
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun startAuthenticationFlow() {
|
||||
// Ensure Wizard is ready
|
||||
|
|
Loading…
Reference in a new issue