From 074e5bcfb6c252f125d3c49a89e3396458522fa2 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 27 Apr 2022 14:17:40 +0100 Subject: [PATCH] porting registration email verification polling to the registration action handler instead of the fragment --- .../onboarding/RegistrationActionHandler.kt | 33 +++++++++++-------- .../ftueauth/FtueAuthWaitForEmailFragment.kt | 9 ----- 2 files changed, 20 insertions(+), 22 deletions(-) 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 2948b9f301..1c90a728dc 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 @@ -37,7 +37,7 @@ class RegistrationActionHandler @Inject constructor() { is RegisterAction.AddThreePid -> handleAddThreePid(registrationWizard, action) is RegisterAction.SendAgainThreePid -> resultOf { registrationWizard.sendAgainThreePid() } is RegisterAction.ValidateThreePid -> resultOf { registrationWizard.handleValidateThreePid(action.code) } - is RegisterAction.CheckIfEmailHasBeenValidated -> resultOf { registrationWizard.checkIfEmailHasBeenValidated(action.delayMillis) } + is RegisterAction.CheckIfEmailHasBeenValidated -> handleCheckIfEmailIsValidated(registrationWizard, action.delayMillis) is RegisterAction.CreateAccount -> resultOf { registrationWizard.createAccount( action.username, @@ -50,12 +50,7 @@ class RegistrationActionHandler @Inject constructor() { private suspend fun handleAddThreePid(wizard: RegistrationWizard, action: RegisterAction.AddThreePid): RegistrationResult { return runCatching { wizard.addThreePid(action.threePid) }.fold( - onSuccess = { - when (it) { - is Success -> RegistrationResult.Complete(it.session) - is FlowResponse -> RegistrationResult.NextStep(it.flowResult) - } - }, + onSuccess = { it.toRegistrationResult() }, onFailure = { when { action.threePid is RegisterThreePid.Email && it.is401() -> RegistrationResult.SendEmailSuccess(action.threePid.email) @@ -64,20 +59,32 @@ class RegistrationActionHandler @Inject constructor() { } ) } + + private tailrec suspend fun handleCheckIfEmailIsValidated(registrationWizard: RegistrationWizard, delayMillis: Long): RegistrationResult { + return runCatching { registrationWizard.checkIfEmailHasBeenValidated(delayMillis) }.fold( + onSuccess = { it.toRegistrationResult() }, + onFailure = { + when { + it.is401() -> null // recursively continue to check with a delay + else -> RegistrationResult.Error(it) + } + } + ) ?: handleCheckIfEmailIsValidated(registrationWizard, 10_000) + } } private inline fun resultOf(block: () -> SdkRegistrationResult): RegistrationResult { return runCatching { block() }.fold( - onSuccess = { - when (it) { - is FlowResponse -> RegistrationResult.NextStep(it.flowResult) - is Success -> RegistrationResult.Complete(it.session) - } - }, + onSuccess = { it.toRegistrationResult() }, onFailure = { RegistrationResult.Error(it) } ) } +private fun SdkRegistrationResult.toRegistrationResult() = when (this) { + is FlowResponse -> RegistrationResult.NextStep(flowResult) + is Success -> RegistrationResult.Complete(session) +} + sealed interface RegistrationResult { data class Error(val cause: Throwable) : RegistrationResult data class Complete(val session: Session) : RegistrationResult diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthWaitForEmailFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthWaitForEmailFragment.kt index d78e0fe74d..80107c5b90 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthWaitForEmailFragment.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthWaitForEmailFragment.kt @@ -68,15 +68,6 @@ class FtueAuthWaitForEmailFragment @Inject constructor() : AbstractFtueAuthFragm views.loginWaitForEmailNotice.text = getString(R.string.login_wait_for_email_notice, params.email) } - override fun onError(throwable: Throwable) { - if (throwable.is401()) { - // Try again, with a delay - viewModel.handle(OnboardingAction.PostRegisterAction(RegisterAction.CheckIfEmailHasBeenValidated(10_000))) - } else { - super.onError(throwable) - } - } - override fun resetViewModel() { viewModel.handle(OnboardingAction.ResetAuthenticationAttempt) }