diff --git a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt index 19f6d226ca..fffb1261be 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/OnboardingViewModel.kt @@ -273,40 +273,37 @@ class OnboardingViewModel @AssistedInject constructor( } private suspend fun internalRegisterAction(action: RegisterAction, overrideNextStage: (() -> Unit)? = null) { - runCatching { registrationActionHandler.processAction(awaitState().selectedHomeserver, action) } - .fold( - onSuccess = { - when (it) { - RegistrationActionHandler.Result.Ignored -> { - // do nothing - } - is RegistrationActionHandler.Result.NextStage -> { - overrideNextStage?.invoke() ?: _viewEvents.post(OnboardingViewEvents.DisplayRegistrationStage(it.stage)) - } - is RegistrationActionHandler.Result.Success -> onSessionCreated( - it.session, - authenticationDescription = awaitState().selectedAuthenticationState.description - ?: AuthenticationDescription.Register(AuthenticationDescription.AuthenticationType.Other) - ) - RegistrationActionHandler.Result.StartRegistration -> _viewEvents.post(OnboardingViewEvents.DisplayStartRegistration) - RegistrationActionHandler.Result.UnsupportedStage -> _viewEvents.post(OnboardingViewEvents.DisplayRegistrationFallback) - is RegistrationActionHandler.Result.SendEmailSuccess -> _viewEvents.post(OnboardingViewEvents.OnSendEmailSuccess(it.email)) - is RegistrationActionHandler.Result.Error -> _viewEvents.post(OnboardingViewEvents.Failure(it.cause)) - RegistrationActionHandler.Result.MissingNextStage -> { - _viewEvents.post(OnboardingViewEvents.Failure(IllegalStateException("No next registration stage found"))) - } - } - }, - onFailure = { - if (it !is CancellationException) { - _viewEvents.post(OnboardingViewEvents.Failure(it)) - } + runCatching { registrationActionHandler.processAction(awaitState().selectedHomeserver, action) }.fold( + onSuccess = { + when (it) { + RegistrationActionHandler.Result.Ignored -> { + // do nothing } - ) + is RegistrationActionHandler.Result.NextStage -> { + overrideNextStage?.invoke() ?: _viewEvents.post(OnboardingViewEvents.DisplayRegistrationStage(it.stage)) + } + is RegistrationActionHandler.Result.RegistrationComplete -> onSessionCreated( + it.session, + authenticationDescription = awaitState().selectedAuthenticationState.description + ?: AuthenticationDescription.Register(AuthenticationDescription.AuthenticationType.Other) + ) + RegistrationActionHandler.Result.StartRegistration -> _viewEvents.post(OnboardingViewEvents.DisplayStartRegistration) + RegistrationActionHandler.Result.UnsupportedStage -> _viewEvents.post(OnboardingViewEvents.DisplayRegistrationFallback) + is RegistrationActionHandler.Result.SendEmailSuccess -> _viewEvents.post(OnboardingViewEvents.OnSendEmailSuccess(it.email)) + is RegistrationActionHandler.Result.Error -> _viewEvents.post(OnboardingViewEvents.Failure(it.cause)) + RegistrationActionHandler.Result.MissingNextStage -> { + _viewEvents.post(OnboardingViewEvents.Failure(IllegalStateException("No next registration stage found"))) + } + } + }, + onFailure = { + if (it !is CancellationException) { + _viewEvents.post(OnboardingViewEvents.Failure(it)) + } + } + ) } - private fun OnboardingViewState.hasSelectedMatrixOrg() = selectedHomeserver.userFacingUrl == matrixOrgUrl - private fun handleRegisterWith(action: AuthenticateAction.Register) { setState { val authDescription = AuthenticationDescription.Register(AuthenticationDescription.AuthenticationType.Password) diff --git a/vector/src/main/java/im/vector/app/features/onboarding/RegistrationActionHandler.kt b/vector/src/main/java/im/vector/app/features/onboarding/RegistrationActionHandler.kt index b9569dc15f..07a6488676 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/RegistrationActionHandler.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/RegistrationActionHandler.kt @@ -45,7 +45,7 @@ class RegistrationActionHandler @Inject constructor( return when { action.ignoresResult() -> Result.Ignored else -> when (result) { - is RegistrationResult.Complete -> Result.Success(result.session) + is RegistrationResult.Complete -> Result.RegistrationComplete(result.session) is RegistrationResult.NextStep -> processFlowResult(result, state) is RegistrationResult.SendEmailSuccess -> Result.SendEmailSuccess(result.email) is RegistrationResult.Error -> Result.Error(result.cause) @@ -91,7 +91,7 @@ class RegistrationActionHandler @Inject constructor( private fun SelectedHomeserverState.hasSelectedMatrixOrg() = userFacingUrl == matrixOrgUrl sealed interface Result { - data class Success(val session: Session) : Result + data class RegistrationComplete(val session: Session) : Result data class NextStage(val stage: Stage) : Result data class Error(val cause: Throwable) : Result data class SendEmailSuccess(val email: String) : Result diff --git a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt index ce32dfeb3d..658e14d411 100644 --- a/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt +++ b/vector/src/test/java/im/vector/app/features/onboarding/OnboardingViewModelTest.kt @@ -31,8 +31,8 @@ import im.vector.app.test.fakes.FakeContext import im.vector.app.test.fakes.FakeDirectLoginUseCase import im.vector.app.test.fakes.FakeHomeServerConnectionConfigFactory import im.vector.app.test.fakes.FakeHomeServerHistoryService -import im.vector.app.test.fakes.FakeRegistrationActionHandler import im.vector.app.test.fakes.FakeLoginWizard +import im.vector.app.test.fakes.FakeRegistrationActionHandler import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeStartAuthenticationFlowUseCase import im.vector.app.test.fakes.FakeStringProvider @@ -316,7 +316,7 @@ class OnboardingViewModelTest { fun `given personalisation enabled, when registering account, then updates state and emits account created event`() = runTest { fakeVectorFeatures.givenPersonalisationEnabled() givenSuccessfullyCreatesAccount(A_HOMESERVER_CAPABILITIES) - givenRegistrationResultFor(RegisterAction.StartRegistration, RegistrationActionHandler.Result.Success(fakeSession)) + givenRegistrationResultFor(RegisterAction.StartRegistration, RegistrationActionHandler.Result.RegistrationComplete(fakeSession)) val test = viewModel.test() viewModel.handle(OnboardingAction.PostRegisterAction(RegisterAction.StartRegistration)) diff --git a/vector/src/test/java/im/vector/app/features/onboarding/RegistrationActionHandlerTest.kt b/vector/src/test/java/im/vector/app/features/onboarding/RegistrationActionHandlerTest.kt index 601f77b36e..7b6f9c615c 100644 --- a/vector/src/test/java/im/vector/app/features/onboarding/RegistrationActionHandlerTest.kt +++ b/vector/src/test/java/im/vector/app/features/onboarding/RegistrationActionHandlerTest.kt @@ -61,12 +61,12 @@ class RegistrationActionHandlerTest { } @Test - fun `given wizard delegate returns success, when handling action, then returns success`() = runTest { + fun `given wizard delegate returns success, when handling action, then returns RegistrationComplete`() = runTest { fakeWizardActionDelegate.givenResultsFor(listOf(RegisterAction.StartRegistration to RegistrationResult.Complete(A_SESSION))) val result = registrationActionHandler.processAction(RegisterAction.StartRegistration) - result shouldBeEqualTo RegistrationActionHandler.Result.Success(A_SESSION) + result shouldBeEqualTo RegistrationActionHandler.Result.RegistrationComplete(A_SESSION) } @Test @@ -154,7 +154,7 @@ class RegistrationActionHandlerTest { val result = registrationActionHandler.processAction(RegisterAction.StartRegistration) - result shouldBeEqualTo RegistrationActionHandler.Result.Success(A_SESSION) + result shouldBeEqualTo RegistrationActionHandler.Result.RegistrationComplete(A_SESSION) } private fun givenFlowResult(stages: List) {