Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mvvmdemo
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LouisWang
mvvmdemo
Commits
987760e1
Commit
987760e1
authored
Jun 29, 2020
by
TaylorZhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(项目):ViewModel中不需要关心界面的生命周期
parent
601c8960
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
8 deletions
+6
-8
mvvm-demo/src/main/java/com/autocareai/mvvmdemo/demo/main/MainActivity.kt
+1
-1
mvvm-demo/src/main/java/com/autocareai/mvvmdemo/demo/main/MainViewModel.kt
+5
-7
No files found.
mvvm-demo/src/main/java/com/autocareai/mvvmdemo/demo/main/MainActivity.kt
View file @
987760e1
...
...
@@ -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
())
}
}
...
...
mvvm-demo/src/main/java/com/autocareai/mvvmdemo/demo/main/MainViewModel.kt
View file @
987760e1
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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment