@@ -19,8 +19,10 @@ import androidx.annotation.DrawableRes
19
19
import androidx.annotation.StringRes
20
20
import androidx.lifecycle.LiveData
21
21
import androidx.lifecycle.MutableLiveData
22
+ import androidx.lifecycle.SavedStateHandle
22
23
import androidx.lifecycle.Transformations
23
24
import androidx.lifecycle.ViewModel
25
+ import androidx.lifecycle.distinctUntilChanged
24
26
import androidx.lifecycle.switchMap
25
27
import androidx.lifecycle.viewModelScope
26
28
import com.example.android.architecture.blueprints.todoapp.Event
@@ -30,13 +32,15 @@ import com.example.android.architecture.blueprints.todoapp.data.Result.Success
30
32
import com.example.android.architecture.blueprints.todoapp.data.Task
31
33
import com.example.android.architecture.blueprints.todoapp.data.source.TasksDataSource
32
34
import com.example.android.architecture.blueprints.todoapp.data.source.TasksRepository
35
+ import com.example.android.architecture.blueprints.todoapp.tasks.TasksFilterType.*
33
36
import kotlinx.coroutines.launch
34
37
35
38
/* *
36
39
* ViewModel for the task list screen.
37
40
*/
38
41
class TasksViewModel (
39
- private val tasksRepository : TasksRepository
42
+ private val tasksRepository : TasksRepository ,
43
+ private val savedStateHandle : SavedStateHandle
40
44
) : ViewModel() {
41
45
42
46
private val _forceUpdate = MutableLiveData <Boolean >(false )
@@ -49,7 +53,7 @@ class TasksViewModel(
49
53
_dataLoading .value = false
50
54
}
51
55
}
52
- tasksRepository.observeTasks().switchMap { filterTasks(it) }
56
+ tasksRepository.observeTasks().distinctUntilChanged(). switchMap { filterTasks(it) }
53
57
}
54
58
55
59
val items: LiveData <List <Task >> = _items
@@ -72,8 +76,6 @@ class TasksViewModel(
72
76
private val _snackbarText = MutableLiveData <Event <Int >>()
73
77
val snackbarText: LiveData <Event <Int >> = _snackbarText
74
78
75
- private var currentFiltering = TasksFilterType .ALL_TASKS
76
-
77
79
// Not used at the moment
78
80
private val isDataLoadingError = MutableLiveData <Boolean >()
79
81
@@ -92,7 +94,7 @@ class TasksViewModel(
92
94
93
95
init {
94
96
// Set initial state
95
- setFiltering(TasksFilterType . ALL_TASKS )
97
+ setFiltering(getSavedFilterType() )
96
98
loadTasks(true )
97
99
}
98
100
@@ -104,23 +106,23 @@ class TasksViewModel(
104
106
* [TasksFilterType.ACTIVE_TASKS]
105
107
*/
106
108
fun setFiltering (requestType : TasksFilterType ) {
107
- currentFiltering = requestType
109
+ savedStateHandle.set( TASKS_FILTER_SAVED_STATE_KEY , requestType.ordinal)
108
110
109
111
// Depending on the filter type, set the filtering label, icon drawables, etc.
110
112
when (requestType) {
111
- TasksFilterType . ALL_TASKS -> {
113
+ ALL_TASKS -> {
112
114
setFilter(
113
115
R .string.label_all, R .string.no_tasks_all,
114
116
R .drawable.logo_no_fill, true
115
117
)
116
118
}
117
- TasksFilterType . ACTIVE_TASKS -> {
119
+ ACTIVE_TASKS -> {
118
120
setFilter(
119
121
R .string.label_active, R .string.no_tasks_active,
120
122
R .drawable.ic_check_circle_96dp, false
121
123
)
122
124
}
123
- TasksFilterType . COMPLETED_TASKS -> {
125
+ COMPLETED_TASKS -> {
124
126
setFilter(
125
127
R .string.label_completed, R .string.no_tasks_completed,
126
128
R .drawable.ic_verified_user_96dp, false
@@ -195,7 +197,7 @@ class TasksViewModel(
195
197
if (tasksResult is Success ) {
196
198
isDataLoadingError.value = false
197
199
viewModelScope.launch {
198
- result.value = filterItems(tasksResult.data, currentFiltering )
200
+ result.value = filterItems(tasksResult.data, getSavedFilterType() )
199
201
}
200
202
} else {
201
203
result.value = emptyList()
@@ -218,11 +220,11 @@ class TasksViewModel(
218
220
// We filter the tasks based on the requestType
219
221
for (task in tasks) {
220
222
when (filteringType) {
221
- TasksFilterType . ALL_TASKS -> tasksToShow.add(task)
222
- TasksFilterType . ACTIVE_TASKS -> if (task.isActive) {
223
+ ALL_TASKS -> tasksToShow.add(task)
224
+ ACTIVE_TASKS -> if (task.isActive) {
223
225
tasksToShow.add(task)
224
226
}
225
- TasksFilterType . COMPLETED_TASKS -> if (task.isCompleted) {
227
+ COMPLETED_TASKS -> if (task.isCompleted) {
226
228
tasksToShow.add(task)
227
229
}
228
230
}
@@ -233,4 +235,12 @@ class TasksViewModel(
233
235
fun refresh () {
234
236
_forceUpdate .value = true
235
237
}
238
+
239
+ private fun getSavedFilterType () : TasksFilterType {
240
+ return TasksFilterType
241
+ .values()[savedStateHandle.get(TASKS_FILTER_SAVED_STATE_KEY ) ? : ALL_TASKS .ordinal]
242
+ }
236
243
}
244
+
245
+ // Used to save the current filtering in SavedStateHandle.
246
+ const val TASKS_FILTER_SAVED_STATE_KEY = " TASKS_FILTER_SAVED_STATE_KEY"
0 commit comments