Share the rung-before-save check with the planner (#178)
This commit is contained in:
1 parent
0fabb6cd58
commit
878a99bb39
3 files changed
+30
-11
No files matched your search
@@ -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 due
|
||||
* by [now] from a start no later than that.
|
||||
* [sinceCreated] keeps only those after the task was created, and with
|
||||
* [now], none due by then from a start no later than that.
|
||||
*/
|
||||
fun triggers(
|
||||
task: Task,
|
||||
@@ -80,7 +80,7 @@ internal object ReminderPlanner {
|
||||
settings: Settings,
|
||||
zone: ZoneId,
|
||||
sinceCreated: Boolean = true,
|
||||
now: Long = Long.MAX_VALUE,
|
||||
now: Long? = null,
|
||||
): List<ScheduledReminder> {
|
||||
if (task.isClosed || (task.due == null && task.start == null)) return emptyList()
|
||||
// Reminders set on the task itself win — every one of them;
|
||||
@@ -108,7 +108,7 @@ internal object ReminderPlanner {
|
||||
// after, its own included: a start of "now" (#178). Whether one
|
||||
// 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)) {
|
||||
if (sinceCreated && created != null && now != null && trigger <= now && startsByCreation(dates, reminder.fromStart, created)) {
|
||||
return@mapNotNull null
|
||||
}
|
||||
// Dismissed on another client already: firing it here within
|
||||
@@ -166,9 +166,17 @@ internal object ReminderPlanner {
|
||||
startsNow: Boolean = false,
|
||||
): Set<ScheduledReminder> {
|
||||
val kept = own ?: defaultsFor(task, settings).filterNot { startsNow && it.fromStart }
|
||||
return (triggers(task, kept, settings, zone, sinceCreated = false) + triggers(task, pastDefaults, settings, zone, sinceCreated = false)).toSet()
|
||||
return triggers(task, kept + pastDefaults, settings, zone, sinceCreated = false).toSet()
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a reminder of [task] has rung by [now], as far as a save can tell
|
||||
* (#106): any of [own], which a save rang if the planner did not; else a
|
||||
* default the planner rang.
|
||||
*/
|
||||
fun anyRung(task: Task, own: List<TaskReminder>?, settings: Settings, zone: ZoneId, now: Long): Boolean =
|
||||
triggers(task, own, settings, zone, sinceCreated = own == null, now = now).any { it.triggerAt <= now }
|
||||
|
||||
/**
|
||||
* The reminders a save just put at or before [now] (#106): those of [after]
|
||||
* that [before], the task as it was, did not have already.
|
||||
|
||||
@@ -371,12 +371,9 @@ 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 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
|
||||
val zone = ZoneId.systemDefault()
|
||||
if (ReminderPlanner.anyRung(current, own, settings, zone, now)) return null
|
||||
ReminderPlanner.triggers(current, own, settings, zone, sinceCreated = false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -471,4 +471,18 @@ class ReminderPlannerTest {
|
||||
ScheduledReminder(15, millis(date.atTime(9, 0), berlin)),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a default from a start of now never rang, a save's own reminder did (#106, #178)`() {
|
||||
val start = Instant.fromEpochMilliseconds(millis(date.atTime(10, 0), berlin) + 800)
|
||||
val task = testTask(id = 16, start = start, created = Instant.fromEpochMilliseconds(millis(date.atTime(10, 0), berlin)))
|
||||
val now = millis(date.atTime(12, 0), berlin)
|
||||
val atStart = Settings(defaultReminders = listOf(TaskReminder(0, fromStart = true).asReminderLead()))
|
||||
|
||||
assertThat(ReminderPlanner.anyRung(task, null, atStart, berlin, now)).isFalse()
|
||||
assertThat(ReminderPlanner.anyRung(task, listOf(TaskReminder(0, fromStart = true)), atStart, berlin, now)).isTrue()
|
||||
val later = testTask(id = 17, start = Instant.fromEpochMilliseconds(millis(date.atTime(11, 0), berlin)), created = task.created)
|
||||
assertThat(ReminderPlanner.anyRung(later, null, atStart, berlin, now)).isTrue()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user