From a193b2659d636b495b5b61e9160ea6e155e98397 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 11 Dec 2019 16:39:20 +0100 Subject: [PATCH] Create Uri extension and cleanup login code --- .../im/vector/riotx/core/di/ActiveSessionHolder.kt | 2 +- .../im/vector/riotx/core/extensions/UrlExtensions.kt | 10 ++++++++++ .../im/vector/riotx/features/login/LoginActivity.kt | 4 ++-- .../im/vector/riotx/features/login/LoginFragment.kt | 5 +++-- .../riotx/features/login/LoginResetPasswordFragment.kt | 3 ++- .../login/LoginSignUpSignInSelectionFragment.kt | 7 ++++--- .../im/vector/riotx/features/login/LoginViewState.kt | 8 -------- .../riotx/features/login/terms/LoginTermsFragment.kt | 3 ++- 8 files changed, 24 insertions(+), 18 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt b/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt index 12dfcbcaac..ff9865c3ea 100644 --- a/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt +++ b/vector/src/main/java/im/vector/riotx/core/di/ActiveSessionHolder.kt @@ -37,7 +37,7 @@ class ActiveSessionHolder @Inject constructor(private val authenticationService: fun setActiveSession(session: Session) { activeSession.set(session) - sessionObservableStore.post(Option.fromNullable(session)) + sessionObservableStore.post(Option.just(session)) keyRequestHandler.start(session) incomingVerificationRequestHandler.start(session) } diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/UrlExtensions.kt b/vector/src/main/java/im/vector/riotx/core/extensions/UrlExtensions.kt index 9a26cedf9a..2141b571a3 100644 --- a/vector/src/main/java/im/vector/riotx/core/extensions/UrlExtensions.kt +++ b/vector/src/main/java/im/vector/riotx/core/extensions/UrlExtensions.kt @@ -35,3 +35,13 @@ fun StringBuilder.appendParamToUrl(param: String, value: String): StringBuilder return this } + + +/** + * Ex: "https://matrix.org/" -> "matrix.org" + */ +fun String?.toReducedUrl(): String { + return (this ?: "") + .substringAfter("://") + .trim { it == '/' } +} diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginActivity.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginActivity.kt index 2dec402f85..fd3094d5ae 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginActivity.kt @@ -96,7 +96,8 @@ class LoginActivity : VectorBaseActivity(), ToolbarConfigurable { } loginSharedActionViewModel = viewModelProvider.get(LoginSharedActionViewModel::class.java) - loginSharedActionViewModel.observe() + loginSharedActionViewModel + .observe() .subscribe { handleLoginNavigation(it) } @@ -106,7 +107,6 @@ class LoginActivity : VectorBaseActivity(), ToolbarConfigurable { .subscribe(this) { updateWithState(it) } - .disposeOnDestroy() loginViewModel.viewEvents .observe() diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt index 2d3b12bf53..981f214aa8 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginFragment.kt @@ -32,6 +32,7 @@ import im.vector.riotx.R import im.vector.riotx.core.error.ErrorFormatter import im.vector.riotx.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.showPassword +import im.vector.riotx.core.extensions.toReducedUrl import io.reactivex.Observable import io.reactivex.functions.BiFunction import io.reactivex.rxkotlin.subscribeBy @@ -103,7 +104,7 @@ class LoginFragment @Inject constructor( ServerType.MatrixOrg -> { loginServerIcon.isVisible = true loginServerIcon.setImageResource(R.drawable.ic_logo_matrix_org) - loginTitle.text = getString(resId, state.homeServerUrlSimple) + loginTitle.text = getString(resId, state.homeServerUrl.toReducedUrl()) loginNotice.text = getString(R.string.login_server_matrix_org_text) } ServerType.Modular -> { @@ -114,7 +115,7 @@ class LoginFragment @Inject constructor( } ServerType.Other -> { loginServerIcon.isVisible = false - loginTitle.text = getString(resId, state.homeServerUrlSimple) + loginTitle.text = getString(resId, state.homeServerUrl.toReducedUrl()) loginNotice.text = getString(R.string.login_server_other_text) } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginResetPasswordFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginResetPasswordFragment.kt index 18fcd8938b..d8e2fcd589 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginResetPasswordFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginResetPasswordFragment.kt @@ -29,6 +29,7 @@ import im.vector.riotx.core.error.ErrorFormatter import im.vector.riotx.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.isEmail import im.vector.riotx.core.extensions.showPassword +import im.vector.riotx.core.extensions.toReducedUrl import io.reactivex.Observable import io.reactivex.functions.BiFunction import io.reactivex.rxkotlin.subscribeBy @@ -57,7 +58,7 @@ class LoginResetPasswordFragment @Inject constructor( } private fun setupUi(state: LoginViewState) { - resetPasswordTitle.text = getString(R.string.login_reset_password_on, state.homeServerUrlSimple) + resetPasswordTitle.text = getString(R.string.login_reset_password_on, state.homeServerUrl.toReducedUrl()) } private fun setupSubmitButton() { diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginSignUpSignInSelectionFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginSignUpSignInSelectionFragment.kt index 0484357ae2..e77499176b 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginSignUpSignInSelectionFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginSignUpSignInSelectionFragment.kt @@ -21,6 +21,7 @@ import androidx.core.view.isVisible import butterknife.OnClick import im.vector.riotx.R import im.vector.riotx.core.error.ErrorFormatter +import im.vector.riotx.core.extensions.toReducedUrl import kotlinx.android.synthetic.main.fragment_login_signup_signin_selection.* import javax.inject.Inject @@ -40,19 +41,19 @@ class LoginSignUpSignInSelectionFragment @Inject constructor( ServerType.MatrixOrg -> { loginSignupSigninServerIcon.setImageResource(R.drawable.ic_logo_matrix_org) loginSignupSigninServerIcon.isVisible = true - loginSignupSigninTitle.text = getString(R.string.login_connect_to, state.homeServerUrlSimple) + loginSignupSigninTitle.text = getString(R.string.login_connect_to, state.homeServerUrl.toReducedUrl()) loginSignupSigninText.text = getString(R.string.login_server_matrix_org_text) } ServerType.Modular -> { loginSignupSigninServerIcon.setImageResource(R.drawable.ic_logo_modular) loginSignupSigninServerIcon.isVisible = true loginSignupSigninTitle.text = getString(R.string.login_connect_to_modular) - loginSignupSigninText.text = state.homeServerUrlSimple + loginSignupSigninText.text = state.homeServerUrl.toReducedUrl() } ServerType.Other -> { loginSignupSigninServerIcon.isVisible = false loginSignupSigninTitle.text = getString(R.string.login_server_other_title) - loginSignupSigninText.text = getString(R.string.login_connect_to, state.homeServerUrlSimple) + loginSignupSigninText.text = getString(R.string.login_connect_to, state.homeServerUrl.toReducedUrl()) } } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt index e4b3fe214a..13e3d25fe0 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginViewState.kt @@ -54,12 +54,4 @@ data class LoginViewState( fun isUserLogged(): Boolean { return asyncLoginAction is Success } - - /** - * Ex: "https://matrix.org/" -> "matrix.org" - */ - val homeServerUrlSimple: String - get() = (homeServerUrl ?: "") - .substringAfter("://") - .trim { it == '/' } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/terms/LoginTermsFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/terms/LoginTermsFragment.kt index 83d68f5f31..4d6e4e3ec9 100755 --- a/vector/src/main/java/im/vector/riotx/features/login/terms/LoginTermsFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/terms/LoginTermsFragment.kt @@ -26,6 +26,7 @@ import im.vector.riotx.R import im.vector.riotx.core.error.ErrorFormatter import im.vector.riotx.core.extensions.cleanup import im.vector.riotx.core.extensions.configureWith +import im.vector.riotx.core.extensions.toReducedUrl import im.vector.riotx.core.utils.openUrlInExternalBrowser import im.vector.riotx.features.login.AbstractLoginFragment import im.vector.riotx.features.login.LoginAction @@ -115,7 +116,7 @@ class LoginTermsFragment @Inject constructor( } override fun updateWithState(state: LoginViewState) { - policyController.homeServer = state.homeServerUrlSimple + policyController.homeServer = state.homeServerUrl.toReducedUrl() renderState() }