splitting the success action from the handleRegisterAction, allowing the homeserver editing to start the registation flow

This commit is contained in:
Adam Brown 2022-04-13 16:54:16 +01:00
parent 36ad59dc0f
commit c7065fc123

View file

@ -147,7 +147,7 @@ class OnboardingViewModel @AssistedInject constructor(
is OnboardingAction.WebLoginSuccess -> handleWebLoginSuccess(action) is OnboardingAction.WebLoginSuccess -> handleWebLoginSuccess(action)
is OnboardingAction.ResetPassword -> handleResetPassword(action) is OnboardingAction.ResetPassword -> handleResetPassword(action)
is OnboardingAction.ResetPasswordMailConfirmed -> handleResetPasswordMailConfirmed() is OnboardingAction.ResetPasswordMailConfirmed -> handleResetPasswordMailConfirmed()
is OnboardingAction.PostRegisterAction -> handleRegisterAction(action.registerAction) is OnboardingAction.PostRegisterAction -> handleRegisterAction(action.registerAction, ::emitFlowResultViewEvent)
is OnboardingAction.ResetAction -> handleResetAction(action) is OnboardingAction.ResetAction -> handleResetAction(action)
is OnboardingAction.UserAcceptCertificate -> handleUserAcceptCertificate(action) is OnboardingAction.UserAcceptCertificate -> handleUserAcceptCertificate(action)
OnboardingAction.ClearHomeServerHistory -> handleClearHomeServerHistory() OnboardingAction.ClearHomeServerHistory -> handleClearHomeServerHistory()
@ -255,11 +255,12 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
private fun handleRegisterAction(action: RegisterAction) { private fun handleRegisterAction(action: RegisterAction, onNextRegistrationStepAction: (FlowResult) -> Unit) {
currentJob = viewModelScope.launch { currentJob = viewModelScope.launch {
if (action.hasLoadingState()) { if (action.hasLoadingState()) {
setState { copy(isLoading = true) } setState { copy(isLoading = true) }
} }
runCatching { registrationActionHandler.handleRegisterAction(registrationWizard, action) } runCatching { registrationActionHandler.handleRegisterAction(registrationWizard, action) }
.fold( .fold(
onSuccess = { onSuccess = {
@ -269,7 +270,7 @@ class OnboardingViewModel @AssistedInject constructor(
} }
else -> when (it) { else -> when (it) {
is RegistrationResult.Success -> onSessionCreated(it.session, isAccountCreated = true) is RegistrationResult.Success -> onSessionCreated(it.session, isAccountCreated = true)
is RegistrationResult.FlowResponse -> onFlowResponse(it.flowResult) is RegistrationResult.FlowResponse -> onFlowResponse(it.flowResult, onNextRegistrationStepAction)
} }
} }
}, },
@ -283,13 +284,20 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
private fun emitFlowResultViewEvent(flowResult: FlowResult) {
_viewEvents.post(OnboardingViewEvents.RegistrationFlowResult(flowResult, isRegistrationStarted))
}
private fun handleRegisterWith(action: OnboardingAction.Register) { private fun handleRegisterWith(action: OnboardingAction.Register) {
reAuthHelper.data = action.password reAuthHelper.data = action.password
handleRegisterAction(RegisterAction.CreateAccount( handleRegisterAction(
RegisterAction.CreateAccount(
action.username, action.username,
action.password, action.password,
action.initialDeviceName action.initialDeviceName
)) ),
::emitFlowResultViewEvent
)
} }
private fun handleResetAction(action: OnboardingAction.ResetAction) { private fun handleResetAction(action: OnboardingAction.ResetAction) {
@ -344,7 +352,7 @@ class OnboardingViewModel @AssistedInject constructor(
} }
when (action.signMode) { when (action.signMode) {
SignMode.SignUp -> handleRegisterAction(RegisterAction.StartRegistration) SignMode.SignUp -> handleRegisterAction(RegisterAction.StartRegistration, ::emitFlowResultViewEvent)
SignMode.SignIn -> startAuthenticationFlow() SignMode.SignIn -> startAuthenticationFlow()
SignMode.SignInWithMatrixId -> _viewEvents.post(OnboardingViewEvents.OnSignModeSelected(SignMode.SignInWithMatrixId)) SignMode.SignInWithMatrixId -> _viewEvents.post(OnboardingViewEvents.OnSignModeSelected(SignMode.SignInWithMatrixId))
SignMode.Unknown -> Unit SignMode.Unknown -> Unit
@ -509,18 +517,17 @@ class OnboardingViewModel @AssistedInject constructor(
_viewEvents.post(OnboardingViewEvents.OnSignModeSelected(SignMode.SignIn)) _viewEvents.post(OnboardingViewEvents.OnSignModeSelected(SignMode.SignIn))
} }
private fun onFlowResponse(flowResult: FlowResult) { private fun onFlowResponse(flowResult: FlowResult, onNextRegistrationStepAction: (FlowResult) -> Unit) {
// If dummy stage is mandatory, and password is already sent, do the dummy stage now // If dummy stage is mandatory, and password is already sent, do the dummy stage now
if (isRegistrationStarted && flowResult.missingStages.any { it is Stage.Dummy && it.mandatory }) { if (isRegistrationStarted && flowResult.missingStages.any { it is Stage.Dummy && it.mandatory }) {
handleRegisterDummy() handleRegisterDummy(onNextRegistrationStepAction)
} else { } else {
// Notify the user onNextRegistrationStepAction(flowResult)
_viewEvents.post(OnboardingViewEvents.RegistrationFlowResult(flowResult, isRegistrationStarted))
} }
} }
private fun handleRegisterDummy() { private fun handleRegisterDummy(onNextRegistrationStepAction: (FlowResult) -> Unit) {
handleRegisterAction(RegisterAction.RegisterDummy) handleRegisterAction(RegisterAction.RegisterDummy, onNextRegistrationStepAction)
} }
private suspend fun onSessionCreated(session: Session, isAccountCreated: Boolean) { private suspend fun onSessionCreated(session: Session, isAccountCreated: Boolean) {
@ -599,19 +606,7 @@ class OnboardingViewModel @AssistedInject constructor(
runCatching { startAuthenticationFlowUseCase.execute(homeServerConnectionConfig) }.fold( runCatching { startAuthenticationFlowUseCase.execute(homeServerConnectionConfig) }.fold(
onSuccess = { onSuccess = {
rememberHomeServer(homeServerConnectionConfig.homeServerUri.toString()) onAuthenticationStartedSuccess(homeServerConnectionConfig, it, serverTypeOverride)
if (it.isHomeserverOutdated) {
_viewEvents.post(OnboardingViewEvents.OutdatedHomeserver)
}
setState {
copy(
serverType = alignServerTypeAfterSubmission(homeServerConnectionConfig, serverTypeOverride),
selectedHomeserver = it.selectedHomeserver,
isLoading = false,
)
}
onAuthenticationStartedSuccess()
}, },
onFailure = { onFailure = {
setState { copy(isLoading = false) } setState { copy(isLoading = false) }
@ -621,22 +616,29 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
/** private fun onAuthenticationStartedSuccess(config: HomeServerConnectionConfig, authResult: StartAuthenticationFlowUseCase.StartAuthenticationResult, serverTypeOverride: ServerType?) {
* If user has entered https://matrix.org, ensure that server type is ServerType.MatrixOrg rememberHomeServer(config.homeServerUri.toString())
* It is also useful to set the value again in the case of a certificate error on matrix.org if (authResult.isHomeserverOutdated) {
**/ _viewEvents.post(OnboardingViewEvents.OutdatedHomeserver)
private fun OnboardingViewState.alignServerTypeAfterSubmission(config: HomeServerConnectionConfig, serverTypeOverride: ServerType?): ServerType {
return if (config.homeServerUri.toString() == matrixOrgUrl) {
ServerType.MatrixOrg
} else {
serverTypeOverride ?: serverType
}
} }
private fun onAuthenticationStartedSuccess() { setState {
copy(
serverType = alignServerTypeAfterSubmission(config, serverTypeOverride),
selectedHomeserver = authResult.selectedHomeserver,
isLoading = false
)
}
withState { withState {
when (lastAction) { when (lastAction) {
is OnboardingAction.HomeServerChange.EditHomeServer -> _viewEvents.post(OnboardingViewEvents.OnHomeserverEdited) is OnboardingAction.HomeServerChange.EditHomeServer -> {
when (it.onboardingFlow) {
OnboardingFlow.SignUp -> handleRegisterAction(RegisterAction.StartRegistration) { _ ->
_viewEvents.post(OnboardingViewEvents.OnHomeserverEdited)
}
else -> throw IllegalArgumentException("developer error")
}
}
is OnboardingAction.HomeServerChange.SelectHomeServer -> { is OnboardingAction.HomeServerChange.SelectHomeServer -> {
if (it.selectedHomeserver.preferredLoginMode.supportsSignModeScreen()) { if (it.selectedHomeserver.preferredLoginMode.supportsSignModeScreen()) {
when (it.onboardingFlow) { when (it.onboardingFlow) {
@ -656,6 +658,19 @@ class OnboardingViewModel @AssistedInject constructor(
} }
} }
/**
* If user has entered https://matrix.org, ensure that server type is ServerType.MatrixOrg
* It is also useful to set the value again in the case of a certificate error on matrix.org
**/
private fun OnboardingViewState.alignServerTypeAfterSubmission(config: HomeServerConnectionConfig, serverTypeOverride: ServerType?): ServerType {
return if (config.homeServerUri.toString() == matrixOrgUrl) {
ServerType.MatrixOrg
} else {
serverTypeOverride ?: serverType
}
}
fun getInitialHomeServerUrl(): String? { fun getInitialHomeServerUrl(): String? {
return loginConfig?.homeServerUrl return loginConfig?.homeServerUrl
} }