renaming success type to something more concrete

This commit is contained in:
Adam Brown 2022-06-08 10:29:56 +01:00
parent 88167a0287
commit befcfe8c5b
4 changed files with 35 additions and 38 deletions

View File

@ -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)

View File

@ -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

View File

@ -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))

View File

@ -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<Stage>) {