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