Home's blocks move on at midnight; a progress block on Completed counts today's (#59)

This commit is contained in:
makiolaj committed 2026-11-27 15:39:23 +01:00
1 parent 4338e9e843
commit 32ec1f8331
3 files changed
+29 -12

No files matched your search

@@ -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<Task>,
context: SourceContext,
hidden: Set<Long>,
admits: (Task) -> Boolean,
): Pair<Int, Int>? {
if (this == BlockSource.Smart(SmartList.COMPLETED)) return null
): Pair<Int, Int> {
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
}
@@ -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<ListsUiState> =
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
@@ -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))
}
}