From 5b9876a20cc3f12c363e3603eccf41997023bd16 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 14 Nov 2019 22:16:50 +0100 Subject: [PATCH] Login screens: Fix navigation issue --- .../vector/riotx/core/error/ErrorFormatter.kt | 4 +++ .../riotx/features/login/LoginActivity.kt | 27 +++++++++++-------- .../vector/riotx/features/login/LoginMode.kt | 23 ++++++++++++++++ .../riotx/features/login/LoginNavigation.kt | 2 +- .../login/LoginServerSelectionFragment.kt | 2 +- .../login/LoginServerUrlFormFragment.kt | 2 +- .../riotx/features/login/LoginViewState.kt | 15 +++++------ 7 files changed, 52 insertions(+), 23 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/features/login/LoginMode.kt diff --git a/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt b/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt index d08675ea1b..65f48f5e7b 100644 --- a/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt +++ b/vector/src/main/java/im/vector/riotx/core/error/ErrorFormatter.kt @@ -21,6 +21,7 @@ import im.vector.matrix.android.api.failure.MatrixError import im.vector.riotx.R import im.vector.riotx.core.resources.StringProvider import java.net.SocketTimeoutException +import java.net.UnknownHostException import javax.inject.Inject class ErrorFormatter @Inject constructor(private val stringProvider: StringProvider) { @@ -36,6 +37,9 @@ class ErrorFormatter @Inject constructor(private val stringProvider: StringProvi is Failure.NetworkConnection -> { if (throwable.ioException is SocketTimeoutException) { stringProvider.getString(R.string.error_network_timeout) + } else if (throwable.ioException is UnknownHostException) { + // Invalid homeserver? + stringProvider.getString(R.string.login_error_unknown_host) } else { stringProvider.getString(R.string.error_no_network) } 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 0ab10134e9..0eaaea33d4 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 @@ -21,7 +21,6 @@ import android.content.Intent import androidx.appcompat.app.AlertDialog import androidx.core.view.isVisible import androidx.fragment.app.FragmentManager -import com.airbnb.mvrx.Success import com.airbnb.mvrx.viewModel import com.airbnb.mvrx.withState import im.vector.riotx.R @@ -69,7 +68,7 @@ class LoginActivity : VectorBaseActivity() { is LoginNavigation.OpenServerSelection -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginServerSelectionFragment::class.java) is LoginNavigation.OnServerSelectionDone -> onServerSelectionDone() is LoginNavigation.OnSignModeSelected -> onSignModeSelected(it) - is LoginNavigation.OnLoginFlowRetrieved -> onLoginFlowRetrieved(it) + is LoginNavigation.OnLoginFlowRetrieved -> onLoginFlowRetrieved() is LoginNavigation.OnSsoLoginFallbackError -> onSsoLoginFallbackError(it) } } @@ -82,16 +81,12 @@ class LoginActivity : VectorBaseActivity() { .disposeOnDestroy() } - private fun onLoginFlowRetrieved(onLoginFlowRetrieved: LoginNavigation.OnLoginFlowRetrieved) { - when (onLoginFlowRetrieved.loginMode) { - LoginMode.Sso -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginSsoFallbackFragment::class.java) - LoginMode.Unsupported, - LoginMode.Password -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginSignUpSignInSelectionFragment::class.java) - } + private fun onLoginFlowRetrieved() { + addFragmentToBackstack(R.id.loginFragmentContainer, LoginSignUpSignInSelectionFragment::class.java) } private fun updateWithState(loginViewState: LoginViewState) { - if (loginViewState.asyncLoginAction is Success) { + if (loginViewState.isUserLogged()) { val intent = HomeActivity.newIntent(this) startActivity(intent) finish() @@ -123,11 +118,21 @@ class LoginActivity : VectorBaseActivity() { } private fun onSignModeSelected(mode: LoginNavigation.OnSignModeSelected) { - // We cannot use the state, it is not ready... + // We cannot use the state to get the SignMode, it is not ready... when (mode.signMode) { SignMode.Unknown -> error("Sign mode has to be set before calling this method") SignMode.SignUp -> Unit // TODO addFragmentToBackstack(R.id.loginFragmentContainer, SignUpFragment::class.java) - SignMode.SignIn -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginFragment::class.java) + SignMode.SignIn -> { + // It depends on the LoginMode + withState(loginViewModel) { + when (it.asyncHomeServerLoginFlowRequest.invoke()) { + null -> error("Developer error") + LoginMode.Password -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginFragment::class.java) + LoginMode.Sso -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginSsoFallbackFragment::class.java) + LoginMode.Unsupported -> TODO() // TODO Import Fallback login fragment from Riot-Android + } + } + } } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginMode.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginMode.kt new file mode 100644 index 0000000000..ae40d3a95a --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginMode.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2019 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.riotx.features.login + +enum class LoginMode { + Password, + Sso, + Unsupported +} diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginNavigation.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginNavigation.kt index ba1e327e3a..a223660708 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginNavigation.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginNavigation.kt @@ -22,7 +22,7 @@ import im.vector.riotx.core.platform.VectorSharedAction sealed class LoginNavigation : VectorSharedAction { object OpenServerSelection : LoginNavigation() object OnServerSelectionDone : LoginNavigation() - data class OnLoginFlowRetrieved(val loginMode: LoginMode) : LoginNavigation() + object OnLoginFlowRetrieved : LoginNavigation() data class OnSignModeSelected(val signMode: SignMode) : LoginNavigation() //object OpenSsoLoginFallback : LoginNavigation() data class OnSsoLoginFallbackError(val errorCode: Int, val description: String, val failingUrl: String) : LoginNavigation() diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginServerSelectionFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginServerSelectionFragment.kt index fb0ec25ab0..27ae99c20b 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginServerSelectionFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginServerSelectionFragment.kt @@ -112,7 +112,7 @@ class LoginServerSelectionFragment @Inject constructor() : AbstractLoginFragment } is Success -> { // The home server url is valid - loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved(it.asyncHomeServerLoginFlowRequest.invoke())) + loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved) } } } diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginServerUrlFormFragment.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginServerUrlFormFragment.kt index 2ea5869448..b5f002b353 100644 --- a/vector/src/main/java/im/vector/riotx/features/login/LoginServerUrlFormFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/login/LoginServerUrlFormFragment.kt @@ -123,7 +123,7 @@ class LoginServerUrlFormFragment @Inject constructor( } is Success -> { // The home server url is valid - loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved(state.asyncHomeServerLoginFlowRequest.invoke())) + loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved) } } } 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 0d1b592611..4be96d20c2 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 @@ -16,10 +16,7 @@ package im.vector.riotx.features.login -import com.airbnb.mvrx.Async -import com.airbnb.mvrx.Loading -import com.airbnb.mvrx.MvRxState -import com.airbnb.mvrx.Uninitialized +import com.airbnb.mvrx.* data class LoginViewState( val serverType: ServerType = ServerType.MatrixOrg, @@ -33,10 +30,10 @@ data class LoginViewState( return asyncLoginAction is Loading || asyncHomeServerLoginFlowRequest is Loading } + + fun isUserLogged(): Boolean { + // TODO Add other async here + return asyncLoginAction is Success + } } -enum class LoginMode { - Password, - Sso, - Unsupported -}