From 06147967a4257d8d3b7bc4facbd66a71e8148673 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 6 Apr 2022 16:37:48 +0100 Subject: [PATCH] creating a ftue version of the policy fragment --- .../im/vector/app/core/di/FragmentModule.kt | 8 +- .../onboarding/ftueauth/FtueAuthVariant.kt | 18 ++- .../ftueauth/terms/FtueAuthTermsFragment.kt | 120 +++++++++++++++++ .../res/layout/fragment_ftue_login_terms.xml | 121 ++++++++++++++++++ 4 files changed, 264 insertions(+), 3 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/onboarding/ftueauth/terms/FtueAuthTermsFragment.kt create mode 100644 vector/src/main/res/layout/fragment_ftue_login_terms.xml 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 91831fc56b..e3e64063f3 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 @@ -116,6 +116,7 @@ import im.vector.app.features.onboarding.ftueauth.FtueAuthUseCaseFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthWaitForEmailFragment import im.vector.app.features.onboarding.ftueauth.FtueAuthWebFragment import im.vector.app.features.onboarding.ftueauth.terms.FtueAuthLegacyStyleTermsFragment +import im.vector.app.features.onboarding.ftueauth.terms.FtueAuthTermsFragment import im.vector.app.features.pin.PinFragment import im.vector.app.features.poll.create.CreatePollFragment import im.vector.app.features.qrcode.QrCodeScannerFragment @@ -481,7 +482,12 @@ interface FragmentModule { @Binds @IntoMap @FragmentKey(FtueAuthLegacyStyleTermsFragment::class) - fun bindFtueAuthTermsFragment(fragment: FtueAuthLegacyStyleTermsFragment): Fragment + fun bindFtueAuthLegacyStyleTermsFragment(fragment: FtueAuthLegacyStyleTermsFragment): Fragment + + @Binds + @IntoMap + @FragmentKey(FtueAuthTermsFragment::class) + fun bindFtueAuthTermsFragment(fragment: FtueAuthTermsFragment): Fragment @Binds @IntoMap 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 be6d30e5ce..c8f88280d6 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 @@ -52,6 +52,7 @@ import im.vector.app.features.onboarding.OnboardingViewEvents import im.vector.app.features.onboarding.OnboardingViewModel import im.vector.app.features.onboarding.OnboardingViewState import im.vector.app.features.onboarding.ftueauth.terms.FtueAuthLegacyStyleTermsFragment +import im.vector.app.features.onboarding.ftueauth.terms.FtueAuthTermsFragment import im.vector.app.features.onboarding.ftueauth.terms.FtueAuthTermsLegacyStyleFragmentArgument import org.matrix.android.sdk.api.auth.registration.FlowResult import org.matrix.android.sdk.api.auth.registration.Stage @@ -397,14 +398,27 @@ class FtueAuthVariant( tag = FRAGMENT_REGISTRATION_STAGE_TAG, option = commonOption ) - is Stage.Terms -> activity.addFragmentToBackstack( + is Stage.Terms -> onTerms(stage) + else -> Unit // Should not happen + } + } + + private fun onTerms(stage: Stage.Terms) { + when { + vectorFeatures.isOnboardingCombinedRegisterEnabled() -> activity.addFragmentToBackstack( + views.loginFragmentContainer, + FtueAuthTermsFragment::class.java, + FtueAuthTermsLegacyStyleFragmentArgument(stage.policies.toLocalizedLoginTerms(activity.getString(R.string.resources_language))), + tag = FRAGMENT_REGISTRATION_STAGE_TAG, + option = commonOption + ) + else -> activity.addFragmentToBackstack( views.loginFragmentContainer, FtueAuthLegacyStyleTermsFragment::class.java, FtueAuthTermsLegacyStyleFragmentArgument(stage.policies.toLocalizedLoginTerms(activity.getString(R.string.resources_language))), tag = FRAGMENT_REGISTRATION_STAGE_TAG, option = commonOption ) - else -> Unit // Should not happen } } diff --git a/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/terms/FtueAuthTermsFragment.kt b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/terms/FtueAuthTermsFragment.kt new file mode 100644 index 0000000000..0ba84e32c8 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/onboarding/ftueauth/terms/FtueAuthTermsFragment.kt @@ -0,0 +1,120 @@ +/* + * 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.terms + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.airbnb.mvrx.args +import im.vector.app.core.extensions.cleanup +import im.vector.app.core.extensions.configureWith +import im.vector.app.core.extensions.toReducedUrl +import im.vector.app.core.utils.openUrlInChromeCustomTab +import im.vector.app.databinding.FragmentFtueLoginTermsBinding +import im.vector.app.features.login.terms.LocalizedFlowDataLoginTermsChecked +import im.vector.app.features.login.terms.LoginTermsViewState +import im.vector.app.features.login.terms.PolicyController +import im.vector.app.features.onboarding.OnboardingAction +import im.vector.app.features.onboarding.OnboardingViewState +import im.vector.app.features.onboarding.RegisterAction +import im.vector.app.features.onboarding.ftueauth.AbstractFtueAuthFragment +import org.matrix.android.sdk.internal.auth.registration.LocalizedFlowDataLoginTerms +import javax.inject.Inject + +/** + * LoginTermsFragment displays the list of policies the user has to accept + */ +class FtueAuthTermsFragment @Inject constructor( + private val policyController: PolicyController +) : AbstractFtueAuthFragment(), + PolicyController.PolicyControllerListener { + + private val params: FtueAuthTermsLegacyStyleFragmentArgument by args() + + override fun getBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentFtueLoginTermsBinding { + return FragmentFtueLoginTermsBinding.inflate(inflater, container, false) + } + + private var loginTermsViewState: LoginTermsViewState = LoginTermsViewState(emptyList()) + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + setupViews() + views.loginTermsPolicyList.configureWith(policyController, hasFixedSize = false) + policyController.listener = this + + val list = ArrayList() + + params.localizedFlowDataLoginTerms + .forEach { + list.add(LocalizedFlowDataLoginTermsChecked(it)) + } + + loginTermsViewState = LoginTermsViewState(list) + } + + private fun setupViews() { + views.displayNameSubmit.setOnClickListener { submit() } + views.loginTermsPolicyList.setHasFixedSize(false) + } + + override fun onDestroyView() { + views.loginTermsPolicyList.cleanup() + policyController.listener = null + super.onDestroyView() + } + + private fun renderState() { + policyController.setData(loginTermsViewState.localizedFlowDataLoginTermsChecked) + + // Button is enabled only if all checkboxes are checked + views.displayNameSubmit.isEnabled = loginTermsViewState.allChecked() + } + + override fun setChecked(localizedFlowDataLoginTerms: LocalizedFlowDataLoginTerms, isChecked: Boolean) { + if (isChecked) { + loginTermsViewState.check(localizedFlowDataLoginTerms) + } else { + loginTermsViewState.uncheck(localizedFlowDataLoginTerms) + } + + renderState() + } + + override fun openPolicy(localizedFlowDataLoginTerms: LocalizedFlowDataLoginTerms) { + localizedFlowDataLoginTerms.localizedUrl + ?.takeIf { it.isNotBlank() } + ?.let { + openUrlInChromeCustomTab(requireContext(), null, it) + } + } + + private fun submit() { + viewModel.handle(OnboardingAction.PostRegisterAction(RegisterAction.AcceptTerms)) + } + + override fun updateWithState(state: OnboardingViewState) { + policyController.homeServer = state.selectedHomeserver.userFacingUrl.toReducedUrl() + renderState() + } + + override fun resetViewModel() { + viewModel.handle(OnboardingAction.ResetAuthenticationAttempt) + } +} diff --git a/vector/src/main/res/layout/fragment_ftue_login_terms.xml b/vector/src/main/res/layout/fragment_ftue_login_terms.xml new file mode 100644 index 0000000000..1a6de5e1ec --- /dev/null +++ b/vector/src/main/res/layout/fragment_ftue_login_terms.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + +