Persistent reminders: per-list override (#100)

This commit is contained in:
makiolaj committed 2026-11-17 16:21:48 +01:00
1 parent 3fecd313f5
commit 953ded977b
10 files changed
+217 -23

No files matched your search

+3 -1
View File
@@ -10,7 +10,9 @@ All notable changes to this project are documented here. The format follows
### Added
- A "Persistent reminders" setting under Notifications brings a swiped-away
reminder straight back, silently, until the task is marked done or snoozed.
Persistent reminders also come back after a reboot (#100).
Persistent reminders also come back after a reboot. Each list can follow
that setting or keep its reminders always or never persistent, under
Per-list reminders (#100).
## [1.3.2] - 2026-11-13
@@ -54,6 +54,9 @@ fun TimeFormatPref.is24Hour(systemIs24Hour: Boolean): Boolean = when (this) {
TimeFormatPref.TWENTY_FOUR_HOUR -> true
}
/** A list's own take on [Settings.persistentReminders]; [INHERIT] follows the app setting. */
enum class ListPersistence { INHERIT, ALWAYS, NEVER }
/** How much of a task's description a due reminder shows when expanded. */
enum class ReminderDescription { HIDDEN, SHORT, FULL }
@@ -109,6 +112,8 @@ data class Settings(
val reminderDescription: ReminderDescription = ReminderDescription.SHORT,
/** A swiped-away reminder comes straight back until the task is done or snoozed. */
val persistentReminders: Boolean = false,
/** Lists of the current store that override [persistentReminders]: always (true) or never (false). */
val perListPersistentReminders: Map<Long, Boolean> = emptyMap(),
val timeFormat: TimeFormatPref = TimeFormatPref.AUTO,
/** The first day of the week; `null` follows the locale. */
val weekStart: DayOfWeek? = null,
@@ -149,6 +154,20 @@ data class Settings(
fun hasOwnReminders(listId: Long, allDay: Boolean): Boolean =
listId in (if (allDay) perListAllDayReminderOverride else perListReminderOverride)
fun persistenceFor(listId: Long): ListPersistence = when (perListPersistentReminders[listId]) {
null -> ListPersistence.INHERIT
true -> ListPersistence.ALWAYS
false -> ListPersistence.NEVER
}
/** Whether a reminder for a task in [listId] is persistent: its list's override, else the app setting. */
fun persistentRemindersFor(listId: Long): Boolean =
remindersEnabled && (perListPersistentReminders[listId] ?: persistentReminders)
/** Whether any list's reminders can be persistent. */
val anyPersistentReminders: Boolean
get() = remindersEnabled && (persistentReminders || perListPersistentReminders.containsValue(true))
/** Whether [listId] has its own default reminders for timed or all-day tasks. */
fun hasOwnReminders(listId: Long): Boolean =
hasOwnReminders(listId, allDay = false) || hasOwnReminders(listId, allDay = true)
@@ -189,6 +208,7 @@ class SettingsPrefs @Inject constructor(
?.let { runCatching { ReminderDescription.valueOf(it) }.getOrNull() }
?: ReminderDescription.SHORT,
persistentReminders = p[PERSISTENT_REMINDERS] ?: false,
perListPersistentReminders = parseListPersistence(p[LIST_PERSISTENT_REMINDERS].orEmpty(), modeOf(p).name),
timeFormat = p[TIME_FORMAT]?.let { runCatching { TimeFormatPref.valueOf(it) }.getOrNull() }
?: TimeFormatPref.AUTO,
autofocusTitle = p[AUTOFOCUS_TITLE] ?: true,
@@ -222,6 +242,13 @@ class SettingsPrefs @Inject constructor(
suspend fun setPersistentReminders(enabled: Boolean) = dataStore.edit { it[PERSISTENT_REMINDERS] = enabled }
/** Sets (or clears, via [ListPersistence.INHERIT]) a list's persistent-reminder override in the current store. */
suspend fun setListPersistence(listId: Long, persistence: ListPersistence) = dataStore.edit { p ->
val prefix = "${modeOf(p).name}:$listId="
val rest = p[LIST_PERSISTENT_REMINDERS].orEmpty().filterNotTo(HashSet()) { it.startsWith(prefix) }
p[LIST_PERSISTENT_REMINDERS] = if (persistence == ListPersistence.INHERIT) rest else rest + "$prefix${persistence.name}"
}
suspend fun setSyncIntervalMinutes(minutes: Int) = dataStore.edit { it[SYNC_INTERVAL] = minutes }
suspend fun setPushEnabled(enabled: Boolean) = dataStore.edit { it[PUSH_ENABLED] = enabled }
@@ -370,6 +397,7 @@ class SettingsPrefs @Inject constructor(
val SNOOZE_MINUTES = intPreferencesKey("snooze_minutes")
val REMINDER_DESCRIPTION = stringPreferencesKey("reminder_description")
val PERSISTENT_REMINDERS = booleanPreferencesKey("persistent_reminders")
val LIST_PERSISTENT_REMINDERS = stringSetPreferencesKey("list_persistent_reminders")
val TIME_FORMAT = stringPreferencesKey("time_format")
val WEEK_START = stringPreferencesKey("week_start")
val AUTOFOCUS_TITLE = booleanPreferencesKey("autofocus_title")
@@ -390,6 +418,19 @@ class SettingsPrefs @Inject constructor(
*/
private val reminderCodec = ReminderOverrideCodec.DEFAULT
/** `MODE:listId=ALWAYS|NEVER` entries, [mode]'s only. */
internal fun parseListPersistence(entries: Set<String>, mode: String): Map<Long, Boolean> =
entries.mapNotNull { entry ->
val (id, value) = entry.removePrefix("$mode:").takeIf { it != entry }?.split('=')
?.takeIf { it.size == 2 } ?: return@mapNotNull null
val listId = id.toLongOrNull() ?: return@mapNotNull null
when (value) {
ListPersistence.ALWAYS.name -> listId to true
ListPersistence.NEVER.name -> listId to false
else -> null
}
}.toMap()
/** `s5,30` → [5 before start, 30 before due]; an empty string is an explicit "no reminder". */
private fun parseLeads(stored: String): List<ReminderLead> =
stored.split(',').mapNotNull { ReminderLead.parse(it)?.takeIf { lead -> lead.minutes >= 0 } }.normalizeReminders()
@@ -125,7 +125,7 @@ internal fun Task.isOccurrence(occurrenceStart: Long?): Boolean = when (occurren
* With [exact], a lookup that fell back to another occurrence posts nothing.
*
* [restoring] brings back a persistent reminder, unless its task was moved to a
* later due since. A persistent reminder that won't come back is forgotten; one
* later due since or its list no longer keeps reminders persistent. A persistent reminder that won't come back is forgotten; one
* whose task an external store just failed to find is kept for a while.
*/
internal suspend fun TaskNotifier.postIfOpen(
@@ -143,7 +143,8 @@ internal suspend fun TaskNotifier.postIfOpen(
if (missingMeansGone()) forget(taskId, occurrence = occurrenceStart) else missed(taskId, occurrenceStart)
return
}
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart)) || (restoring != null && task.postponedSince(restoring))) {
val dropRestore = restoring != null && (task.postponedSince(restoring) || !settings.persistentRemindersFor(task.listId))
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart)) || dropRestore) {
forget(taskId, occurrence = occurrenceStart)
if (task.taskId != taskId) forget(task.taskId, occurrence = occurrenceStart)
return
@@ -76,7 +76,8 @@ class ReminderActionReceiver : BroadcastReceiver() {
if (store == null) return@runCatching
val settings = settingsPrefs.settings.first()
startupGate.awaitReady()
if (settings.remindersEnabled && settings.persistentReminders && notifier.isCurrentStore(store)) {
// Whether its list still keeps it persistent is up to the re-post, which reads the task.
if (settings.anyPersistentReminders && notifier.isCurrentStore(store)) {
notifier.repostRemembered(taskId, occurrence, settings)
} else {
notifier.forget(taskId, store, occurrence)
@@ -54,7 +54,7 @@ import kotlin.time.Instant
* Posts one notification per due task on its list's channel, so each list can
* have its own sound and importance. The tag is the task id, so a re-fired alarm
* replaces rather than duplicates. Tapping opens the task's detail screen (see
* [MainActivity.taskIntent]). With [Settings.persistentReminders] a reminder
* [MainActivity.taskIntent]). With [Settings.persistentRemindersFor] its list a reminder
* re-posts silently when swiped away and is remembered so a reboot brings it back.
*/
@Singleton
@@ -102,7 +102,7 @@ class TaskNotifier @Inject constructor(
// canPost() checks POST_NOTIFICATIONS before we ever call notify().
@SuppressLint("MissingPermission")
private suspend fun post(task: Task, settings: Settings, alert: Boolean, postedAt: Long?) {
val persistent = settings.persistentReminders
val persistent = settings.persistentRemindersFor(task.listId)
val occurrence = task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE
val shownAt = postedAt ?: System.currentTimeMillis()
val channelId = ensureListChannel(task.listId, task.listName)
@@ -223,9 +223,9 @@ class TaskNotifier @Inject constructor(
*/
suspend fun restyleActive() = postLock.withLock {
val settings = settingsPrefs.settings.first()
val persistent = settings.remindersEnabled && settings.persistentReminders
postedReminders().forEach { posted ->
val occurrence = posted.occurrence ?: return@forEach
val persistent = settings.persistentRemindersFor(posted.listId ?: return@forEach)
val extras = posted.notification.extras
if (!extras.containsKey(EXTRA_DUE)) return@forEach
val due = extras.getLong(EXTRA_DUE).takeIf { it != NO_DUE }
@@ -275,7 +275,7 @@ class TaskNotifier @Inject constructor(
* other tasks here.
*/
suspend fun restorePersistent(settings: Settings) {
if (!settings.remindersEnabled || !settings.persistentReminders) return records.clear()
if (!settings.anyPersistentReminders) return records.clear()
// Before it, the store is a guess, and a wrong one would drop every record.
startupGate.awaitReady()
val store = currentStore
@@ -286,7 +286,13 @@ class TaskNotifier @Inject constructor(
records.all().filter { it.taskId !in shown }.forEach { runCatching { repost(it, settings) } }
}
private class Posted(val taskId: Long, val occurrence: Long?, val postedAt: Long, val notification: Notification)
private class Posted(
val taskId: Long,
val listId: Long?,
val occurrence: Long?,
val postedAt: Long,
val notification: Notification,
)
/** The current store's posted reminders; another store's ids name other tasks. */
private fun postedReminders(): List<Posted> {
@@ -297,7 +303,8 @@ class TaskNotifier @Inject constructor(
val taskId = posted.tag?.toLongOrNull() ?: return@mapNotNull null
val extras = posted.notification.extras
val occurrence = if (extras.containsKey(EXTRA_OCCURRENCE)) extras.getLong(EXTRA_OCCURRENCE) else null
Posted(taskId, occurrence, posted.notification.`when`, posted.notification)
val listId = posted.notification.channelId.removePrefix(prefix).toLongOrNull()
Posted(taskId, listId, occurrence, posted.notification.`when`, posted.notification)
}
}
@@ -43,6 +43,7 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.agendula.data.prefs.ListPersistence
import de.jeanlucmakiola.agendula.data.prefs.ReminderDescription
import de.jeanlucmakiola.agendula.data.prefs.SNOOZE_PRESETS
import de.jeanlucmakiola.agendula.data.reminders.TaskNotifier
@@ -124,6 +125,7 @@ internal fun NotificationsScreen(
var perListExpanded by remember { mutableStateOf(false) }
var expandedLists by remember { mutableStateOf(emptySet<Long>()) }
var overrideTarget by remember { mutableStateOf<OverrideTarget?>(null) }
var persistenceTarget by remember { mutableStateOf<Long?>(null) }
val allDayFiresAt = stringResource(
R.string.settings_allday_reminder_fires_at,
formatMinuteOfDay(state.settings.allDayReminderMinuteOfDay, LocalUse24HourFormat.current),
@@ -132,6 +134,7 @@ internal fun NotificationsScreen(
val ownTimed = lists.count { state.settings.hasOwnReminders(it.id, allDay = false) }
val ownAllDay = lists.count { state.settings.hasOwnReminders(it.id, allDay = true) }
val ownAny = lists.count { state.settings.hasOwnReminders(it.id) }
val ownPersistence = lists.count { it.id in state.settings.perListPersistentReminders }
CollapsingScaffold(
title = stringResource(R.string.settings_section_reminders),
@@ -212,7 +215,7 @@ internal fun NotificationsScreen(
)
GroupedSwitchRow(
title = stringResource(R.string.settings_persistent_reminders),
summary = stringResource(R.string.settings_persistent_reminders_hint),
summary = withOwnCount(stringResource(R.string.settings_persistent_reminders_hint), ownPersistence),
checked = state.settings.persistentReminders,
onCheckedChange = viewModel::setPersistentReminders,
position = Position.Middle,
@@ -260,7 +263,10 @@ internal fun NotificationsScreen(
// off, unless it is open and its two rows close the run.
GroupedRow(
title = AnnotatedString(list.name),
summary = listSourceSummary(state.settings.hasOwnReminders(list.id)),
summary = listSourceSummary(
state.settings.hasOwnReminders(list.id),
state.settings.persistenceFor(list.id),
),
position = if (last && !expanded) Position.Bottom else Position.Middle,
leading = {
Icon(Icons.Rounded.Circle, contentDescription = null, tint = pastelize(list.color, dark))
@@ -298,6 +304,15 @@ internal fun NotificationsScreen(
position = Position.Middle,
onClick = { overrideTarget = OverrideTarget(list.id, allDay = true) },
)
GroupedRow(
title = stringResource(R.string.settings_persistent_reminders),
summary = listPersistenceSummary(
state.settings.persistenceFor(list.id),
state.settings.persistentReminders,
),
position = Position.Middle,
onClick = { persistenceTarget = list.id },
)
GroupedRow(
title = stringResource(R.string.settings_list_notifications),
summary = stringResource(R.string.settings_list_notifications_hint),
@@ -385,6 +400,18 @@ internal fun NotificationsScreen(
onDismiss = { overrideTarget = null },
)
}
persistenceTarget?.let { listId ->
OptionPicker(
title = state.lists.firstOrNull { it.id == listId }?.name
?: stringResource(R.string.settings_persistent_reminders),
options = ListPersistence.entries,
selected = state.settings.persistenceFor(listId),
label = { listPersistenceLabel(it) },
summary = { listPersistenceOptionSummary(it, state.settings.persistentReminders) },
onSelect = { viewModel.setListPersistence(listId, it) },
onDismiss = { persistenceTarget = null },
)
}
}
/** "10 minutes", "1 hour". */
@@ -420,18 +447,51 @@ private fun withOwnCount(label: String, own: Int): String =
if (own == 0) label
else label + "\n" + pluralStringResource(R.plurals.settings_default_reminder_lists_own, own, own)
/** Whether a list follows the general default; an override stands out in the primary colour. */
/**
* Whether a list follows the general default, plus its persistence when it sets
* its own; an override stands out in the primary colour.
*/
@Composable
private fun listSourceSummary(own: Boolean): AnnotatedString =
if (own) {
buildAnnotatedString {
withStyle(SpanStyle(color = MaterialTheme.colorScheme.primary)) {
append(stringResource(R.string.settings_list_reminder_own))
}
private fun listSourceSummary(own: Boolean, persistence: ListPersistence): AnnotatedString {
val ownText = stringResource(R.string.settings_list_reminder_own)
val followsText = stringResource(R.string.settings_list_reminder_follows)
val persistenceText = if (persistence == ListPersistence.INHERIT) null else listPersistenceLabel(persistence)
val primary = MaterialTheme.colorScheme.primary
return buildAnnotatedString {
if (own) withStyle(SpanStyle(color = primary)) { append(ownText) } else append(followsText)
if (persistenceText != null) {
append("\n")
withStyle(SpanStyle(color = primary)) { append(persistenceText) }
}
} else {
AnnotatedString(stringResource(R.string.settings_list_reminder_follows))
}
}
@Composable
private fun listPersistenceLabel(persistence: ListPersistence): String = stringResource(
when (persistence) {
ListPersistence.INHERIT -> R.string.reminder_use_default
ListPersistence.ALWAYS -> R.string.settings_list_persistent_on
ListPersistence.NEVER -> R.string.settings_list_persistent_off
},
)
/** A list's persistence row: its own choice, or the app setting it follows. */
@Composable
private fun listPersistenceSummary(persistence: ListPersistence, appWide: Boolean): String =
if (persistence == ListPersistence.INHERIT) {
stringResource(R.string.settings_list_reminder_inherits, listPersistenceLabel(appWide.asPersistence()))
} else {
listPersistenceLabel(persistence)
}
@Composable
private fun listPersistenceOptionSummary(persistence: ListPersistence, appWide: Boolean): String = when (persistence) {
ListPersistence.INHERIT -> listPersistenceLabel(appWide.asPersistence())
ListPersistence.ALWAYS -> stringResource(R.string.settings_list_persistent_on_summary)
ListPersistence.NEVER -> stringResource(R.string.settings_list_persistent_off_summary)
}
private fun Boolean.asPersistence(): ListPersistence = if (this) ListPersistence.ALWAYS else ListPersistence.NEVER
/** Row summary for a list: its override, or the inherited global default. */
@Composable
@@ -6,6 +6,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import de.jeanlucmakiola.agendula.data.appname.LauncherName
import de.jeanlucmakiola.agendula.data.appname.LauncherNameManager
import de.jeanlucmakiola.agendula.data.di.IoDispatcher
import de.jeanlucmakiola.agendula.data.prefs.ListPersistence
import de.jeanlucmakiola.agendula.data.prefs.ReminderDescription
import de.jeanlucmakiola.agendula.data.prefs.Settings
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
@@ -308,6 +309,11 @@ class SettingsViewModel @Inject constructor(
resyncReminders()
}
fun setListPersistence(listId: Long, persistence: ListPersistence) = viewModelScope.launch {
prefs.setListPersistence(listId, persistence)
restyleReminders()
}
/** Every reminder setting moves alarms that are already armed. */
private suspend fun resyncReminders() {
runCatching { reminderScheduler.sync() }
@@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import de.jeanlucmakiola.agendula.data.prefs.DEFAULT_COLLAPSED_SECTIONS
import de.jeanlucmakiola.agendula.data.prefs.ListPersistence
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
import de.jeanlucmakiola.agendula.data.sync.RemoteLists
import de.jeanlucmakiola.agendula.data.tasks.TasksRepository
@@ -361,7 +362,10 @@ class TaskListViewModel @Inject constructor(
fun deleteList(listId: Long, accountId: Long?) = viewModelScope.launch {
if (accountId == null) {
runCatching { repository.deleteList(listId) }
.onSuccess { _listDeleted.value = true }
.onSuccess {
forgetListSettings(listId)
_listDeleted.value = true
}
.onFailure { _listWriteFailure.value = ListWriteFailure.DELETE }
return@launch
}
@@ -373,7 +377,16 @@ class TaskListViewModel @Inject constructor(
val outcome = runCatching { remoteLists.delete(listId) }
.getOrElse { RemoteLists.Outcome.Unreachable }
val failure = outcome.asFailure(ListWriteFailure.DELETE)
if (failure == null) _listDeleted.value = true else _listWriteFailure.value = failure
if (failure == null) {
forgetListSettings(listId)
_listDeleted.value = true
} else {
_listWriteFailure.value = failure
}
}
private suspend fun forgetListSettings(listId: Long) {
runCatching { settingsPrefs.setListPersistence(listId, ListPersistence.INHERIT) }
}
/** Inline "add subtask" from an expanded list group — files it under [parent]. */
+4
View File
@@ -484,6 +484,10 @@
<string name="settings_snooze_duration">Snooze duration</string>
<string name="settings_persistent_reminders">Persistent reminders</string>
<string name="settings_persistent_reminders_hint">A swiped-away reminder comes back until you mark the task done or snooze it</string>
<string name="settings_list_persistent_on">Persistent</string>
<string name="settings_list_persistent_off">Not persistent</string>
<string name="settings_list_persistent_on_summary">Swiped-away reminders come back</string>
<string name="settings_list_persistent_off_summary">Swiped-away reminders stay gone</string>
<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>
@@ -0,0 +1,59 @@
package de.jeanlucmakiola.agendula.data.prefs
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test
class ListPersistenceTest {
private val overrides = mapOf(1L to true, 2L to false)
@Test
fun `a list without an override follows the app setting`() {
assertThat(Settings(persistentReminders = true, perListPersistentReminders = overrides).persistentRemindersFor(3)).isTrue()
assertThat(Settings(persistentReminders = false, perListPersistentReminders = overrides).persistentRemindersFor(3)).isFalse()
}
@Test
fun `a list override wins over the app setting`() {
val off = Settings(persistentReminders = false, perListPersistentReminders = overrides)
val on = Settings(persistentReminders = true, perListPersistentReminders = overrides)
assertThat(off.persistentRemindersFor(1)).isTrue()
assertThat(on.persistentRemindersFor(2)).isFalse()
}
@Test
fun `nothing is persistent with reminders off`() {
val settings = Settings(remindersEnabled = false, persistentReminders = true, perListPersistentReminders = overrides)
assertThat(settings.persistentRemindersFor(1)).isFalse()
assertThat(settings.persistentRemindersFor(3)).isFalse()
assertThat(settings.anyPersistentReminders).isFalse()
}
@Test
fun `any persistent counts an always list with the app setting off`() {
assertThat(Settings(perListPersistentReminders = mapOf(1L to true)).anyPersistentReminders).isTrue()
assertThat(Settings(perListPersistentReminders = mapOf(2L to false)).anyPersistentReminders).isFalse()
assertThat(Settings(persistentReminders = true, perListPersistentReminders = mapOf(2L to false)).anyPersistentReminders).isTrue()
}
@Test
fun `persistence names the override`() {
val settings = Settings(perListPersistentReminders = overrides)
assertThat(settings.persistenceFor(1)).isEqualTo(ListPersistence.ALWAYS)
assertThat(settings.persistenceFor(2)).isEqualTo(ListPersistence.NEVER)
assertThat(settings.persistenceFor(3)).isEqualTo(ListPersistence.INHERIT)
}
@Test
fun `stored overrides are read for the current store only`() {
val stored = setOf("OWN:1=ALWAYS", "OWN:2=NEVER", "EXTERNAL:1=NEVER", "EXTERNAL:5=ALWAYS")
assertThat(parseListPersistence(stored, "OWN")).isEqualTo(mapOf(1L to true, 2L to false))
assertThat(parseListPersistence(stored, "EXTERNAL")).isEqualTo(mapOf(1L to false, 5L to true))
}
@Test
fun `malformed entries are skipped`() {
val stored = setOf("OWN:x=ALWAYS", "OWN:3=MAYBE", "OWN:4", "OWN4=ALWAYS", "OWNER:6=ALWAYS", "OWN:7=ALWAYS")
assertThat(parseListPersistence(stored, "OWN")).isEqualTo(mapOf(7L to true))
}
}