Merge branch 'fix/178-default-reminder-start' into release/v1.4.0

# Conflicts:
#	CHANGELOG.md
This commit is contained in:
makiolaj committed 2026-11-25 15:43:36 +01:00
commit ac78c56e03
10 files changed
+225 -48

No files matched your search

+6
View File
@@ -64,6 +64,12 @@ All notable changes to this project are documented here. The format follows
all-inkl.com, find their task list instead of reporting none. That list is
the account's calendar itself, so Agendula doesn't offer to delete it or to
create new lists there (#180).
- A task with only a start date gets the default reminder too, counted from
its start, when none of the default reminders is set from the start. Likewise
a task with only a due date gets reminders set from the start, counted from
its due date. This reaches tasks you already have that have no reminders of
their own. A new task starting "now" still takes none from its start, which
would ring at once (#178).
## [1.3.2] - 2026-11-13
@@ -4,9 +4,11 @@ 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
import de.jeanlucmakiola.agendula.domain.forDates
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
@@ -20,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.
*
@@ -40,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) }
@@ -67,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 with
* [now], none due by then from a start no later than that.
*/
fun triggers(
task: Task,
@@ -75,6 +80,7 @@ internal object ReminderPlanner {
settings: Settings,
zone: ZoneId,
sinceCreated: Boolean = true,
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;
@@ -85,8 +91,9 @@ internal object ReminderPlanner {
// ⚠️ Present-but-empty is "none", not "the default": the store's
// answer for a task whose alarms another client turned off. A
// default here would keep ringing for what the user switched off.
// A default counts only from a date the task has; its own reminders
// stand in each date for the other when the task lacks it.
// A default counts from a date the task has, or from its one date when
// no default does (#178); its own reminders stand in each date for the
// other when the task lacks it.
val reminders = own ?: defaultsFor(task, settings)
val created = task.created?.toEpochMilliseconds()
val dates = task.dates
@@ -97,6 +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, 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. One still ahead stays, in case
// [created] is another client's clock running fast.
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
// the missed-reminder grace would ring for something done with.
//
@@ -123,10 +137,19 @@ 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)
.filter { (if (it.fromStart) task.start else task.due) != null }
settings.defaultRemindersFor(task.listId, task.isAllDay).forDates(task.start != null, task.due != null)
/**
* [task]'s reminders once saved: its [own], with defaults older than the
@@ -143,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)).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.
@@ -8,6 +8,7 @@ import de.jeanlucmakiola.agendula.domain.TaskEnd
import de.jeanlucmakiola.agendula.domain.TaskForm
import de.jeanlucmakiola.agendula.domain.TaskReminder
import de.jeanlucmakiola.agendula.domain.calendarDate
import de.jeanlucmakiola.agendula.domain.forDates
import java.time.ZoneId
import kotlin.time.Instant
@@ -132,7 +133,7 @@ internal object StoredReminders {
if (fresh.isEmpty()) return wanted
val defaults = defaultsFor(form, settings, now, zone, keepPast)
if (anchorsOf(start, due).isEmpty() || wanted.isEmpty()) return wanted.ifEmpty { defaults }
val named = wanted.map { it.fromStart }.toSet()
val named = namedEnds(form, wanted)
return wanted + defaults.filter { it.fromStart in fresh && it.fromStart !in named }
}
@@ -175,7 +176,9 @@ internal object StoredReminders {
/** What [switched] keeps of [reminders], or null when it swaps in the new kind's defaults. */
fun keptAcrossSwitch(reminders: List<TaskReminder>, form: TaskForm, settings: Settings): List<TaskReminder>? {
if (reminders.toSet() == settings.defaultRemindersFor(form.listId, !form.isAllDay).toSet()) return null
val oldDefaults = settings.defaultRemindersFor(form.listId, !form.isAllDay)
val ofItsDates = oldDefaults.forDates(form.start != null, form.due != null)
if (reminders.toSet().let { it == oldDefaults.toSet() || it == ofItsDates.toSet() }) return null
val kept = if (form.isAllDay) {
reminders.map { it.inWholeDays() }
} else {
@@ -200,8 +203,8 @@ internal object StoredReminders {
zone: ZoneId,
): Set<Boolean> {
if (form.rrule != null || wanted.isEmpty()) return emptySet()
val named = wanted.map { it.fromStart }.toSet()
val defaults = settings.defaultRemindersFor(form.listId, was.isAllDay)
val named = namedEnds(form, wanted)
val defaults = settings.defaultRemindersFor(form.listId, was.isAllDay).forDates(was.start != null, was.due != null)
fun revived(fromStart: Boolean, before: Instant?, isNow: Instant?): Boolean {
if (before == null || isNow == null || fromStart in named) return false
val moved = if (was.isAllDay == form.isAllDay) {
@@ -224,9 +227,16 @@ internal object StoredReminders {
private fun anchorsOf(start: Any?, due: Any?): Set<Boolean> =
setOfNotNull(true.takeIf { start != null }, false.takeIf { due != null })
/** The dates [wanted] count from on [form], as [TaskReminder.fromStart]. */
private fun namedEnds(form: TaskForm, wanted: List<TaskReminder>): Set<Boolean> {
val dates = TaskDates(form.start, form.due, form.isAllDay)
return wanted.map { dates.reminderEnd(it.fromStart) == TaskEnd.START }.toSet()
}
/**
* The defaults [form] takes: those counting from a date it has. A one-off
* task leaves out what is already past — a start of "now" would ring at once.
* The defaults [form] takes, of the dates it has (see [forDates]). A one-off
* task leaves out what is already past and any from a start of
* [now][TaskForm.startsNow], which would ring at once.
*/
private fun defaultsFor(
form: TaskForm,
@@ -235,14 +245,16 @@ internal object StoredReminders {
zone: ZoneId,
keepPast: Boolean = false,
): List<TaskReminder> =
settings.defaultRemindersFor(form.listId, form.isAllDay).filter { reminder ->
if ((if (reminder.fromStart) form.start else form.due) == null) return@filter false
form.rrule != null || keepPast || ReminderPlanner.ringsAt(
TaskDates(form.start, form.due, form.isAllDay, form.untimed),
reminder.fromStart,
reminder.minutesBefore,
settings.allDayReminderMinuteOfDay,
zone,
) >= now
}
settings.defaultRemindersFor(form.listId, form.isAllDay)
.forDates(form.start != null, form.due != null)
.filter { reminder ->
if (form.startsNow && reminder.fromStart && form.rrule == null) return@filter false
keepPast || form.rrule != null || ReminderPlanner.ringsAt(
TaskDates(form.start, form.due, form.isAllDay, form.untimed),
reminder.fromStart,
reminder.minutesBefore,
settings.allDayReminderMinuteOfDay,
zone,
) >= now
}
}
@@ -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)
}
}
@@ -78,6 +78,17 @@ fun taskReminderDisplayOrder(allDay: Boolean, allDayMinuteOfDay: Int): Comparato
}
})
/**
* The default reminders of these that a task with [hasStart] and [hasDue] takes:
* those counting from a date it has, or, when none does, all of them counting
* from the one date it has (#178).
*/
fun List<TaskReminder>.forDates(hasStart: Boolean, hasDue: Boolean): List<TaskReminder> {
val ofItsDates = filter { if (it.fromStart) hasStart else hasDue }
if (ofItsDates.isNotEmpty() || hasStart == hasDue) return ofItsDates
return map { it.copy(fromStart = hasStart) }.distinct()
}
/** A default reminder's lead as a reminder of the task's own. */
fun ReminderLead.asTaskReminder(): TaskReminder = TaskReminder(minutes, fromStart)
@@ -259,6 +259,14 @@ class TaskEditViewModel @Inject constructor(
?: writable.firstOrNull { it.id in filterLists }?.id
?: writable.firstOrNull { !it.isLocal }?.id
?: writable.firstOrNull()?.id
defaultStart = dates.start?.let { start ->
DefaultStart(
at = start,
timed = !dates.isAllDay,
isNow = start == now,
followsDue = settings.defaultStartDate.takeIf { dates.due != null },
)
}
_state.value = withFields(
TaskEditUiState(
loading = false,
@@ -278,17 +286,7 @@ class TaskEditViewModel @Inject constructor(
title = initialTitle.orEmpty(),
tags = (listOfNotNull(presetTag) + preset?.tags.orEmpty()).distinct(),
).withDefaultReminders(),
).also {
baseline = it.toForm().copy(title = "")
defaultStart = it.start?.let { start ->
DefaultStart(
at = start,
timed = !it.isAllDay,
isNow = start == now,
followsDue = settings.defaultStartDate.takeIf { dates.due != null },
)
}
}
).also { baseline = it.toForm().copy(title = "") }
}
}
@@ -548,7 +546,7 @@ class TaskEditViewModel @Inject constructor(
private fun TaskEditUiState.withDefaultReminders(): TaskEditUiState {
if (remindersTouched) return this
val defaulted = StoredReminders.defaulted(
toForm().copy(reminders = loadedReminders),
savedForm().copy(reminders = loadedReminders),
loadedStart,
loadedDue,
loadedAllDay,
@@ -587,6 +585,12 @@ class TaskEditViewModel @Inject constructor(
}
private fun nowMillis(): Long = Clock.System.now().toEpochMilliseconds()
/** [toForm] with [TaskForm.startsNow] when the start is still the default "now". */
private fun TaskEditUiState.savedForm(): TaskForm = toForm().let { form ->
val nowStart = defaultStart?.takeIf { it.isNow }?.at
form.copy(startsNow = nowStart != null && !form.isAllDay && form.start == nowStart)
}
fun onPriorityChange(value: Priority) = update { it.copy(priority = value) }
fun onPercentChange(value: Int?) = update { it.copy(percentComplete = value?.coerceIn(0, 100)) }
fun onParentChange(parentId: Long?) = update { it.copy(parentId = parentId) }
@@ -645,10 +649,7 @@ class TaskEditViewModel @Inject constructor(
fun save(force: Boolean = false, scope: RecurringScope = lastScope) {
lastScope = scope
val current = _state.value
val form = current.toForm().let { form ->
val nowStart = defaultStart?.takeIf { it.isNow }?.at
form.copy(startsNow = nowStart != null && !form.isAllDay && form.start == nowStart)
}
val form = current.savedForm()
val errors = form.validate()
if (errors.isNotEmpty()) {
_state.value = current.copy(errors = errors)
@@ -413,4 +413,76 @@ class ReminderPlannerTest {
assertThat(ReminderPlanner.triggers(task, null, settings, berlin, sinceCreated = false))
.containsExactly(ScheduledReminder(4, due.toEpochMilliseconds() - 3_600_000L))
}
@Test
fun `a task with only one date takes the defaults of the other from it (#178)`() {
val start = Instant.fromEpochMilliseconds(millis(date.atTime(9, 0), berlin))
val due = Instant.fromEpochMilliseconds(millis(date.atTime(18, 0), berlin))
val startOnly = testTask(id = 11, start = start)
val dueOnly = testTask(id = 12, due = due)
val now = millis(date.atTime(0, 0), berlin)
assertThat(ReminderPlanner.plan(listOf(startOnly), emptyMap(), Settings(defaultReminders = listOf(15).asReminderLeads()), now, berlin))
.containsExactly(ScheduledReminder(11, millis(date.atTime(8, 45), berlin)))
val fromStart = Settings(defaultReminders = listOf(TaskReminder(60, fromStart = true).asReminderLead()))
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()
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()))
}
@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)),
)
}
@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()
}
}
@@ -280,4 +280,40 @@ class StoredRemindersTest {
assertThat(switched(listOf(ownTime, TaskReminder(2_880)), form)).containsExactly(TaskReminder(2_880))
assertThat(switched(listOf(ownTime), form)).containsExactly(dayBeforeDue, hourBeforeDue)
}
@Test
fun `a task with only a start takes the due defaults from its start (#178)`() {
val dueDefault = Settings()
val startOnly = TaskForm(title = "t", listId = 1, start = now + 2.hours)
assertThat(stored(startOnly, with = dueDefault)).containsExactly(TaskReminder(0, fromStart = true))
assertThat(stored(startOnly, testTask(), with = dueDefault)).containsExactly(TaskReminder(0, fromStart = true))
val startDefault = Settings(defaultReminders = listOf(TaskReminder(30, fromStart = true).asReminderLead()))
val dueOnly = TaskForm(title = "t", listId = 1, due = now + 2.hours)
assertThat(stored(dueOnly, with = startDefault)).containsExactly(TaskReminder(30))
}
@Test
fun `a start of now takes no due default from it (#178)`() {
val form = TaskForm(title = "t", listId = 1, start = now - 2.hours, startsNow = true)
assertThat(stored(form, with = Settings())).isEmpty()
assertThat(StoredReminders.pastDefaults(form, null, Settings(), now.toEpochMilliseconds(), zone)).isEmpty()
assertThat(stored(form.copy(start = now), with = Settings())).isEmpty()
assertThat(StoredReminders.leftOutAsPast(form, null, null, false, Settings(), now.toEpochMilliseconds(), zone)).isEmpty()
}
@Test
fun `a due swapped for a start keeps the reminder that now counts from it`() {
val current = testTask(due = now + 5.days)
val form = TaskForm(title = "t", listId = 1, start = now + 2.days, reminders = listOf(TaskReminder(0)))
assertThat(stored(form, current, with = Settings())).containsExactly(TaskReminder(0))
}
@Test
fun `a start-only default swaps when the task turns all-day`() {
val legacy = Settings(defaultReminders = listOf(15).asReminderLeads(), defaultAllDayReminders = listOf(1_440).asReminderLeads())
val start = allDayInstantOf(LocalDate.of(2026, 10, 3))
val form = TaskForm(title = "t", listId = 1, start = start, isAllDay = true)
assertThat(switched(listOf(TaskReminder(15, fromStart = true)), form, legacy))
.containsExactly(TaskReminder(1_440, fromStart = true))
}
}
@@ -42,10 +42,10 @@ class ListDefaultsHintTest {
}
@Test
fun `the #121 case - only At due, a start-only task - says the list added none`() {
fun `the #121 case - only At due, a start-only task - takes it from the start (#178)`() {
val (reminders, hint) = hintForNew(TaskForm(title = "t", listId = 1, start = now + 2.days))
assertThat(reminders).isEmpty()
assertThat(hint).isEqualTo(ListDefaultsHint("Prywatny", none = true))
assertThat(reminders).containsExactly(TaskReminder(0, fromStart = true))
assertThat(hint).isEqualTo(ListDefaultsHint("Prywatny", none = false))
}
@Test
@@ -89,5 +89,6 @@ class ListDefaultsHintTest {
val timedDefaults = settings.defaultRemindersFor(2, allDay = false)
assertThat(StoredReminders.keptAcrossSwitch(timedDefaults, form, settings)).isNull()
assertThat(StoredReminders.keptAcrossSwitch(listOf(TaskReminder(1_440)), form, settings)).isNotNull()
assertThat(StoredReminders.keptAcrossSwitch(listOf(atDue), form, settings)).isNull()
}
}
@@ -136,6 +136,16 @@ class TaskEditViewModelTest {
assertThat(vm.state.value.isAllDay).isFalse()
}
@Test
fun `a start of now takes no default, a start picked later does (#178)`() {
val prefs = settingsPrefs(preferencesOf(booleanPreferencesKey("default_start_today") to true))
val vm = TaskEditViewModel(repository, prefs).apply { bindNew() }
assertThat(vm.state.value.reminders).isEmpty()
vm.onWhenChange(TaskEnd.START, LocalDate.now().plusDays(1), LocalTime.of(15, 0))
assertThat(vm.state.value.reminders).containsExactly(TaskReminder(0, fromStart = true))
}
@Test
fun `clearing the last date drops the reminders and the task saves`() {
val vm = editing()