Commit 95fa53f2 authored by 赵鹏翔's avatar 赵鹏翔

业务逻辑调整

parent 1f94afb6
......@@ -41,30 +41,24 @@ class LoginActivity : AppCompatActivity() {
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
val username = binding.username
val password = binding.password
val loading = binding.loading
val login = binding.login
val tvErrInfo = binding.tvErrInfo
loginViewModel = ViewModelProvider(this)
.get(LoginViewModel::class.java)
loginViewModel.loginLiveData.observe(this) { result ->
loading.visibility = View.GONE
binding.vLoading.visibility = View.GONE
result.getOrElse {
tvErrInfo.text = it.message
tvErrInfo.visibility = View.VISIBLE
binding.tvErrorInfo.text = it.message
binding.tvErrorInfo.visibility = View.VISIBLE
return@observe
}
result.getOrNull().also {
it?.let { it1 ->
val userName = username.text.toString()
val passWord = password.text.toString()
val userName = binding.etUsername.text.toString()
val passWord = binding.etPassword.text.toString()
LocalKeyDataMKManageKit.putLoginIngfo(userName, passWord)
AccountService.init(it1, userName)
......@@ -80,75 +74,68 @@ class LoginActivity : AppCompatActivity() {
loginViewModel.loginFormState.observe(this@LoginActivity, Observer {
val loginState = it ?: return@Observer
login.isEnabled = loginState.isDataValid
binding.btnLogin.isEnabled = loginState.isDataValid
if (loginState.usernameError != null) {
username.error = getString(loginState.usernameError)
binding.etUsername.error = getString(loginState.usernameError)
}
if (loginState.passwordError != null && !TextUtils.isEmpty(password.text)) {
password.error = getString(loginState.passwordError)
if (loginState.passwordError != null && !TextUtils.isEmpty(binding.etPassword.text)) {
binding.etPassword.error = getString(loginState.passwordError)
}
})
username.apply {
binding.etUsername.apply {
afterTextChanged {
loginViewModel.loginDataChanged(
username.text.toString(),
password.text.toString()
binding.etUsername.text.toString(),
binding.etPassword.text.toString()
)
}
setOnEditorActionListener { _, actionId, _ ->
when (actionId) {
EditorInfo.IME_ACTION_NEXT -> {
password.requestFocus()
password.text?.let { password.setSelection(it.length) };
binding.etPassword.requestFocus()
binding.etPassword.text?.let { binding.etPassword.setSelection(it.length) };
}
}
false
}
}
password.apply {
binding.etPassword.apply {
afterTextChanged {
loginViewModel.loginDataChanged(
username.text.toString(),
password.text.toString()
binding.etUsername.text.toString(),
binding.etPassword.text.toString()
)
}
setOnEditorActionListener { _, actionId, _ ->
when (actionId) {
EditorInfo.IME_ACTION_DONE -> {
loading.visibility = View.VISIBLE;
loginViewModel.login(
username.text.toString(),
password.text.toString()
)
if (!TextUtils.isEmpty(binding.etUsername.text.toString())){
checkPermissions()
}
}
}
false
}
binding.login.clickWithTrigger {
binding.btnLogin.clickWithTrigger {
checkPermissions()
}
}
LocalKeyDataMKManageKit.getLoginUserName()?.let {
username.setText(it)
binding.etUsername.setText(it)
}
LocalKeyDataMKManageKit.getLoginUserPassword()?.let {
password.setText(it)
binding.etPassword.setText(it)
}
if (!TextUtils.isEmpty(username.text) && !TextUtils.isEmpty(password.text)) {
loading.visibility = View.VISIBLE;
loginViewModel.login(
username.text.toString(),
password.text.toString()
)
if (!TextUtils.isEmpty(binding.etUsername.text) && !TextUtils.isEmpty(binding.etPassword.text)) {
checkPermissions()
}
}
private fun checkPermissions() {
......@@ -157,23 +144,28 @@ class LoginActivity : AppCompatActivity() {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
.request(RequestCallback { allGranted: Boolean, grantedList: List<String?>?, deniedList: List<String?>? ->
.request { allGranted: Boolean, grantedList: List<String?>?, deniedList: List<String?>? ->
if (allGranted) {
binding.loading.visibility = View.VISIBLE
loginViewModel.login(
binding.username.text.toString(),
binding.password.text.toString()
)
login()
} else {
ToastUtils.showShort("请检查权限后重试")
}
})
}
}
private fun login(){
binding.vLoading.visibility = View.VISIBLE
loginViewModel.login(
binding.etUsername.text.toString(),
binding.etPassword.text.toString()
)
}
/**
* Extension function to simplify setting an afterTextChanged action to EditText components.
*/
private fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(editable: Editable?) {
afterTextChanged.invoke(editable.toString())
......
......@@ -8,7 +8,7 @@
tools:context=".ui.LoginActivity">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/username"
android:id="@+id/et_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
......@@ -25,7 +25,7 @@
app:met_bottomTextSize="26sp" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/password"
android:id="@+id/et_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="46dp"
......@@ -39,11 +39,11 @@
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/username"
app:layout_constraintTop_toBottomOf="@+id/et_username"
app:met_bottomTextSize="26sp" />
<Button
android:id="@+id/login"
android:id="@+id/btn_login"
android:layout_width="656dp"
android:layout_height="100dp"
android:layout_gravity="start"
......@@ -56,7 +56,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintTop_toBottomOf="@+id/et_password"
app:layout_constraintVertical_bias="0.2" />
<ImageView
......@@ -94,7 +94,7 @@
app:layout_constraintStart_toEndOf="@+id/ivLogo" />
<TextView
android:id="@+id/tvErrInfo"
android:id="@+id/tv_error_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号或密码输入错误,请重新输入"
......@@ -102,13 +102,13 @@
android:textSize="24sp"
android:visibility="gone"
app:layout_constraintStart_toEndOf="@+id/ivLogo"
app:layout_constraintStart_toStartOf="@+id/username"
app:layout_constraintTop_toBottomOf="@+id/password" />
app:layout_constraintStart_toStartOf="@+id/et_username"
app:layout_constraintTop_toBottomOf="@+id/et_password" />
<!--封装成一个View-->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/loading"
android:id="@+id/v_loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
......@@ -148,6 +148,7 @@
app:SpinKit_Color="@color/white" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment