Drop or switch a task list's scope without querying its tasks again (#59)
This commit is contained in:
1 parent
c63ab674bd
commit
fe1fef5a52
1 file changed
+16
-6
@@ -60,7 +60,6 @@ import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -198,6 +197,18 @@ class TaskListViewModel @Inject constructor(
|
||||
|
||||
private val filter: TaskFilter? get() = binding.value?.filter
|
||||
|
||||
/**
|
||||
* The view whose scope narrows the rows, apart from [binding]: dropping or
|
||||
* switching it reads the rows already loaded through another scope, rather
|
||||
* than querying the tasks again.
|
||||
*/
|
||||
private val scopeViewId = MutableStateFlow<Long?>(null)
|
||||
|
||||
/** That view; a view since deleted, or left without a scope, narrows nothing. */
|
||||
private val scopeView: Flow<View?> = scopeViewId.flatMapLatest { id ->
|
||||
if (id == null) flowOf(null) else settingsPrefs.views.map { views -> views.firstOrNull { it.id == id && it.scope != null } }
|
||||
}
|
||||
|
||||
private val tagFilter = MutableStateFlow<Set<String>>(emptySet())
|
||||
|
||||
/** Occurrences swiped to delete but not yet committed — hidden until the undo lapses. */
|
||||
@@ -224,6 +235,8 @@ class TaskListViewModel @Inject constructor(
|
||||
|
||||
val state: StateFlow<TaskListUiState> =
|
||||
binding.filterNotNull()
|
||||
.map { it.copy(scopeViewId = null) }
|
||||
.distinctUntilChanged()
|
||||
.flatMapLatest { b ->
|
||||
val f = b.filter
|
||||
val source: Flow<FilteredTasks> = when (f) {
|
||||
@@ -239,10 +252,6 @@ class TaskListViewModel @Inject constructor(
|
||||
// Several lists at once only exists for the widget, which never routes here.
|
||||
is TaskFilter.OfLists -> error("OfLists has no task-list screen")
|
||||
}
|
||||
// A view since deleted, or left without a scope, narrows nothing.
|
||||
val scopeView: Flow<View?> = b.scopeViewId
|
||||
?.let { id -> settingsPrefs.views.map { views -> views.firstOrNull { it.id == id && it.scope != null } } }
|
||||
?: flowOf(null)
|
||||
// Each read carries the day it was made for, so rows and groups always agree.
|
||||
val rows = dayWindow.flatMapLatest { window -> source.map { window to it } }
|
||||
val content: Flow<TaskListUiState> = combine(rows, repository.taskLists(), scopeView) { (window, found), lists, view ->
|
||||
@@ -366,6 +375,7 @@ class TaskListViewModel @Inject constructor(
|
||||
pendingAdds.clear()
|
||||
}
|
||||
requested = next
|
||||
this.scopeViewId.value = scopeViewId
|
||||
binding.value = next
|
||||
}
|
||||
|
||||
@@ -396,7 +406,7 @@ class TaskListViewModel @Inject constructor(
|
||||
|
||||
/** Drops the view's scope, the chip's ✕: the screen shows all of its tasks. */
|
||||
fun dropScope() {
|
||||
binding.update { it?.copy(scopeViewId = null) }
|
||||
scopeViewId.value = null
|
||||
}
|
||||
|
||||
fun toggleTagFilter(tag: String) {
|
||||
|
||||
Reference in new issue
Block a user