From d4a5b71a4d571f95309331e1074965ecff38c5bb Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 26 Apr 2022 17:35:42 +0100 Subject: [PATCH 01/27] adding email input FTUE screen - lifts the threepid email error handling to the RegistrationActionHandler rather than having the UI infer success from a 401 --- .../im/vector/app/core/di/FragmentModule.kt | 6 + .../app/core/extensions/TextInputLayout.kt | 27 ++++ .../onboarding/OnboardingViewModel.kt | 7 +- .../onboarding/RegistrationActionHandler.kt | 68 +++++++-- .../FtueAuthChooseDisplayNameFragment.kt | 5 +- .../ftueauth/FtueAuthEmailEntryFragment.kt | 87 ++++++++++++ .../FtueAuthGenericTextInputFormFragment.kt | 7 +- .../onboarding/ftueauth/FtueAuthVariant.kt | 17 ++- vector/src/main/res/drawable/ic_email.xml | 10 ++ .../res/layout/fragment_ftue_email_input.xml | 133 ++++++++++++++++++ vector/src/main/res/values/donottranslate.xml | 4 + .../onboarding/OnboardingViewModelTest.kt | 13 +- .../RegistrationActionHandlerTest.kt | 23 ++- .../test/fakes/FakeRegisterActionHandler.kt | 2 +- .../app/test/fakes/FakeRegistrationWizard.kt | 5 + .../app/test/fixtures/FailureFixture.kt | 25 ++++ 16 files changed, 400 insertions(+), 39 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthEmailEntryFragment.kt create mode 100644 vector/src/main/res/drawable/ic_email.xml create mode 100644 vector/src/main/res/layout/fragment_ftue_email_input.xml create mode 100644 vector/src/test/java/im/vector/app/test/fixtures/FailureFixture.kt diff --git a/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt b/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt index c68a35f4e5..a9dcdc3700 100644 --- a/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt +++ b/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt @@ -101,6 +101,7 @@ import im.vector.app.features.onboarding.ftueauth.FtueAuthAccountCreatedFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthCaptchaFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthChooseDisplayNameFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthChooseProfilePictureFragment +import im.vector.app.features.onboarding.ftueauth.FtueAuthEmailEntryFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthGenericTextInputFormFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthLegacyStyleCaptchaFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthLoginFragment @@ -494,6 +495,11 @@ interface FragmentModule { @FragmentKey(FtueAuthAccountCreatedFragment::class) fun bindFtueAuthAccountCreatedFragment(fragment: FtueAuthAccountCreatedFragment): Fragment + @Binds + @IntoMap + @FragmentKey(FtueAuthEmailEntryFragment::class) + fun bindFtueAuthEmailEntryFragment(fragment: FtueAuthEmailEntryFragment): Fragment + @Binds @IntoMap @FragmentKey(FtueAuthChooseDisplayNameFragment::class) diff --git a/vector/src/main/java/im/vector/app/core/extensions/TextInputLayout.kt b/vector/src/main/java/im/vector/app/core/extensions/TextInputLayout.kt index d9b92e78b7..55e75af537 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/TextInputLayout.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/TextInputLayout.kt @@ -16,7 +16,11 @@ package im.vector.app.core.extensions +import android.text.Editable +import android.view.View +import android.view.inputmethod.EditorInfo import com.google.android.material.textfield.TextInputLayout +import im.vector.app.core.platform.SimpleTextWatcher import kotlinx.coroutines.flow.map import reactivecircus.flowbinding.android.widget.textChanges @@ -30,3 +34,26 @@ fun TextInputLayout.hasSurroundingSpaces() = editText().text.toString().let { it fun TextInputLayout.hasContentFlow(mapper: (CharSequence) -> CharSequence = { it }) = editText().textChanges().map { mapper(it).isNotEmpty() } fun TextInputLayout.content() = editText().text.toString() + +fun TextInputLayout.hasContent() = !editText?.text.isNullOrEmpty() + +fun TextInputLayout.associateContentStateWith(button: View) { + editText?.addTextChangedListener(object : SimpleTextWatcher() { + override fun afterTextChanged(s: Editable) { + val newContent = s.toString() + button.isEnabled = newContent.isNotEmpty() + } + }) +} + +fun TextInputLayout.setOnImeDone(action: () -> Unit) { + editText?.setOnEditorActionListener { _, actionId, _ -> + when (actionId) { + EditorInfo.IME_ACTION_DONE -> { + action() + true + } + else -> false + } + } +} 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 03526b47a5..ccd7eebe87 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 @@ -50,7 +50,6 @@ import org.matrix.android.sdk.api.auth.HomeServerHistoryService import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.auth.login.LoginWizard import org.matrix.android.sdk.api.auth.registration.FlowResult -import org.matrix.android.sdk.api.auth.registration.RegistrationResult import org.matrix.android.sdk.api.auth.registration.RegistrationWizard import org.matrix.android.sdk.api.auth.registration.Stage import org.matrix.android.sdk.api.session.Session @@ -275,8 +274,10 @@ class OnboardingViewModel @AssistedInject constructor( // do nothing } else -> when (it) { - is RegistrationResult.Success -> onSessionCreated(it.session, isAccountCreated = true) - is RegistrationResult.FlowResponse -> onFlowResponse(it.flowResult, onNextRegistrationStepAction) + is RegistrationResult.Complete -> onSessionCreated(it.session, isAccountCreated = true) + is RegistrationResult.NextStep -> onFlowResponse(it.flowResult, onNextRegistrationStepAction) + is RegistrationResult.SendEmailSuccess -> _viewEvents.post(OnboardingViewEvents.OnSendEmailSuccess(it.email)) + is RegistrationResult.Error -> _viewEvents.post(OnboardingViewEvents.Failure(it.cause)) } } }, 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 b4998d2ba0..2948b9f301 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 @@ -16,26 +16,73 @@ package im.vector.app.features.onboarding +import org.matrix.android.sdk.api.auth.registration.FlowResult import org.matrix.android.sdk.api.auth.registration.RegisterThreePid -import org.matrix.android.sdk.api.auth.registration.RegistrationResult +import org.matrix.android.sdk.api.auth.registration.RegistrationResult.FlowResponse +import org.matrix.android.sdk.api.auth.registration.RegistrationResult.Success import org.matrix.android.sdk.api.auth.registration.RegistrationWizard +import org.matrix.android.sdk.api.failure.is401 +import org.matrix.android.sdk.api.session.Session import javax.inject.Inject +import org.matrix.android.sdk.api.auth.registration.RegistrationResult as SdkRegistrationResult class RegistrationActionHandler @Inject constructor() { suspend fun handleRegisterAction(registrationWizard: RegistrationWizard, action: RegisterAction): RegistrationResult { return when (action) { - RegisterAction.StartRegistration -> registrationWizard.getRegistrationFlow() - is RegisterAction.CaptchaDone -> registrationWizard.performReCaptcha(action.captchaResponse) - is RegisterAction.AcceptTerms -> registrationWizard.acceptTerms() - is RegisterAction.RegisterDummy -> registrationWizard.dummy() - is RegisterAction.AddThreePid -> registrationWizard.addThreePid(action.threePid) - is RegisterAction.SendAgainThreePid -> registrationWizard.sendAgainThreePid() - is RegisterAction.ValidateThreePid -> registrationWizard.handleValidateThreePid(action.code) - is RegisterAction.CheckIfEmailHasBeenValidated -> registrationWizard.checkIfEmailHasBeenValidated(action.delayMillis) - is RegisterAction.CreateAccount -> registrationWizard.createAccount(action.username, action.password, action.initialDeviceName) + RegisterAction.StartRegistration -> resultOf { registrationWizard.getRegistrationFlow() } + is RegisterAction.CaptchaDone -> resultOf { registrationWizard.performReCaptcha(action.captchaResponse) } + is RegisterAction.AcceptTerms -> resultOf { registrationWizard.acceptTerms() } + is RegisterAction.RegisterDummy -> resultOf { registrationWizard.dummy() } + 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.CreateAccount -> resultOf { + registrationWizard.createAccount( + action.username, + action.password, + action.initialDeviceName + ) + } } } + + 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) + } + }, + onFailure = { + when { + action.threePid is RegisterThreePid.Email && it.is401() -> RegistrationResult.SendEmailSuccess(action.threePid.email) + else -> RegistrationResult.Error(it) + } + } + ) + } +} + +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) + } + }, + onFailure = { RegistrationResult.Error(it) } + ) +} + +sealed interface RegistrationResult { + data class Error(val cause: Throwable) : RegistrationResult + data class Complete(val session: Session) : RegistrationResult + data class NextStep(val flowResult: FlowResult) : RegistrationResult + data class SendEmailSuccess(val email: String) : RegistrationResult } sealed interface RegisterAction { @@ -56,7 +103,6 @@ sealed interface RegisterAction { } fun RegisterAction.ignoresResult() = when (this) { - is RegisterAction.AddThreePid -> true is RegisterAction.SendAgainThreePid -> true else -> false } diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthChooseDisplayNameFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthChooseDisplayNameFragment.kt index 1ce0c544e5..c64e22bc9c 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthChooseDisplayNameFragment.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthChooseDisplayNameFragment.kt @@ -23,6 +23,7 @@ import android.view.View import android.view.ViewGroup import android.view.inputmethod.EditorInfo import com.google.android.material.textfield.TextInputLayout +import im.vector.app.core.extensions.hasContent import im.vector.app.core.platform.SimpleTextWatcher import im.vector.app.databinding.FragmentFtueDisplayNameBinding import im.vector.app.features.onboarding.OnboardingAction @@ -69,7 +70,7 @@ class FtueAuthChooseDisplayNameFragment @Inject constructor() : AbstractFtueAuth override fun updateWithState(state: OnboardingViewState) { views.displayNameInput.editText?.setText(state.personalizationState.displayName) - views.displayNameSubmit.isEnabled = views.displayNameInput.hasContentEmpty() + views.displayNameSubmit.isEnabled = views.displayNameInput.hasContent() } override fun resetViewModel() { @@ -81,5 +82,3 @@ class FtueAuthChooseDisplayNameFragment @Inject constructor() : AbstractFtueAuth return true } } - -private fun TextInputLayout.hasContentEmpty() = !editText?.text.isNullOrEmpty() diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthEmailEntryFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthEmailEntryFragment.kt new file mode 100644 index 0000000000..1dd99aa587 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthEmailEntryFragment.kt @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.onboarding.ftueauth + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope +import im.vector.app.core.extensions.associateContentStateWith +import im.vector.app.core.extensions.content +import im.vector.app.core.extensions.editText +import im.vector.app.core.extensions.hasContent +import im.vector.app.core.extensions.isEmail +import im.vector.app.core.extensions.setOnImeDone +import im.vector.app.databinding.FragmentFtueEmailInputBinding +import im.vector.app.features.onboarding.OnboardingAction +import im.vector.app.features.onboarding.OnboardingViewEvents +import im.vector.app.features.onboarding.OnboardingViewState +import im.vector.app.features.onboarding.RegisterAction +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach +import org.matrix.android.sdk.api.auth.registration.RegisterThreePid +import org.matrix.android.sdk.api.failure.is401 +import reactivecircus.flowbinding.android.widget.textChanges +import javax.inject.Inject + +class FtueAuthEmailEntryFragment @Inject constructor() : AbstractFtueAuthFragment() { + + override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentFtueEmailInputBinding { + return FragmentFtueEmailInputBinding.inflate(inflater, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setupViews() + } + + private fun setupViews() { + views.emailEntryInput.associateContentStateWith(button = views.emailEntrySubmit) + views.emailEntryInput.setOnImeDone { updateEmail() } + views.emailEntrySubmit.debouncedClicks { updateEmail() } + + views.emailEntryInput.editText().textChanges() + .onEach { + views.emailEntryInput.error = null + views.emailEntrySubmit.isEnabled = it.isEmail() + } + .launchIn(lifecycleScope) + } + + private fun updateEmail() { + val email = views.emailEntryInput.content() + viewModel.handle(OnboardingAction.PostRegisterAction(RegisterAction.AddThreePid(RegisterThreePid.Email(email)))) + } + + override fun onError(throwable: Throwable) { + views.emailEntryInput.error = errorFormatter.toHumanReadable(throwable) + } + + override fun updateWithState(state: OnboardingViewState) { + views.emailEntrySubmit.isEnabled = views.emailEntryInput.content().isEmail() + } + + override fun resetViewModel() { + // Nothing to do + } + + override fun onBackPressed(toolbarButton: Boolean): Boolean { + viewModel.handle(OnboardingAction.PostViewEvent(OnboardingViewEvents.OnTakeMeHome)) + return true + } +} diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthGenericTextInputFormFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthGenericTextInputFormFragment.kt index ce3dee7a19..fce1308d3c 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthGenericTextInputFormFragment.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthGenericTextInputFormFragment.kt @@ -223,12 +223,7 @@ class FtueAuthGenericTextInputFormFragment @Inject constructor() : AbstractFtueA override fun onError(throwable: Throwable) { when (params.mode) { TextInputFormFragmentMode.SetEmail -> { - if (throwable.is401()) { - // This is normal use case, we go to the mail waiting screen - viewModel.handle(OnboardingAction.PostViewEvent(OnboardingViewEvents.OnSendEmailSuccess(viewModel.currentThreePid ?: ""))) - } else { - views.loginGenericTextInputFormTil.error = errorFormatter.toHumanReadable(throwable) - } + views.loginGenericTextInputFormTil.error = errorFormatter.toHumanReadable(throwable) } TextInputFormFragmentMode.SetMsisdn -> { if (throwable.is401()) { diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthVariant.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthVariant.kt index 6f1b85df4f..a232a6ddba 100644 --- a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthVariant.kt +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/FtueAuthVariant.kt @@ -393,10 +393,7 @@ class FtueAuthVariant( when (stage) { is Stage.ReCaptcha -> onCaptcha(stage) - is Stage.Email -> addRegistrationStageFragmentToBackstack( - FtueAuthGenericTextInputFormFragment::class.java, - FtueAuthGenericTextInputFormFragmentArgument(TextInputFormFragmentMode.SetEmail, stage.mandatory), - ) + is Stage.Email -> onEmail(stage) is Stage.Msisdn -> addRegistrationStageFragmentToBackstack( FtueAuthGenericTextInputFormFragment::class.java, FtueAuthGenericTextInputFormFragmentArgument(TextInputFormFragmentMode.SetMsisdn, stage.mandatory), @@ -406,6 +403,18 @@ class FtueAuthVariant( } } + private fun onEmail(stage: Stage) { + when { + vectorFeatures.isOnboardingCombinedRegisterEnabled() -> addRegistrationStageFragmentToBackstack( + FtueAuthEmailEntryFragment::class.java + ) + else -> addRegistrationStageFragmentToBackstack( + FtueAuthGenericTextInputFormFragment::class.java, + FtueAuthGenericTextInputFormFragmentArgument(TextInputFormFragmentMode.SetEmail, stage.mandatory), + ) + } + } + private fun onTerms(stage: Stage.Terms) { when { vectorFeatures.isOnboardingCombinedRegisterEnabled() -> addRegistrationStageFragmentToBackstack( diff --git a/vector/src/main/res/drawable/ic_email.xml b/vector/src/main/res/drawable/ic_email.xml new file mode 100644 index 0000000000..48de7aec41 --- /dev/null +++ b/vector/src/main/res/drawable/ic_email.xml @@ -0,0 +1,10 @@ + + + diff --git a/vector/src/main/res/layout/fragment_ftue_email_input.xml b/vector/src/main/res/layout/fragment_ftue_email_input.xml new file mode 100644 index 0000000000..95740ec94b --- /dev/null +++ b/vector/src/main/res/layout/fragment_ftue_email_input.xml @@ -0,0 +1,133 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +