diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt b/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt index 6f7b09e..e6e0759 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/domain/ViewScope.kt @@ -212,26 +212,30 @@ val DefaultStartDestination: StartDestination = StartDestination.View(View.HOME_ * [admits] lets through. Today counts its own rows, completed ones included. Any * other source counts its open tasks and those completed today that it would hold * were they open, so the ring fills as the day's work gets done rather than with - * everything ever completed. Completed, all done by definition, has no progress. + * everything ever completed. Completed, all done by definition, counts what was + * completed today against itself. */ fun BlockSource.progress( tasks: List, context: SourceContext, hidden: Set, admits: (Task) -> Boolean, -): Pair? { - if (this == BlockSource.Smart(SmartList.COMPLETED)) return null +): Pair { val clock = context.clock val shown = tasks.filter(admits) + fun doneToday(task: Task): Boolean { + val at = task.completedAt + return task.isCompleted && at != null && at >= clock.todayStart && at < clock.todayEnd + } + if (this == BlockSource.Smart(SmartList.COMPLETED)) { + val done = shown.count { doneToday(it) && contains(it, context, hidden) } + return done to done + } if (this == BlockSource.Smart(SmartList.TODAY)) { val inToday = shown.filter { contains(it, context, hidden) } return inToday.count { it.isClosed } to inToday.size } val open = shown.count { !it.isClosed && contains(it, context, hidden) } - val doneToday = shown.count { task -> - val at = task.completedAt - task.isCompleted && at != null && at >= clock.todayStart && at < clock.todayEnd && - contains(task.copy(status = TaskStatus.NEEDS_ACTION), context, hidden) - } - return doneToday to open + doneToday + val done = shown.count { doneToday(it) && contains(it.copy(status = TaskStatus.NEEDS_ACTION), context, hidden) } + return done to open + done } 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 a94ce79..1fbd2ef 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 @@ -47,6 +47,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update @@ -243,7 +244,7 @@ internal fun homeContent( fun shows(task: Task) = source.counts(task, context, smartHidden) && within.admits(task, clock) when (block.type) { BlockType.PROGRESS -> source.progress(topLevel, context, smartHidden) { within.admits(it, clock) } - ?.let { (done, total) -> progress[block.id] = BlockProgress(done, total) } + .let { (done, total) -> progress[block.id] = BlockProgress(done, total) } BlockType.COUNT_TILE -> counts[block.id] = if (source is BlockSource.Smart) { topLevel.count(::shows) } else { @@ -368,6 +369,18 @@ class ListsViewModel @Inject constructor( if (starting) null else (views.firstOrNull { it.id == active } ?: views.first()) to views }.distinctUntilChanged() + /** The view shown, again at each midnight, so the blocks that count today move on to the new day. */ + private val shownToday = combine( + shownView, + flow { + while (true) { + val window = DayWindow.today(Clock.System.now(), ZoneId.systemDefault()) + emit(window) + delay((window.second - Clock.System.now()).inWholeMilliseconds.coerceIn(0, 60_000) + 1) + } + }.distinctUntilChanged(), + ) { shown, _ -> shown } + val state: StateFlow = combine( repository.taskLists(), @@ -381,7 +394,7 @@ class ListsViewModel @Inject constructor( settingsPrefs.savedFilters, ::Triple, ), - shownView, + shownToday, ) { lists, openTasks, completedTasks, (settings, weekStart, filters), shown -> val (view, views) = shown ?: return@combine ListsUiState.Loading val (hidden, upcomingRange) = settings diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt index 59ce376..40e81ec 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/ui/lists/CustomRulesBlocksTest.kt @@ -100,6 +100,6 @@ class CustomRulesBlocksTest { ) assertThat(state.progress.getValue(1L)).isEqualTo(BlockProgress(done = 1, total = 3)) assertThat(state.progress.getValue(2L)).isEqualTo(BlockProgress(done = 1, total = 4)) - assertThat(state.progress).doesNotContainKey(3L) + assertThat(state.progress.getValue(3L)).isEqualTo(BlockProgress(done = 1, total = 1)) } }