Only hold back a late start-of-now reminder, keep the old rung check (#178)

This commit is contained in:
makiolaj committed 2026-11-25 15:27:24 +01:00
1 parent 42c1df84bd
commit 0fabb6cd58
3 files changed
+21 -10

No files matched your search

@@ -44,7 +44,7 @@ internal object ReminderPlanner {
// One reminder per *occurrence* and lead: a recurring series yields a row
// per occurrence, all sharing a taskId, so this is a Set rather than a
// taskId-keyed Map.
.flatMap { triggers(it, perTask[it.taskId], settings, zone) }
.flatMap { triggers(it, perTask[it.taskId], settings, zone, now = now) }
// The lower bound trails `now` so a reminder missed while the device was
// off still fires once on boot instead of being silently dropped.
.filter { it.triggerAt in (now - MISSED_GRACE_MS)..(now + WINDOW_MS) }
@@ -71,8 +71,8 @@ internal object ReminderPlanner {
/**
* Every reminder of [task] not yet dismissed, past ones included: [own] if it
* has reminders of its own, else its list's or the app's default — of which
* [sinceCreated] keeps only those after the task was created, and none from
* a start no later than that.
* [sinceCreated] keeps only those after the task was created, and none due
* by [now] from a start no later than that.
*/
fun triggers(
task: Task,
@@ -80,6 +80,7 @@ internal object ReminderPlanner {
settings: Settings,
zone: ZoneId,
sinceCreated: Boolean = true,
now: Long = Long.MAX_VALUE,
): List<ScheduledReminder> {
if (task.isClosed || (task.due == null && task.start == null)) return emptyList()
// Reminders set on the task itself win — every one of them;
@@ -103,10 +104,13 @@ internal object ReminderPlanner {
// that is a task saved after its default had passed, not a
// reminder missed while the device was off.
if (sinceCreated && own == null && created != null && trigger < created) return@mapNotNull null
// Nor does any reminder from a start the task was created at or
// Nor, late, does any from a start the task was created at or
// after, its own included: a start of "now" (#178). Whether one
// rings on save is up to #106, which counts from before this.
if (sinceCreated && created != null && startsByCreation(dates, reminder.fromStart, created)) return@mapNotNull null
// rings on save is up to #106. One still ahead stays, in case
// [created] is another client's clock running fast.
if (sinceCreated && created != null && trigger <= now && startsByCreation(dates, reminder.fromStart, created)) {
return@mapNotNull null
}
// Dismissed on another client already: firing it here within
// the missed-reminder grace would ring for something done with.
//
@@ -371,10 +371,12 @@ class TasksRepositoryImpl @Inject constructor(
return logFailure("Reading the reminders before a save failed") {
val own = dataSource.reminders(current.taskId)
val now = Clock.System.now().toEpochMilliseconds()
val zone = ZoneId.systemDefault()
// Those the planner arms count as rung; not a default from before the task, nor one from a start of now.
if (ReminderPlanner.triggers(current, own, settings, zone).any { it.triggerAt <= now }) return null
ReminderPlanner.triggers(current, own, settings, zone, sinceCreated = false)
val all = ReminderPlanner.triggers(current, own, settings, ZoneId.systemDefault(), sinceCreated = false)
// As triggers() counts them: a default before the task was created never rang.
val created = current.created?.toEpochMilliseconds()
val counted = if (own == null && created != null) all.filter { it.triggerAt >= created } else all
if (counted.any { it.triggerAt <= now }) return null
all
}
}
@@ -441,6 +441,11 @@ class ReminderPlannerTest {
assertThat(ReminderPlanner.plan(listOf(task), mapOf(13L to atStart), Settings(), now, berlin)).isEmpty()
assertThat(ReminderPlanner.triggers(task, atStart, Settings(), berlin, sinceCreated = false))
.containsExactly(ScheduledReminder(13, start.toEpochMilliseconds()))
assertThat(ReminderPlanner.afterSave(task, null, atStart, Settings(), berlin))
.contains(ScheduledReminder(13, start.toEpochMilliseconds()))
val fastClock = task.copy(created = Instant.fromEpochMilliseconds(start.toEpochMilliseconds() + 600_000))
assertThat(ReminderPlanner.plan(listOf(fastClock), mapOf(13L to atStart), Settings(), start.toEpochMilliseconds() - 60_000, berlin))
.containsExactly(ScheduledReminder(13, start.toEpochMilliseconds()))
val nextDay = task.copy(start = Instant.fromEpochMilliseconds(start.toEpochMilliseconds() + 86_400_000))
assertThat(ReminderPlanner.plan(listOf(nextDay), mapOf(13L to atStart), Settings(), now, berlin))
.containsExactly(ScheduledReminder(13, nextDay.start!!.toEpochMilliseconds()))