From 56f81d9c2e3d71441d44fe156952171e38c43ee2 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 27 Nov 2026 13:36:38 +0100 Subject: [PATCH] Work out home's sections and scoped search only once shown (#59) --- .../agendula/ui/lists/ListsViewModel.kt | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt index e991c98..418ecd9 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/lists/ListsViewModel.kt @@ -127,19 +127,28 @@ sealed interface ListsUiState { val goneSources: Set = emptySet(), /** The view's scope names a list since deleted, so that part of it matches nothing. */ val scopeListsGone: Boolean = false, - /** The lists, tags and filters blocks within the view's scope. */ - val sections: HomeSections = HomeSections(emptyList(), emptyList(), emptyList()), - /** The same for every task: a block that ignores the scope, and the lists and filters picker. */ - val unscopedSections: HomeSections = sections, + /** + * The lists, tags and filters blocks within the view's scope, worked out + * only once a block shows them. + */ + private val scopedSections: Lazy = lazyOf(HomeSections(emptyList(), emptyList(), emptyList())), + /** The same for every task, for a block that ignores the scope and the pickers; also only once read. */ + private val everySection: Lazy = scopedSections, + /** The saved filters' names by id, which a filter source goes by. */ + val filterNames: Map = emptyMap(), /** Every list, whatever the scope: whether there are any, and the names sources show. */ val lists: List = emptyList(), /** Every task (open and completed) — the corpus the home search filters. */ val allTasks: List = emptyList(), - /** [allTasks] within the view's scope, where a search in a scoped view starts. */ - val scopedTasks: List = allTasks, + /** [allTasks] within the view's scope, where a search in a scoped view starts; filtered once searched. */ + private val scopedCorpus: Lazy> = lazyOf(allTasks), /** The list a new task goes to, so it shows in the view: the scope's one list, if it keeps tasks to one. */ val newTaskList: Long? = null, ) : ListsUiState { + val sections: HomeSections get() = scopedSections.value + val unscopedSections: HomeSections get() = everySection.value + val scopedTasks: List get() = scopedCorpus.value + fun sectionsFor(block: Block): HomeSections = if (block.ignoreScope) unscopedSections else sections } } @@ -201,7 +210,7 @@ internal fun homeContent( } val allTasks = openTasks + completedTasks - val sections = homeSections(lists, openTasks, allTasks, filters, scope, clock) + val every = lazy(LazyThreadSafetyMode.PUBLICATION) { homeSections(lists, openTasks, allTasks, filters, AppliedScope.None, clock) } return ListsUiState.Content( view = view, views = views, @@ -210,15 +219,20 @@ internal fun homeContent( previews = previews, goneSources = gone, scopeListsGone = scope.goneLists.isNotEmpty(), - sections = sections, - unscopedSections = if (scope == AppliedScope.None) { - sections + scopedSections = if (scope == AppliedScope.None) { + every } else { - homeSections(lists, openTasks, allTasks, filters, AppliedScope.None, clock) + lazy(LazyThreadSafetyMode.PUBLICATION) { homeSections(lists, openTasks, allTasks, filters, scope, clock) } }, + everySection = every, + filterNames = filters.associate { it.id to it.name }, lists = lists, allTasks = allTasks, - scopedTasks = if (scope == AppliedScope.None) allTasks else allTasks.filter { scope.admits(it, clock) }, + scopedCorpus = if (scope == AppliedScope.None) { + lazyOf(allTasks) + } else { + lazy(LazyThreadSafetyMode.PUBLICATION) { allTasks.filter { scope.admits(it, clock) } } + }, newTaskList = scope.lists?.singleOrNull()?.takeIf { it in listIds }, ) }