Remind right away when a save puts a reminder in the past (#106)
This commit is contained in:
1 parent
1463b08d6f
commit
d7dfc4da06
11 files changed
+184
-55
No files matched your search
@@ -7,6 +7,11 @@ All notable changes to this project are documented here. The format follows
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Added
|
||||
- A "Remind right away when past" setting under Notifications: saving a task
|
||||
whose reminder time has already passed, such as one starting now, notifies
|
||||
at once instead of leaving the reminder out (#106).
|
||||
|
||||
## [1.3.2] - 2026-11-13
|
||||
|
||||
### Added
|
||||
|
||||
@@ -85,6 +85,8 @@ data class Settings(
|
||||
val allDayReminderMinuteOfDay: Int = DEFAULT_ALL_DAY_REMINDER_MINUTE,
|
||||
/** Master switch for due reminders; off clears every scheduled alarm. */
|
||||
val remindersEnabled: Boolean = true,
|
||||
/** Ring at once when a save puts a task's reminder in the past, instead of leaving it out (#106). */
|
||||
val remindPastOnSave: Boolean = false,
|
||||
/** Whether the inline "add a subtask" row shows on expanded task-list groups. */
|
||||
val showAddSubtaskRow: Boolean = true,
|
||||
/**
|
||||
@@ -172,6 +174,7 @@ class SettingsPrefs @Inject constructor(
|
||||
allDayReminderMinuteOfDay = p[ALL_DAY_REMINDER_MINUTE]?.takeIf { it in 0 until 24 * 60 }
|
||||
?: DEFAULT_ALL_DAY_REMINDER_MINUTE,
|
||||
remindersEnabled = p[REMINDERS_ENABLED] ?: true,
|
||||
remindPastOnSave = p[REMIND_PAST_ON_SAVE] ?: false,
|
||||
showAddSubtaskRow = p[SHOW_ADD_SUBTASK_ROW] ?: true,
|
||||
bottomAddBar = p[BOTTOM_ADD_BAR] ?: false,
|
||||
perListReminderOverride = reminderCodec.parseLeads(p[LIST_REMINDER_OVERRIDE]),
|
||||
@@ -225,6 +228,8 @@ class SettingsPrefs @Inject constructor(
|
||||
|
||||
suspend fun setDefaultStartToday(enabled: Boolean) = dataStore.edit { it[DEFAULT_START_TODAY] = enabled }
|
||||
|
||||
suspend fun setRemindPastOnSave(enabled: Boolean) = dataStore.edit { it[REMIND_PAST_ON_SAVE] = enabled }
|
||||
|
||||
suspend fun setTimeFormat(pref: TimeFormatPref) = dataStore.edit { it[TIME_FORMAT] = pref.name }
|
||||
|
||||
suspend fun setWeekStart(day: DayOfWeek?) = dataStore.edit {
|
||||
@@ -371,6 +376,7 @@ class SettingsPrefs @Inject constructor(
|
||||
val PUSH_ENABLED = booleanPreferencesKey("push_enabled")
|
||||
val SMART_LIST_HIDDEN = stringSetPreferencesKey("smart_list_hidden")
|
||||
val DEFAULT_START_TODAY = booleanPreferencesKey("default_start_today")
|
||||
val REMIND_PAST_ON_SAVE = booleanPreferencesKey("remind_past_on_save")
|
||||
val COLLAPSED_SECTIONS = stringSetPreferencesKey("collapsed_sections")
|
||||
val SAVED_FILTERS = stringPreferencesKey("saved_filters")
|
||||
val SAVED_FILTER_NEXT_ID = longPreferencesKey("saved_filter_next_id")
|
||||
|
||||
@@ -37,57 +37,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.
|
||||
.filter { !it.isClosed && (it.due != null || it.start != null) }
|
||||
.flatMap { task ->
|
||||
// Reminders set on the task itself win — every one of them;
|
||||
// otherwise the task's list may override the global lead, or opt
|
||||
// out entirely (override = null). All-day tasks have their own
|
||||
// day-scale defaults.
|
||||
//
|
||||
// ⚠️ 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.
|
||||
val own = perTask[task.taskId]
|
||||
val reminders = own ?: settings.defaultRemindersFor(task.listId, task.isAllDay)
|
||||
.filter { (if (it.fromStart) task.start else task.due) != null }
|
||||
val created = task.created?.toEpochMilliseconds()
|
||||
reminders.mapNotNull { reminder ->
|
||||
val anchor = if (reminder.fromStart) task.start ?: task.due!! else task.due ?: task.start!!
|
||||
val trigger = triggerAt(
|
||||
anchor,
|
||||
task.isAllDay,
|
||||
reminder.minutesBefore,
|
||||
settings.allDayReminderMinuteOfDay,
|
||||
zone,
|
||||
)
|
||||
// A default never rings for a moment before the task existed:
|
||||
// that is a task saved after its default had passed, not a
|
||||
// reminder missed while the device was off.
|
||||
if (own == null && created != null && trigger < created) return@mapNotNull null
|
||||
// Dismissed on another client already: firing it here within
|
||||
// the missed-reminder grace would ring for something done with.
|
||||
//
|
||||
// ⚠️ An all-day reminder without a time of its own is compared
|
||||
// from the start of its day, not from this device's time of
|
||||
// day: other clients ring it at midnight, or at their own hour,
|
||||
// so a dismissal at 07:00 must still silence the 09:00 this
|
||||
// device would ring it at.
|
||||
val earliest = if (task.isAllDay && reminder.allDay.minuteOfDay == null) {
|
||||
triggerAt(anchor, true, reminder.minutesBefore, 0, zone)
|
||||
} else {
|
||||
trigger
|
||||
}
|
||||
val acknowledged = reminder.acknowledgedAt?.let { earliest <= it.toEpochMilliseconds() } == true
|
||||
if (acknowledged) return@mapNotNull null
|
||||
ScheduledReminder(
|
||||
taskId = task.taskId,
|
||||
triggerAt = trigger,
|
||||
occurrenceStart = task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE,
|
||||
)
|
||||
}
|
||||
}
|
||||
.flatMap { triggers(it, perTask[it.taskId], settings, zone) }
|
||||
// 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) }
|
||||
@@ -98,6 +48,69 @@ internal object ReminderPlanner {
|
||||
.take(MAX_ALARMS)
|
||||
.toSet()
|
||||
|
||||
/**
|
||||
* Every reminder of [task] that is still to ring, wherever it falls in time:
|
||||
* [own] if it has reminders of its own, else its list's or the app's default.
|
||||
*/
|
||||
fun triggers(task: Task, own: List<TaskReminder>?, settings: Settings, zone: ZoneId): List<ScheduledReminder> {
|
||||
if (task.isClosed || (task.due == null && task.start == null)) return emptyList()
|
||||
// Reminders set on the task itself win — every one of them;
|
||||
// otherwise the task's list may override the global lead, or opt
|
||||
// out entirely (override = null). All-day tasks have their own
|
||||
// day-scale defaults.
|
||||
//
|
||||
// ⚠️ 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.
|
||||
val reminders = own ?: settings.defaultRemindersFor(task.listId, task.isAllDay)
|
||||
.filter { (if (it.fromStart) task.start else task.due) != null }
|
||||
val created = task.created?.toEpochMilliseconds()
|
||||
return reminders.mapNotNull { reminder ->
|
||||
val anchor = if (reminder.fromStart) task.start ?: task.due!! else task.due ?: task.start!!
|
||||
val trigger = triggerAt(
|
||||
anchor,
|
||||
task.isAllDay,
|
||||
reminder.minutesBefore,
|
||||
settings.allDayReminderMinuteOfDay,
|
||||
zone,
|
||||
)
|
||||
// A default never rings for a moment before the task existed:
|
||||
// that is a task saved after its default had passed, not a
|
||||
// reminder missed while the device was off.
|
||||
if (own == null && created != null && trigger < created) return@mapNotNull null
|
||||
// Dismissed on another client already: firing it here within
|
||||
// the missed-reminder grace would ring for something done with.
|
||||
//
|
||||
// ⚠️ An all-day reminder without a time of its own is compared
|
||||
// from the start of its day, not from this device's time of
|
||||
// day: other clients ring it at midnight, or at their own hour,
|
||||
// so a dismissal at 07:00 must still silence the 09:00 this
|
||||
// device would ring it at.
|
||||
val earliest = if (task.isAllDay && reminder.allDay.minuteOfDay == null) {
|
||||
triggerAt(anchor, true, reminder.minutesBefore, 0, zone)
|
||||
} else {
|
||||
trigger
|
||||
}
|
||||
val acknowledged = reminder.acknowledgedAt?.let { earliest <= it.toEpochMilliseconds() } == true
|
||||
if (acknowledged) return@mapNotNull null
|
||||
ScheduledReminder(
|
||||
taskId = task.taskId,
|
||||
triggerAt = trigger,
|
||||
occurrenceStart = task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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. Ringing them at
|
||||
* once is the point; one the task had all along is no news, however overdue.
|
||||
*/
|
||||
fun pastOnSave(before: Collection<ScheduledReminder>, after: Collection<ScheduledReminder>, now: Long): Set<ScheduledReminder> =
|
||||
after.filterTo(HashSet()) { it.triggerAt <= now } - before.toSet()
|
||||
|
||||
/**
|
||||
* When a reminder [leadMinutes] before [anchor] fires.
|
||||
*
|
||||
|
||||
@@ -6,11 +6,13 @@ import android.content.Context
|
||||
import android.os.Build
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import de.jeanlucmakiola.agendula.data.di.IoDispatcher
|
||||
import de.jeanlucmakiola.agendula.data.prefs.Settings
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.tasks.ProviderResolver
|
||||
import de.jeanlucmakiola.agendula.data.tasks.StorageMode
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TaskQuery
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TasksDataSource
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
@@ -57,6 +59,47 @@ class ReminderScheduler @Inject constructor(
|
||||
suspend fun markFired(reminder: ScheduledReminder) =
|
||||
withContext(io) { syncLock.withLock { store.markFired(reminder) } }
|
||||
|
||||
/**
|
||||
* The reminders [task] has as stored now, for [remindPastOnSave] to compare
|
||||
* the saved task against; null when that is off or they cannot be read.
|
||||
* Empty for a recurring one: its occurrences are left to [sync] and its
|
||||
* missed-reminder grace.
|
||||
*/
|
||||
suspend fun remindersOf(task: Task?): List<ScheduledReminder>? = withContext(io) {
|
||||
runCatching {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
when {
|
||||
!settings.remindersEnabled || !settings.remindPastOnSave -> null
|
||||
task == null || task.isRecurring -> emptyList()
|
||||
else -> ReminderPlanner.triggers(task, dataSource.reminders()[task.taskId], settings, ZoneId.systemDefault())
|
||||
}
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
/**
|
||||
* Rings [taskId] at once when its save put a reminder at or before now that
|
||||
* [before] did not have (#106), if [Settings.remindPastOnSave] is on. The
|
||||
* reminders it rings for are marked fired, so [sync] does not arm them
|
||||
* again within the missed-reminder grace.
|
||||
*/
|
||||
suspend fun remindPastOnSave(taskId: Long, before: List<ScheduledReminder>) = withContext(io) {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
if (!settings.remindersEnabled || !settings.remindPastOnSave) return@withContext
|
||||
val task = runCatching { dataSource.task(taskId) }.getOrNull() ?: return@withContext
|
||||
if (task.isRecurring) return@withContext
|
||||
val after = runCatching {
|
||||
ReminderPlanner.triggers(task, dataSource.reminders()[taskId], settings, ZoneId.systemDefault())
|
||||
}.getOrElse { return@withContext }
|
||||
val past = ReminderPlanner.pastOnSave(before, after, System.currentTimeMillis())
|
||||
if (past.isEmpty()) return@withContext
|
||||
val due = syncLock.withLock {
|
||||
// A pass that got in first has armed or rung these already.
|
||||
val handled = store.all() + store.fired()
|
||||
past.filterNot { it in handled }.onEach { store.markFired(it) }
|
||||
}
|
||||
if (due.isNotEmpty()) runCatching { notifier.postIfOpen(dataSource, taskId, ScheduledReminder.NO_OCCURRENCE, settings) }
|
||||
}
|
||||
|
||||
/**
|
||||
* @param afterReboot every alarm is gone, so nothing in the store is armed;
|
||||
* skips asking the system about each one.
|
||||
|
||||
@@ -143,12 +143,13 @@ internal object StoredReminders {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* task leaves out what is already past — a start of "now" would ring at once —
|
||||
* unless [Settings.remindPastOnSave] asks for exactly that.
|
||||
*/
|
||||
private fun defaultsFor(form: TaskForm, settings: Settings, now: Long, zone: ZoneId): List<TaskReminder> =
|
||||
settings.defaultRemindersFor(form.listId, form.isAllDay).filter { reminder ->
|
||||
val anchor = (if (reminder.fromStart) form.start else form.due) ?: return@filter false
|
||||
form.rrule != null || ReminderPlanner.triggerAt(
|
||||
form.rrule != null || settings.remindPastOnSave || ReminderPlanner.triggerAt(
|
||||
anchor,
|
||||
form.isAllDay,
|
||||
reminder.minutesBefore,
|
||||
|
||||
@@ -3,6 +3,8 @@ package de.jeanlucmakiola.agendula.data.tasks
|
||||
import de.jeanlucmakiola.agendula.domain.TaskReminder
|
||||
import de.jeanlucmakiola.agendula.data.di.IoDispatcher
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.reminders.ReminderScheduler
|
||||
import de.jeanlucmakiola.agendula.data.reminders.ScheduledReminder
|
||||
import de.jeanlucmakiola.floret.time.DayWindow
|
||||
import de.jeanlucmakiola.agendula.domain.SavedFilter
|
||||
import de.jeanlucmakiola.agendula.domain.SeriesCollapse
|
||||
@@ -40,6 +42,7 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
private val providerResolver: ProviderResolver,
|
||||
private val startupGate: StartupGate,
|
||||
private val settingsPrefs: SettingsPrefs,
|
||||
private val reminderScheduler: ReminderScheduler,
|
||||
@IoDispatcher private val io: CoroutineDispatcher,
|
||||
) : TasksRepository {
|
||||
|
||||
@@ -114,6 +117,7 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
override suspend fun createTask(form: TaskForm): Long =
|
||||
withContext(io) {
|
||||
dataSource.insertTask(form, storedRemindersFor(form, current = null))
|
||||
.also { remindPastOnSave(it, before = emptyList()) }
|
||||
}
|
||||
|
||||
private suspend fun storedRemindersFor(form: TaskForm, current: Task?): List<TaskReminder> =
|
||||
@@ -205,8 +209,13 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
// Going through the occurrence forks an override instead, which takes
|
||||
// the reminders on its own row and leaves the series' alone.
|
||||
val instance = current?.takeIf { it.isRecurring }?.occurrenceStart
|
||||
if (instance == null) dataSource.updateTask(resolvedRow(current, taskId), form, reminders)
|
||||
else dataSource.updateInstance(taskId, instance, form, reminders)
|
||||
if (instance == null) {
|
||||
val before = reminderScheduler.remindersOf(current)
|
||||
dataSource.updateTask(resolvedRow(current, taskId), form, reminders)
|
||||
if (before != null) remindPastOnSave(resolvedRow(current, taskId), before)
|
||||
} else {
|
||||
dataSource.updateInstance(taskId, instance, form, reminders)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun setCancelled(taskId: Long, occurrenceStart: Instant?, cancelled: Boolean) =
|
||||
@@ -304,6 +313,11 @@ class TasksRepositoryImpl @Inject constructor(
|
||||
*/
|
||||
private fun resolvedRow(resolved: Task?, taskId: Long): Long = resolved?.taskId ?: taskId
|
||||
|
||||
/** The save stands whatever its reminder does. */
|
||||
private suspend fun remindPastOnSave(taskId: Long, before: List<ScheduledReminder>) {
|
||||
runCatching { reminderScheduler.remindPastOnSave(taskId, before) }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Subtask levels a duplicate follows; deeper than any real list, short of a cycle. */
|
||||
|
||||
@@ -190,6 +190,15 @@ internal fun NotificationsScreen(
|
||||
position = Position.Middle,
|
||||
onClick = { showAllDayTime = true },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_remind_past_on_save),
|
||||
summary = stringResource(R.string.settings_remind_past_on_save_hint),
|
||||
position = Position.Middle,
|
||||
trailing = {
|
||||
Switch(checked = state.settings.remindPastOnSave, onCheckedChange = viewModel::setRemindPastOnSave)
|
||||
},
|
||||
onClick = { viewModel.setRemindPastOnSave(!state.settings.remindPastOnSave) },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_reminder_description),
|
||||
summary = reminderDescriptionLabel(state.settings.reminderDescription),
|
||||
|
||||
@@ -264,6 +264,7 @@ class SettingsViewModel @Inject constructor(
|
||||
fun setShowAddSubtaskRow(show: Boolean) = viewModelScope.launch { prefs.setShowAddSubtaskRow(show) }
|
||||
fun setBottomAddBar(enabled: Boolean) = viewModelScope.launch { prefs.setBottomAddBar(enabled) }
|
||||
fun setDefaultStartToday(enabled: Boolean) = viewModelScope.launch { prefs.setDefaultStartToday(enabled) }
|
||||
fun setRemindPastOnSave(enabled: Boolean) = viewModelScope.launch { prefs.setRemindPastOnSave(enabled) }
|
||||
|
||||
/** Toggle whether [field] shows by default on a new task's edit form. */
|
||||
fun setFormFieldDefault(field: TaskFormField, enabled: Boolean) = viewModelScope.launch {
|
||||
|
||||
@@ -485,6 +485,8 @@
|
||||
<string name="settings_reliable_delivery_hint">Android may delay reminders to save battery. Exempt Agendula so they arrive on time.</string>
|
||||
<string name="settings_reliable_delivery_exempt">Exempt from battery optimisation — reminders arrive on time.</string>
|
||||
<string name="settings_reminder_description">Description in reminders</string>
|
||||
<string name="settings_remind_past_on_save">Remind right away when past</string>
|
||||
<string name="settings_remind_past_on_save_hint">Saving a task whose reminder time has already passed notifies at once instead of skipping it</string>
|
||||
<string name="settings_reminder_description_hidden">Hidden</string>
|
||||
<string name="settings_reminder_description_short">Short</string>
|
||||
<string name="settings_reminder_description_full">Full</string>
|
||||
|
||||
@@ -324,4 +324,32 @@ class ReminderPlannerTest {
|
||||
)
|
||||
assertThat(plan).containsExactly(ScheduledReminder(10, millis(date.atTime(8, 45), berlin)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a save rings the reminders it put in the past, not the ones the task had`() {
|
||||
val now = millis(date.atTime(12, 0), berlin)
|
||||
val overdue = ScheduledReminder(1, now - 3 * 24 * 3_600_000L)
|
||||
val justNow = ScheduledReminder(1, now - 60_000L)
|
||||
val atNow = ScheduledReminder(1, now)
|
||||
val later = ScheduledReminder(1, now + 60_000L)
|
||||
assertThat(ReminderPlanner.pastOnSave(emptyList(), listOf(overdue, atNow, later), now))
|
||||
.containsExactly(overdue, atNow)
|
||||
assertThat(ReminderPlanner.pastOnSave(listOf(overdue), listOf(overdue, justNow, later), now))
|
||||
.containsExactly(justNow)
|
||||
assertThat(ReminderPlanner.pastOnSave(listOf(overdue), listOf(overdue), now)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `triggers reach past the window but not a closed or dateless task`() {
|
||||
val due = Instant.fromEpochMilliseconds(millis(date.atTime(12, 0), berlin))
|
||||
val own = listOf(TaskReminder(0), TaskReminder(30 * 24 * 60))
|
||||
val task = testTask(id = 4, due = due)
|
||||
assertThat(ReminderPlanner.triggers(task, own, Settings(), berlin)).containsExactly(
|
||||
ScheduledReminder(4, due.toEpochMilliseconds()),
|
||||
ScheduledReminder(4, due.toEpochMilliseconds() - 30L * 24 * 3_600_000L),
|
||||
)
|
||||
assertThat(ReminderPlanner.triggers(testTask(id = 4), own, Settings(), berlin)).isEmpty()
|
||||
val done = testTask(id = 4, due = due, status = de.jeanlucmakiola.agendula.domain.TaskStatus.COMPLETED)
|
||||
assertThat(ReminderPlanner.triggers(done, own, Settings(), berlin)).isEmpty()
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,13 @@ class StoredRemindersTest {
|
||||
assertThat(stored(form)).containsExactly(dayBeforeDue, hourBeforeDue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a default already past is kept when it is to ring on save`() {
|
||||
val form = TaskForm(title = "t", listId = 1, start = now, due = now + 5.days)
|
||||
assertThat(stored(form, with = settings.copy(remindPastOnSave = true)))
|
||||
.containsExactly(dayBeforeStart, dayBeforeDue, hourBeforeDue)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a recurring task keeps it for the occurrences to come`() {
|
||||
val form = TaskForm(title = "t", listId = 1, start = now, due = now + 5.days, rrule = "FREQ=DAILY")
|
||||
|
||||
Reference in new issue
Block a user