From fe1fef5a52d3010953214e7bfd782bb32a8880a2 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 27 Nov 2026 13:36:38 +0100 Subject: [PATCH] Drop or switch a task list's scope without querying its tasks again (#59) --- .../agendula/ui/tasklist/TaskListViewModel.kt | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt index fe56be7..4ca3913 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/tasklist/TaskListViewModel.kt @@ -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(null) + + /** That view; a view since deleted, or left without a scope, narrows nothing. */ + private val scopeView: Flow = 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>(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 = binding.filterNotNull() + .map { it.copy(scopeViewId = null) } + .distinctUntilChanged() .flatMapLatest { b -> val f = b.filter val source: Flow = 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 = 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 = 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) {