From 2a68c8d08be9233b6625ce37f75a9ae6b0e74ed2 Mon Sep 17 00:00:00 2001
From: Benoit Marty <benoitm@matrix.org>
Date: Mon, 6 Jul 2020 10:49:03 +0200
Subject: [PATCH] Simplify the server selection screen: remove the "Continue"
 button

---
 .../riotx/features/login/LoginActivity.kt     |  1 +
 .../riotx/features/login/LoginFragment.kt     |  1 +
 .../login/LoginServerSelectionFragment.kt     | 38 ++-----------------
 .../LoginSignUpSignInSelectionFragment.kt     |  1 +
 .../riotx/features/login/LoginViewModel.kt    | 15 +++++++-
 .../riotx/features/login/LoginViewState.kt    |  2 +-
 .../vector/riotx/features/login/ServerType.kt |  1 +
 .../res/drawable/bg_login_server_selector.xml |  1 +
 .../fragment_login_server_selection.xml       | 24 +++---------
 9 files changed, 29 insertions(+), 55 deletions(-)

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 86be00702c..1eebf532e7 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
@@ -235,6 +235,7 @@ open class LoginActivity : VectorBaseActivity(), ToolbarConfigurable {
             ServerType.Other     -> addFragmentToBackstack(R.id.loginFragmentContainer,
                     LoginServerUrlFormFragment::class.java,
                     option = commonOption)
+            ServerType.Unknown   -> Unit /* Should not happen */
         }
     }
 
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 28c2299faa..ef8281fa89 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
@@ -166,6 +166,7 @@ class LoginFragment @Inject constructor() : AbstractLoginFragment() {
                     loginTitle.text = getString(resId, state.homeServerUrl.toReducedUrl())
                     loginNotice.text = getString(R.string.login_server_other_text)
                 }
+                ServerType.Unknown   -> Unit /* Should not happen */
             }
             loginPasswordNotice.isVisible = false
         }
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 79c5c7d024..3a1bba7f11 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
@@ -19,7 +19,6 @@ package im.vector.riotx.features.login
 import android.os.Bundle
 import android.view.View
 import butterknife.OnClick
-import com.airbnb.mvrx.withState
 import im.vector.riotx.R
 import im.vector.riotx.core.utils.openUrlInChromeCustomTab
 import kotlinx.android.synthetic.main.fragment_login_server_selection.*
@@ -40,11 +39,7 @@ class LoginServerSelectionFragment @Inject constructor() : AbstractLoginFragment
     }
 
     private fun updateSelectedChoice(state: LoginViewState) {
-        state.serverType.let {
-            loginServerChoiceMatrixOrg.isChecked = it == ServerType.MatrixOrg
-            loginServerChoiceModular.isChecked = it == ServerType.Modular
-            loginServerChoiceOther.isChecked = it == ServerType.Other
-        }
+        loginServerChoiceMatrixOrg.isChecked = state.serverType == ServerType.MatrixOrg
     }
 
     private fun initTextViews() {
@@ -61,42 +56,17 @@ class LoginServerSelectionFragment @Inject constructor() : AbstractLoginFragment
 
     @OnClick(R.id.loginServerChoiceMatrixOrg)
     fun selectMatrixOrg() {
-        if (loginServerChoiceMatrixOrg.isChecked) {
-            // Consider this is a submit
-            submit()
-        } else {
-            loginViewModel.handle(LoginAction.UpdateServerType(ServerType.MatrixOrg))
-        }
+        loginViewModel.handle(LoginAction.UpdateServerType(ServerType.MatrixOrg))
     }
 
     @OnClick(R.id.loginServerChoiceModular)
     fun selectModular() {
-        if (loginServerChoiceModular.isChecked) {
-            // Consider this is a submit
-            submit()
-        } else {
-            loginViewModel.handle(LoginAction.UpdateServerType(ServerType.Modular))
-        }
+        loginViewModel.handle(LoginAction.UpdateServerType(ServerType.Modular))
     }
 
     @OnClick(R.id.loginServerChoiceOther)
     fun selectOther() {
-        if (loginServerChoiceOther.isChecked) {
-            // Consider this is a submit
-            submit()
-        } else {
-            loginViewModel.handle(LoginAction.UpdateServerType(ServerType.Other))
-        }
-    }
-
-    @OnClick(R.id.loginServerSubmit)
-    fun submit() = withState(loginViewModel) { state ->
-        if (state.serverType == ServerType.MatrixOrg) {
-            // Request login flow here
-            loginViewModel.handle(LoginAction.UpdateHomeServer(getString(R.string.matrix_org_server_url)))
-        } else {
-            loginViewModel.handle(LoginAction.PostViewEvent(LoginViewEvents.OnServerSelectionDone))
-        }
+        loginViewModel.handle(LoginAction.UpdateServerType(ServerType.Other))
     }
 
     @OnClick(R.id.loginServerIKnowMyIdSubmit)
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 427ad99b41..6ac5993f30 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
@@ -49,6 +49,7 @@ open class LoginSignUpSignInSelectionFragment @Inject constructor() : AbstractLo
                 loginSignupSigninTitle.text = getString(R.string.login_server_other_title)
                 loginSignupSigninText.text = getString(R.string.login_connect_to, state.homeServerUrl.toReducedUrl())
             }
+            ServerType.Unknown   -> Unit /* Should not happen */
         }
     }
 
diff --git a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt
index fc970297d1..47736ec047 100644
--- a/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt
+++ b/vector/src/main/java/im/vector/riotx/features/login/LoginViewModel.kt
@@ -321,7 +321,7 @@ class LoginViewModel @AssistedInject constructor(
             LoginAction.ResetHomeServerType -> {
                 setState {
                     copy(
-                            serverType = ServerType.MatrixOrg
+                            serverType = ServerType.Unknown
                     )
                 }
             }
@@ -390,6 +390,15 @@ class LoginViewModel @AssistedInject constructor(
                     serverType = action.serverType
             )
         }
+
+        when (action.serverType) {
+            ServerType.Unknown   -> Unit /* Should not happen */
+            ServerType.MatrixOrg ->
+                // Request login flow here
+                handle(LoginAction.UpdateHomeServer(stringProvider.getString(R.string.matrix_org_server_url)))
+            ServerType.Modular,
+            ServerType.Other     -> _viewEvents.post(LoginViewEvents.OnServerSelectionDone)
+        }.exhaustive
     }
 
     private fun handleInitWith(action: LoginAction.InitWith) {
@@ -682,7 +691,9 @@ class LoginViewModel @AssistedInject constructor(
                 _viewEvents.post(LoginViewEvents.Failure(failure))
                 setState {
                     copy(
-                            asyncHomeServerLoginFlowRequest = Uninitialized
+                            asyncHomeServerLoginFlowRequest = Uninitialized,
+                            // If we were trying to retrieve matrix.org login flow, also reset the serverType
+                            serverType = if (serverType == ServerType.MatrixOrg) ServerType.Unknown else serverType
                     )
                 }
             }
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 944d1f7d82..655966ce25 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
@@ -35,7 +35,7 @@ data class LoginViewState(
 
         // User choices
         @PersistState
-        val serverType: ServerType = ServerType.MatrixOrg,
+        val serverType: ServerType = ServerType.Unknown,
         @PersistState
         val signMode: SignMode = SignMode.Unknown,
         @PersistState
diff --git a/vector/src/main/java/im/vector/riotx/features/login/ServerType.kt b/vector/src/main/java/im/vector/riotx/features/login/ServerType.kt
index 4c7007c137..50dfee19f0 100644
--- a/vector/src/main/java/im/vector/riotx/features/login/ServerType.kt
+++ b/vector/src/main/java/im/vector/riotx/features/login/ServerType.kt
@@ -17,6 +17,7 @@
 package im.vector.riotx.features.login
 
 enum class ServerType {
+    Unknown,
     MatrixOrg,
     Modular,
     Other
diff --git a/vector/src/main/res/drawable/bg_login_server_selector.xml b/vector/src/main/res/drawable/bg_login_server_selector.xml
index 57be1e5d54..3fcc4e006a 100644
--- a/vector/src/main/res/drawable/bg_login_server_selector.xml
+++ b/vector/src/main/res/drawable/bg_login_server_selector.xml
@@ -2,6 +2,7 @@
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 
     <item android:drawable="@drawable/bg_login_server_checked" android:state_checked="true" />
+    <item android:drawable="@drawable/bg_login_server_checked" android:state_pressed="true" />
 
     <item android:drawable="@drawable/bg_login_server" />
 
diff --git a/vector/src/main/res/layout/fragment_login_server_selection.xml b/vector/src/main/res/layout/fragment_login_server_selection.xml
index 16465486a2..de7bf65cbb 100644
--- a/vector/src/main/res/layout/fragment_login_server_selection.xml
+++ b/vector/src/main/res/layout/fragment_login_server_selection.xml
@@ -43,6 +43,7 @@
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toBottomOf="@+id/loginServerTitle" />
 
+            <!-- Use a CheckableConstraintLayout to keep the pressed state when retrieving login flow -->
             <im.vector.riotx.core.platform.CheckableConstraintLayout
                 android:id="@+id/loginServerChoiceMatrixOrg"
                 android:layout_width="match_parent"
@@ -84,7 +85,7 @@
 
             </im.vector.riotx.core.platform.CheckableConstraintLayout>
 
-            <im.vector.riotx.core.platform.CheckableConstraintLayout
+            <androidx.constraintlayout.widget.ConstraintLayout
                 android:id="@+id/loginServerChoiceModular"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
@@ -135,9 +136,9 @@
                     app:layout_constraintEnd_toEndOf="parent"
                     app:layout_constraintTop_toTopOf="@+id/loginServerChoiceModularText" />
 
-            </im.vector.riotx.core.platform.CheckableConstraintLayout>
+            </androidx.constraintlayout.widget.ConstraintLayout>
 
-            <im.vector.riotx.core.platform.CheckableConstraintLayout
+            <androidx.constraintlayout.widget.ConstraintLayout
                 android:id="@+id/loginServerChoiceOther"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
@@ -178,20 +179,7 @@
                     app:layout_constraintStart_toStartOf="parent"
                     app:layout_constraintTop_toBottomOf="@+id/loginServerChoiceOtherTitle" />
 
-            </im.vector.riotx.core.platform.CheckableConstraintLayout>
-
-            <com.google.android.material.button.MaterialButton
-                android:id="@+id/loginServerSubmit"
-                style="@style/Style.Vector.Login.Button"
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_marginTop="24dp"
-                android:text="@string/login_continue"
-                android:transitionName="loginSubmitTransition"
-                app:layout_constraintBottom_toTopOf="@+id/loginServerIKnowMyIdSubmit"
-                app:layout_constraintEnd_toEndOf="parent"
-                app:layout_constraintStart_toStartOf="parent"
-                app:layout_constraintTop_toBottomOf="@+id/loginServerChoiceOther" />
+            </androidx.constraintlayout.widget.ConstraintLayout>
 
             <com.google.android.material.button.MaterialButton
                 android:id="@+id/loginServerIKnowMyIdSubmit"
@@ -204,7 +192,7 @@
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintStart_toStartOf="parent"
-                app:layout_constraintTop_toBottomOf="@+id/loginServerSubmit" />
+                app:layout_constraintTop_toBottomOf="@+id/loginServerChoiceOther" />
 
         </androidx.constraintlayout.widget.ConstraintLayout>