Commit 987760e1 by TaylorZhang

feat(项目):ViewModel中不需要关心界面的生命周期

parent 601c8960
......@@ -29,7 +29,7 @@ class MainActivity : BaseLifecycleActivity<MainViewModel>() {
// TODO: 2020/6/24 如果防止用户短时间多次点击,使用onClick,如果用户可以多次点击,还是使用原生的setOnClickListener
btnLogin.onClick {
// 登录
mViewModel.login(this, etPhone.text.toString(), etPwd.text.toString())
mViewModel.login(etPhone.text.toString(), etPwd.text.toString())
}
}
......
package com.autocareai.mvvmdemo.demo.main
import androidx.lifecycle.LifecycleOwner
import com.autocareai.lib.lifecycle.extension.post
import com.autocareai.lib.lifecycle.livedata.SingleLiveEvent
import com.autocareai.mvvmdemo.R
......@@ -26,11 +25,11 @@ class MainViewModel : BaseViewModel() {
/**
* 登录
*/
fun login(owner: LifecycleOwner, phone: String, password: String) {
checkInput(owner, phone, password)
fun login(phone: String, password: String) {
checkInput(phone, password)
}
private fun checkInput(owner: LifecycleOwner, phone: String, password: String) {
private fun checkInput(phone: String, password: String) {
// TODO: 2020/6/24 检查用户输入是否合规的逻辑也应该放在ViewModel中
if (phone.isEmpty()) {
shortToast(R.string.main_please_input_phone)
......@@ -42,12 +41,11 @@ class MainViewModel : BaseViewModel() {
return
}
realLogin(owner, phone, password)
realLogin(phone, password)
}
private fun realLogin(owner: LifecycleOwner, phone: String, password: String) {
private fun realLogin(phone: String, password: String) {
DemoApi.login(phone, password)
.attachLifecycle(owner)
.onSuccess {
shortToast(R.string.main_login_success)
toListEvent.post(it)
......
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