xiongzhu 1 anno fa
parent
commit
5f453a7618

+ 5 - 5
app/src/main/java/com/example/modifier/model/ServerConfig.kt

@@ -1,9 +1,9 @@
 package com.example.modifier.model
 
 data class ServerConfig(
-    val url: String? = "https://rcs.izouma.com",
-    val deviceLabel: String? = "Android",
-    val username: String? = "",
-    val password: String? = "",
-    val token: String? = ""
+    var url: String? = "https://rcs.izouma.com",
+    var deviceLabel: String? = "Android",
+    var username: String? = "",
+    var password: String? = "",
+    var token: String? = ""
 )

+ 30 - 25
app/src/main/java/com/example/modifier/ui/LoginActivity.kt

@@ -7,6 +7,9 @@ import androidx.appcompat.app.AppCompatActivity
 import androidx.core.view.ViewCompat
 import androidx.core.view.WindowInsetsCompat
 import androidx.databinding.DataBindingUtil
+import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.lifecycleScope
+import androidx.lifecycle.repeatOnLifecycle
 import androidx.lifecycle.viewModelScope
 import com.example.modifier.Global
 import com.example.modifier.R
@@ -35,35 +38,37 @@ class LoginActivity : AppCompatActivity() {
 
         binding.etServer.setSimpleItems(Global.servers.toTypedArray())
         binding.btnLogin.setOnClickListener { v ->
-            val server = binding.etDeviceLabel.text?.toString()
-            val deviceLabel = binding.etDeviceLabel.text?.toString()
-            val username = binding.etUsername.text?.toString()
-            val password = binding.etPassword.text?.toString()
-            if (StringUtils.isBlank(server)) {
-                binding.tlServer.error = "Server is required"
+            loginViewModel.serverConfig.value!!.url = binding.etServer.text.toString()
+            loginViewModel.serverConfig.value!!.deviceLabel = binding.etDeviceLabel.text.toString()
+            loginViewModel.serverConfig.value!!.username = binding.etUsername.text.toString()
+            loginViewModel.serverConfig.value!!.password = binding.etPassword.text.toString()
+            loginViewModel.viewModelScope.launch {
+                loginViewModel.login()
             }
-            if (StringUtils.isBlank(deviceLabel)) {
-                binding.tlName.error = "Device label is required"
-            }
-            if (StringUtils.isBlank(username)) {
-                binding.tlUsername.error = "Username is required"
-            }
-            if (StringUtils.isBlank(password)) {
-                binding.tlPassword.error = "Password is required"
-            }
-            if (StringUtils.isNotBlank(server)
-                && StringUtils.isNotBlank(deviceLabel)
-                && StringUtils.isNotBlank(username)
-                && StringUtils.isNotBlank(password)
-            ) {
-                loginViewModel.viewModelScope.launch {
-                    loginViewModel.login()
+        }
+        loginViewModel.serverConfig.postValue(ServerConfig())
+        lifecycleScope.launch {
+            // repeatOnLifecycle launches the block in a new coroutine every time the
+            // lifecycle is in the STARTED state (or above) and cancels it when it's STOPPED.
+            repeatOnLifecycle(Lifecycle.State.STARTED) {
+                // Trigger the flow and start listening for values.
+                // Note that this happens when lifecycle is STARTED and stops
+                // collecting when the lifecycle is STOPPED
+                loginViewModel.uiState.collect { uiState ->
+                    // New value received
+                    when (uiState) {
+                        is LoginUiState.Normal -> {}
+                        is LoginUiState.Loading -> Utils.makeLoadingButton(
+                            this@LoginActivity,
+                            binding.btnLogin
+                        )
 
-                }
+                        is LoginUiState.Success -> {}
+                        is LoginUiState.Error -> {}
 
+                    }
+                }
             }
         }
-        loginViewModel.serverConfig.postValue(ServerConfig())
-        loginViewModel.uiState.launchIn(loginViewModel.viewModelScope)
     }
 }

+ 11 - 8
app/src/main/java/com/example/modifier/ui/LoginUiState.kt

@@ -1,10 +1,13 @@
 package com.example.modifier.ui
 
-data class LoginUiState(
-    val isLoading: Boolean = false,
-    val isUserLoggedIn: Boolean = false,
-    val serverError: String? = null,
-    val usernameError: String? = null,
-    val passwordError: String? = null,
-    val deviceLabelError: String? = null
-)
+sealed class LoginUiState {
+    data object Normal : LoginUiState()
+    data object Loading : LoginUiState()
+    data object Success : LoginUiState()
+    data class Error(
+        val serverError: String?,
+        val usernameError: String?,
+        val passwordError: String?,
+        val deviceLabelError: String?
+    ) : LoginUiState()
+}

+ 2 - 2
app/src/main/java/com/example/modifier/ui/LoginViewModel.kt

@@ -47,7 +47,7 @@ class LoginViewModel @Inject constructor() : ViewModel() {
         val TOKEN = stringPreferencesKey("token")
     }
 
-    private val _uiState = MutableStateFlow(LoginUiState())
+    private val _uiState = MutableStateFlow<LoginUiState>(LoginUiState.Normal)
     val uiState: StateFlow<LoginUiState> = _uiState.asStateFlow()
     val serverConfig = MutableLiveData<ServerConfig>()
 
@@ -81,7 +81,7 @@ class LoginViewModel @Inject constructor() : ViewModel() {
 
     suspend fun login() {
         Log.d(TAG, "login")
-        _uiState.emit(LoginUiState(isLoading = true))
+        _uiState.value = LoginUiState.Loading
         val client = HttpClient(OkHttp) {
             defaultRequest {
                 url(serverConfig.value!!.url!!)