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