@@ -8,11 +8,9 @@ import androidx.lifecycle.ViewModel
88import androidx.lifecycle.viewModelScope
99import com.edricchan.studybuddy.data.common.QueryMapper
1010import com.edricchan.studybuddy.domain.common.sorting.SortDirection
11- import com.edricchan.studybuddy.domain.common.sorting.toFirestoreDirection
1211import com.edricchan.studybuddy.exts.common.TAG
1312import com.edricchan.studybuddy.features.tasks.constants.sharedprefs.TodoOptionsPrefConstants
1413import com.edricchan.studybuddy.features.tasks.constants.sharedprefs.TodoOptionsPrefConstants.TodoSortValues
15- import com.edricchan.studybuddy.features.tasks.data.mapper.toDto
1614import com.edricchan.studybuddy.features.tasks.data.model.TodoItem
1715import com.edricchan.studybuddy.features.tasks.data.repo.TaskRepository
1816import com.edricchan.studybuddy.features.tasks.data.repo.toggleCompleted
@@ -33,20 +31,6 @@ class TasksListViewModel @Inject constructor(
3331 @param:ApplicationContext private val context : Context ,
3432 private val savedState : SavedStateHandle
3533) : ViewModel() {
36- private val taskOptionsPrefs = context.getSharedPreferences(
37- TodoOptionsPrefConstants .FILE_TODO_OPTIONS ,
38- Context .MODE_PRIVATE
39- )
40-
41- // TODO: Remove when migrated to new UI for sorting preferences
42- private val sortCompatMap = mapOf (
43- TodoSortValues .NONE to null ,
44- TodoSortValues .TITLE_ASC to (TaskItem .Field .Title to SortDirection .Ascending ),
45- TodoSortValues .TITLE_DESC to (TaskItem .Field .Title to SortDirection .Descending ),
46- TodoSortValues .DUE_DATE_NEW_TO_OLD to (TaskItem .Field .DueDate to SortDirection .Descending ),
47- TodoSortValues .DUE_DATE_OLD_TO_NEW to (TaskItem .Field .DueDate to SortDirection .Ascending )
48- )
49-
5034 // A MutableStateFlow but without strict equality comparisons
5135 private val _query =
5236 MutableSharedFlow <QueryMapper ?>(replay = 1 , onBufferOverflow = BufferOverflow .DROP_OLDEST )
@@ -64,39 +48,6 @@ class TasksListViewModel @Inject constructor(
6448 setQuery(null )
6549 }
6650
67- /* * The (compat) Firestore query to use, in its [TodoSortValues] form. */
68- val compatQuery = savedState.getStateFlow(
69- KEY_COMPAT_QUERY ,
70- taskOptionsPrefs.getString(
71- TodoOptionsPrefConstants .PREF_DEFAULT_SORT , TodoSortValues .NONE
72- )
73- )
74-
75- /* * Updates the current Firestore query with the new sorting option to use. */
76- @Deprecated(" For backwards compatibility with existing shared prefs" )
77- suspend fun updateSort (value : String ) {
78- // Persist updated data
79- taskOptionsPrefs.edit {
80- putString(
81- TodoOptionsPrefConstants .PREF_DEFAULT_SORT ,
82- value
83- )
84- }
85- savedState[KEY_COMPAT_QUERY ] = value
86-
87- // Update the query
88- val sortBy = sortCompatMap[value]
89- setQuery(
90- sortBy?.let { (field, dir) ->
91- { it.orderBy(field.toDto().fieldName, dir.toFirestoreDirection()) }
92- } ? : run {
93- Log .w(TAG , " Unrecognised sort option $value was specified" )
94- // Reset the query used
95- null
96- }
97- )
98- }
99-
10051 /* * The current list of tasks to be shown. */
10152 @OptIn(ExperimentalCoroutinesApi ::class )
10253 val tasks = query
@@ -128,15 +79,4 @@ class TasksListViewModel @Inject constructor(
12879 resetQuery()
12980 }
13081 }
131-
132- companion object {
133- /* *
134- * Saved-state key used to store the current query.
135- *
136- * Its saved-state value represents the compat version as used in the
137- * task settings UI.
138- * @see TodoSortValues
139- */
140- const val KEY_COMPAT_QUERY = " tasks:compatQuery"
141- }
14282}
0 commit comments