Login screens: Fix navigation issue

This commit is contained in:
Benoit Marty 2019-11-14 22:16:50 +01:00
parent adf299081d
commit 5b9876a20c
7 changed files with 52 additions and 23 deletions

View File

@ -21,6 +21,7 @@ import im.vector.matrix.android.api.failure.MatrixError
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.resources.StringProvider import im.vector.riotx.core.resources.StringProvider
import java.net.SocketTimeoutException import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.inject.Inject import javax.inject.Inject
class ErrorFormatter @Inject constructor(private val stringProvider: StringProvider) { class ErrorFormatter @Inject constructor(private val stringProvider: StringProvider) {
@ -36,6 +37,9 @@ class ErrorFormatter @Inject constructor(private val stringProvider: StringProvi
is Failure.NetworkConnection -> { is Failure.NetworkConnection -> {
if (throwable.ioException is SocketTimeoutException) { if (throwable.ioException is SocketTimeoutException) {
stringProvider.getString(R.string.error_network_timeout) stringProvider.getString(R.string.error_network_timeout)
} else if (throwable.ioException is UnknownHostException) {
// Invalid homeserver?
stringProvider.getString(R.string.login_error_unknown_host)
} else { } else {
stringProvider.getString(R.string.error_no_network) stringProvider.getString(R.string.error_no_network)
} }

View File

@ -21,7 +21,6 @@ import android.content.Intent
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import com.airbnb.mvrx.Success
import com.airbnb.mvrx.viewModel import com.airbnb.mvrx.viewModel
import com.airbnb.mvrx.withState import com.airbnb.mvrx.withState
import im.vector.riotx.R import im.vector.riotx.R
@ -69,7 +68,7 @@ class LoginActivity : VectorBaseActivity() {
is LoginNavigation.OpenServerSelection -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginServerSelectionFragment::class.java) is LoginNavigation.OpenServerSelection -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginServerSelectionFragment::class.java)
is LoginNavigation.OnServerSelectionDone -> onServerSelectionDone() is LoginNavigation.OnServerSelectionDone -> onServerSelectionDone()
is LoginNavigation.OnSignModeSelected -> onSignModeSelected(it) is LoginNavigation.OnSignModeSelected -> onSignModeSelected(it)
is LoginNavigation.OnLoginFlowRetrieved -> onLoginFlowRetrieved(it) is LoginNavigation.OnLoginFlowRetrieved -> onLoginFlowRetrieved()
is LoginNavigation.OnSsoLoginFallbackError -> onSsoLoginFallbackError(it) is LoginNavigation.OnSsoLoginFallbackError -> onSsoLoginFallbackError(it)
} }
} }
@ -82,16 +81,12 @@ class LoginActivity : VectorBaseActivity() {
.disposeOnDestroy() .disposeOnDestroy()
} }
private fun onLoginFlowRetrieved(onLoginFlowRetrieved: LoginNavigation.OnLoginFlowRetrieved) { private fun onLoginFlowRetrieved() {
when (onLoginFlowRetrieved.loginMode) { addFragmentToBackstack(R.id.loginFragmentContainer, LoginSignUpSignInSelectionFragment::class.java)
LoginMode.Sso -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginSsoFallbackFragment::class.java)
LoginMode.Unsupported,
LoginMode.Password -> addFragmentToBackstack(R.id.loginFragmentContainer, LoginSignUpSignInSelectionFragment::class.java)
}
} }
private fun updateWithState(loginViewState: LoginViewState) { private fun updateWithState(loginViewState: LoginViewState) {
if (loginViewState.asyncLoginAction is Success) { if (loginViewState.isUserLogged()) {
val intent = HomeActivity.newIntent(this) val intent = HomeActivity.newIntent(this)
startActivity(intent) startActivity(intent)
finish() finish()
@ -123,11 +118,21 @@ class LoginActivity : VectorBaseActivity() {
} }
private fun onSignModeSelected(mode: LoginNavigation.OnSignModeSelected) { 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) { when (mode.signMode) {
SignMode.Unknown -> error("Sign mode has to be set before calling this method") 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.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
}
}
}
} }
} }

View File

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

View File

@ -22,7 +22,7 @@ import im.vector.riotx.core.platform.VectorSharedAction
sealed class LoginNavigation : VectorSharedAction { sealed class LoginNavigation : VectorSharedAction {
object OpenServerSelection : LoginNavigation() object OpenServerSelection : LoginNavigation()
object OnServerSelectionDone : LoginNavigation() object OnServerSelectionDone : LoginNavigation()
data class OnLoginFlowRetrieved(val loginMode: LoginMode) : LoginNavigation() object OnLoginFlowRetrieved : LoginNavigation()
data class OnSignModeSelected(val signMode: SignMode) : LoginNavigation() data class OnSignModeSelected(val signMode: SignMode) : LoginNavigation()
//object OpenSsoLoginFallback : LoginNavigation() //object OpenSsoLoginFallback : LoginNavigation()
data class OnSsoLoginFallbackError(val errorCode: Int, val description: String, val failingUrl: String) : LoginNavigation() data class OnSsoLoginFallbackError(val errorCode: Int, val description: String, val failingUrl: String) : LoginNavigation()

View File

@ -112,7 +112,7 @@ class LoginServerSelectionFragment @Inject constructor() : AbstractLoginFragment
} }
is Success -> { is Success -> {
// The home server url is valid // The home server url is valid
loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved(it.asyncHomeServerLoginFlowRequest.invoke())) loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved)
} }
} }
} }

View File

@ -123,7 +123,7 @@ class LoginServerUrlFormFragment @Inject constructor(
} }
is Success -> { is Success -> {
// The home server url is valid // The home server url is valid
loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved(state.asyncHomeServerLoginFlowRequest.invoke())) loginSharedActionViewModel.post(LoginNavigation.OnLoginFlowRetrieved)
} }
} }
} }

View File

@ -16,10 +16,7 @@
package im.vector.riotx.features.login package im.vector.riotx.features.login
import com.airbnb.mvrx.Async import com.airbnb.mvrx.*
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.MvRxState
import com.airbnb.mvrx.Uninitialized
data class LoginViewState( data class LoginViewState(
val serverType: ServerType = ServerType.MatrixOrg, val serverType: ServerType = ServerType.MatrixOrg,
@ -33,10 +30,10 @@ data class LoginViewState(
return asyncLoginAction is Loading return asyncLoginAction is Loading
|| asyncHomeServerLoginFlowRequest is Loading || asyncHomeServerLoginFlowRequest is Loading
} }
fun isUserLogged(): Boolean {
// TODO Add other async here
return asyncLoginAction is Success
}
} }
enum class LoginMode {
Password,
Sso,
Unsupported
}