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

业务逻辑调整

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