flattening loading state to the handle entry points, reducing duplication

This commit is contained in:
Adam Brown 2022-04-14 11:27:13 +01:00
parent 5001be9f21
commit ee693b5ad4
1 changed files with 13 additions and 16 deletions

View File

@ -257,15 +257,15 @@ class OnboardingViewModel @AssistedInject constructor(
private fun handleRegisterAction(action: RegisterAction, onNextRegistrationStepAction: (FlowResult) -> Unit) { private fun handleRegisterAction(action: RegisterAction, onNextRegistrationStepAction: (FlowResult) -> Unit) {
currentJob = viewModelScope.launch { currentJob = viewModelScope.launch {
if (action.hasLoadingState()) {
setState { copy(isLoading = true) }
}
internalRegisterAction(action, onNextRegistrationStepAction) internalRegisterAction(action, onNextRegistrationStepAction)
setState { copy(isLoading = false) }
} }
} }
private suspend fun internalRegisterAction(action: RegisterAction, onNextRegistrationStepAction: (FlowResult) -> Unit) { private suspend fun internalRegisterAction(action: RegisterAction, onNextRegistrationStepAction: (FlowResult) -> Unit) {
if (action.hasLoadingState()) {
setState { copy(isLoading = true) }
}
runCatching { registrationActionHandler.handleRegisterAction(registrationWizard, action) } runCatching { registrationActionHandler.handleRegisterAction(registrationWizard, action) }
.fold( .fold(
onSuccess = { onSuccess = {
@ -285,7 +285,6 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
) )
setState { copy(isLoading = false) }
} }
private fun emitFlowResultViewEvent(flowResult: FlowResult) { private fun emitFlowResultViewEvent(flowResult: FlowResult) {
@ -293,17 +292,15 @@ class OnboardingViewModel @AssistedInject constructor(
} }
private fun handleRegisterWith(action: OnboardingAction.Register) { private fun handleRegisterWith(action: OnboardingAction.Register) {
currentJob = viewModelScope.launch { reAuthHelper.data = action.password
reAuthHelper.data = action.password handleRegisterAction(
internalRegisterAction( RegisterAction.CreateAccount(
RegisterAction.CreateAccount( action.username,
action.username, action.password,
action.password, action.initialDeviceName
action.initialDeviceName ),
), ::emitFlowResultViewEvent
::emitFlowResultViewEvent )
)
}
} }
private fun handleResetAction(action: OnboardingAction.ResetAction) { private fun handleResetAction(action: OnboardingAction.ResetAction) {