Ring nothing from a start of now on any store (#178)

The planner skips reminders from a timed start no later than the task's
creation, so the rule holds without the save-time flag.
This commit is contained in:
makiolaj committed 2026-11-25 15:07:18 +01:00
1 parent 6a0bc6ab70
commit 1787580a48
2 files changed
+56 -2

No files matched your search

@@ -4,6 +4,7 @@ import de.jeanlucmakiola.agendula.data.prefs.Settings
import de.jeanlucmakiola.agendula.domain.AllDayReminder
import de.jeanlucmakiola.agendula.domain.Task
import de.jeanlucmakiola.agendula.domain.TaskDates
import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.untimedDate
import de.jeanlucmakiola.agendula.domain.TaskReminder
import de.jeanlucmakiola.agendula.domain.calendarDate
@@ -21,6 +22,8 @@ internal object ReminderPlanner {
/** How long after its trigger a missed reminder is still worth firing. */
const val MISSED_GRACE_MS = 6L * 60 * 60 * 1000 // 6 hours
private const val CREATED_SLACK_MS = 1_000L
/**
* Alarms this app will hold at once.
*
@@ -68,7 +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.
* [sinceCreated] keeps only those after the task was created, and none from
* a start no later than that.
*/
fun triggers(
task: Task,
@@ -99,6 +103,10 @@ 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
// 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
// Dismissed on another client already: firing it here within
// the missed-reminder grace would ring for something done with.
//
@@ -125,6 +133,16 @@ internal object ReminderPlanner {
}
}
/**
* Whether a reminder [fromStart] counts from a timed start no later than
* [created], give or take [CREATED_SLACK_MS] for one stored in whole seconds.
*/
private fun startsByCreation(dates: TaskDates, fromStart: Boolean, created: Long): Boolean {
if (dates.reminderEnd(fromStart) != TaskEnd.START || !dates.hasTime(TaskEnd.START)) return false
val start = dates.start ?: return false
return start.toEpochMilliseconds() <= created + CREATED_SLACK_MS
}
/** The list's or the app's default reminders for [task], of the dates it has. */
private fun defaultsFor(task: Task, settings: Settings): List<TaskReminder> =
settings.defaultRemindersFor(task.listId, task.isAllDay).forDates(task.start != null, task.due != null)
@@ -144,7 +162,7 @@ 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)).toSet()
return (triggers(task, kept, settings, zone, sinceCreated = false) + triggers(task, pastDefaults, settings, zone, sinceCreated = false)).toSet()
}
/**
@@ -428,4 +428,40 @@ class ReminderPlannerTest {
assertThat(ReminderPlanner.plan(listOf(dueOnly), emptyMap(), fromStart, now, berlin))
.containsExactly(ScheduledReminder(12, millis(date.atTime(17, 0), berlin)))
}
@Test
fun `nothing rings from a start of now, whatever the store kept (#178)`() {
val start = Instant.fromEpochMilliseconds(millis(date.atTime(12, 0), berlin) + 800)
val createdInSeconds = Instant.fromEpochMilliseconds(millis(date.atTime(12, 0), berlin))
val task = testTask(id = 13, start = start, created = createdInSeconds)
val now = start.toEpochMilliseconds() + 60_000
val atStart = listOf(TaskReminder(0, fromStart = true))
assertThat(ReminderPlanner.plan(listOf(task), emptyMap(), Settings(), now, berlin)).isEmpty()
assertThat(ReminderPlanner.plan(listOf(task), mapOf(13L to atStart), Settings(), now, berlin)).isEmpty()
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()))
}
@Test
fun `a start without a time on the day the task was made still rings`() {
val created = Instant.fromEpochMilliseconds(millis(date.atTime(8, 0), berlin))
val untimed = testTask(id = 14, start = untimedInstantOf(TaskEnd.START, date, berlin), created = created)
.copy(due = Instant.fromEpochMilliseconds(millis(date.plusDays(2).atTime(18, 0), berlin)), untimed = TaskEnd.START)
val allDay = testTask(id = 15, start = allDayInstantOf(date), isAllDay = true, created = created)
val atStart = listOf(TaskReminder(0, fromStart = true))
val plan = ReminderPlanner.plan(
listOf(untimed, allDay),
mapOf(14L to atStart, 15L to atStart),
Settings(),
created.toEpochMilliseconds(),
berlin,
)
assertThat(plan).containsExactly(
ScheduledReminder(14, millis(date.atTime(9, 0), berlin)),
ScheduledReminder(15, millis(date.atTime(9, 0), berlin)),
)
}
}