Rebind one task list model on home instead of keeping one per view (#59)

This commit is contained in:
makiolaj committed 2026-11-27 13:36:38 +01:00
1 parent fe1fef5a52
commit 3b1f89623e
2 files changed
+7 -5

No files matched your search

@@ -188,7 +188,8 @@ fun ListsScreen(
// Blocks open what they show within the view's scope, unless they ignore it.
val scopeFor = { block: Block -> content?.view?.takeIf { it.scope != null && !block.ignoreScope }?.id }
val blockModel: TaskListViewModel? = if (content != null && taskBlock != null && taskFilter != null) {
val model: TaskListViewModel = hiltViewModel(key = "view-${content.view.id}-block-${taskBlock.id}")
// One model, rebound as the view changes, rather than one kept per view visited.
val model: TaskListViewModel = hiltViewModel(key = "home-task-block")
val scopeViewId = scopeFor(taskBlock)
val options = taskBlock.options as BlockOptions.TaskList
LaunchedEffect(taskFilter, scopeViewId, options) {
@@ -378,7 +379,7 @@ private fun ListsContent(
val rows = remember(blocks) { blocks.rows() }
val blockState = taskBlock?.model?.state?.collectAsStateWithLifecycle()?.value as? TaskListUiState.Content
val taskRows = if (taskBlock != null && blockState != null) {
rememberTaskRows(blockState, taskBlock.filter, taskBlock.model)
rememberTaskRows(blockState, taskBlock.filter, taskBlock.model, expansionKey = state.view.id)
} else {
null
}
@@ -591,7 +592,7 @@ private fun sourceLabel(source: BlockSource, state: ListsUiState.Content): Strin
is BlockSource.Smart -> stringResource(smartListLabel(source.smart))
is BlockSource.List -> state.lists.firstOrNull { it.id == source.listId }?.name.orEmpty()
is BlockSource.Tag -> source.tag
is BlockSource.Filter -> state.unscopedSections.filters.firstOrNull { it.filter.id == source.filterId }?.filter?.name.orEmpty()
is BlockSource.Filter -> state.filterNames[source.filterId].orEmpty()
is BlockSource.ListElsewhere, is BlockSource.Rules -> ""
}
@@ -552,6 +552,7 @@ internal fun rememberTaskRows(
filter: TaskFilter,
viewModel: TaskListViewModel,
onClearCompleted: (() -> Unit)? = null,
expansionKey: Any = viewModel,
): TaskRows {
val todayStart = state.todayStart
val todayEnd = state.todayEnd
@@ -589,8 +590,8 @@ internal fun rememberTaskRows(
)
// Parents the user expanded to reveal their subtasks inline. Children are
// grouped from the same flat task set the list already holds.
// Per model: home hosts a different one for each view's task list.
var expandedParents by rememberSaveable(viewModel) { mutableStateOf(emptySet<Long>()) }
// Per [expansionKey]: home rebinds one model from view to view.
var expandedParents by rememberSaveable(expansionKey) { mutableStateOf(emptySet<Long>()) }
val childrenByParent = remember(state.tasks) {
state.tasks.filter { it.isSubtask }.groupBy { it.parentId!! }
}