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
|
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
|
||||||
|
setState { copy(isLoading = true) }
|
||||||
if (safeLoginWizard == null) {
|
currentJob = viewModelScope.launch {
|
||||||
setState { copy(isLoading = false) }
|
runCatching { safeLoginWizard.resetPassword(action.email) }.fold(
|
||||||
_viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration")))
|
onSuccess = {
|
||||||
} else {
|
setState {
|
||||||
setState { copy(isLoading = true) }
|
copy(
|
||||||
|
isLoading = false,
|
||||||
currentJob = viewModelScope.launch {
|
resetState = ResetState(email = action.email, newPassword = action.newPassword)
|
||||||
try {
|
)
|
||||||
safeLoginWizard.resetPassword(action.email)
|
}
|
||||||
} catch (failure: Throwable) {
|
_viewEvents.post(OnboardingViewEvents.OnResetPasswordSendThreePidDone)
|
||||||
setState { copy(isLoading = false) }
|
},
|
||||||
_viewEvents.post(OnboardingViewEvents.Failure(failure))
|
onFailure = {
|
||||||
return@launch
|
setState { copy(isLoading = false) }
|
||||||
}
|
_viewEvents.post(OnboardingViewEvents.Failure(it))
|
||||||
|
}
|
||||||
setState {
|
)
|
||||||
copy(
|
|
||||||
isLoading = false,
|
|
||||||
resetState = ResetState(email = action.email, newPassword = action.newPassword)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
_viewEvents.post(OnboardingViewEvents.OnResetPasswordSendThreePidDone)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleResetPasswordMailConfirmed() {
|
private fun handleResetPasswordMailConfirmed() {
|
||||||
val safeLoginWizard = loginWizard
|
currentJob = viewModelScope.launch {
|
||||||
|
val resetState = awaitState().resetState
|
||||||
if (safeLoginWizard == null) {
|
when (val newPassword = resetState.newPassword) {
|
||||||
setState { copy(isLoading = false) }
|
null -> {
|
||||||
_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) {
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
setState {
|
else -> {
|
||||||
copy(
|
runCatching { loginWizard.resetPasswordMailConfirmed(newPassword) }.fold(
|
||||||
isLoading = false,
|
onSuccess = {
|
||||||
resetState = ResetState()
|
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) {
|
private fun handleLogin(action: AuthenticateAction.Login) {
|
||||||
val safeLoginWizard = loginWizard
|
val safeLoginWizard = loginWizard
|
||||||
|
setState { copy(isLoading = true) }
|
||||||
if (safeLoginWizard == null) {
|
currentJob = viewModelScope.launch {
|
||||||
setState { copy(isLoading = false) }
|
try {
|
||||||
_viewEvents.post(OnboardingViewEvents.Failure(Throwable("Bad configuration")))
|
val result = safeLoginWizard.login(
|
||||||
} else {
|
action.username,
|
||||||
setState { copy(isLoading = true) }
|
action.password,
|
||||||
currentJob = viewModelScope.launch {
|
action.initialDeviceName
|
||||||
try {
|
)
|
||||||
val result = safeLoginWizard.login(
|
reAuthHelper.data = action.password
|
||||||
action.username,
|
onSessionCreated(result, authenticationDescription = AuthenticationDescription.Login)
|
||||||
action.password,
|
} catch (failure: Throwable) {
|
||||||
action.initialDeviceName
|
setState { copy(isLoading = false) }
|
||||||
)
|
_viewEvents.post(OnboardingViewEvents.Failure(failure))
|
||||||
reAuthHelper.data = action.password
|
|
||||||
onSessionCreated(result, authenticationDescription = AuthenticationDescription.Login)
|
|
||||||
} catch (failure: Throwable) {
|
|
||||||
setState { copy(isLoading = false) }
|
|
||||||
_viewEvents.post(OnboardingViewEvents.Failure(failure))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue