Skip to content

Commit d893423

Browse files
Spring WongSpring Wong
Spring Wong
authored and
Spring Wong
committed
added clearData to reset ViewModel data
1 parent 3391902 commit d893423

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

app/src/main/java/com/spring/androidconductormvvmsample/controllers/DetailController.kt

+4
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@ class DetailController : Controller() {
4747
view.btnPush.clicks().subscribe({
4848
flowControl.push(DetailController())
4949
})
50+
51+
view.btnClear.clicks().subscribe({
52+
viewModel.clearData()
53+
})
5054
}
5155
}

app/src/main/java/com/spring/androidconductormvvmsample/viewModel/SimpleViewModel.kt

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import javax.inject.Inject
1717
*/
1818
class SimpleViewModel {
1919
@Inject
20-
lateinit var service: GithubService
21-
var user : User? = null
20+
protected lateinit var service: GithubService
21+
private var user : User? = null
2222

2323
constructor() {
2424
MainActivity.graph.inject(this)
@@ -31,4 +31,9 @@ class SimpleViewModel {
3131
return service.getMyProfile().doOnSuccess { success -> this.user = success }.subscribeOn(Schedulers.newThread())
3232
}
3333
}
34+
35+
fun clearData() {
36+
user = null
37+
}
38+
3439
}

app/src/main/res/layout/controller_detail.xml

+6
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@
4141
android:layout_height="wrap_content"
4242
android:text="Click Me to push new controller"/>
4343

44+
<Button
45+
android:id="@+id/btnClear"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:text="Click me to clear user cache"/>
49+
4450
</LinearLayout>

0 commit comments

Comments
 (0)