porting registration email verification polling to the registration action handler instead of the fragment

This commit is contained in:
Adam Brown 2022-04-27 14:17:40 +01:00
parent d4a5b71a4d
commit 074e5bcfb6
2 changed files with 20 additions and 22 deletions

View File

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

View File

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