Persistent reminders: per-list review fixes (#100)
This commit is contained in:
1 parent
953ded977b
commit
bae04e1839
7 files changed
+95
-43
No files matched your search
@@ -112,8 +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(),
|
||||
/** Lists of the current store that override [persistentReminders]; [ListPersistence.INHERIT] is never stored. */
|
||||
val perListPersistentReminders: Map<Long, ListPersistence> = emptyMap(),
|
||||
val timeFormat: TimeFormatPref = TimeFormatPref.AUTO,
|
||||
/** The first day of the week; `null` follows the locale. */
|
||||
val weekStart: DayOfWeek? = null,
|
||||
@@ -154,19 +154,19 @@ 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
|
||||
}
|
||||
fun persistenceFor(listId: Long): ListPersistence = perListPersistentReminders[listId] ?: ListPersistence.INHERIT
|
||||
|
||||
/** 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)
|
||||
fun persistentRemindersFor(listId: Long): Boolean = remindersEnabled && when (persistenceFor(listId)) {
|
||||
ListPersistence.INHERIT -> persistentReminders
|
||||
ListPersistence.ALWAYS -> true
|
||||
ListPersistence.NEVER -> false
|
||||
}
|
||||
|
||||
/** Whether any list's reminders can be persistent. */
|
||||
val anyPersistentReminders: Boolean
|
||||
get() = remindersEnabled && (persistentReminders || perListPersistentReminders.containsValue(true))
|
||||
get() = remindersEnabled &&
|
||||
(persistentReminders || perListPersistentReminders.containsValue(ListPersistence.ALWAYS))
|
||||
|
||||
/** Whether [listId] has its own default reminders for timed or all-day tasks. */
|
||||
fun hasOwnReminders(listId: Long): Boolean =
|
||||
@@ -249,6 +249,28 @@ class SettingsPrefs @Inject constructor(
|
||||
p[LIST_PERSISTENT_REMINDERS] = if (persistence == ListPersistence.INHERIT) rest else rest + "$prefix${persistence.name}"
|
||||
}
|
||||
|
||||
/** Drops the per-list settings of a list deleted from the current store. */
|
||||
suspend fun forgetList(listId: Long) = dataStore.edit { p ->
|
||||
val mode = modeOf(p).name
|
||||
p[LIST_PERSISTENT_REMINDERS] = p[LIST_PERSISTENT_REMINDERS].orEmpty().filterNotTo(HashSet()) { it.startsWith("$mode:$listId=") }
|
||||
p[SMART_LIST_HIDDEN] = p[SMART_LIST_HIDDEN].orEmpty() - "$mode:$listId"
|
||||
listOf(LIST_REMINDER_OVERRIDE, LIST_ALL_DAY_REMINDER_OVERRIDE).forEach { key ->
|
||||
val current = reminderCodec.parseLeads(p[key])
|
||||
if (listId in current) p[key] = reminderCodec.serializeLeads(current - listId)
|
||||
}
|
||||
}
|
||||
|
||||
/** Drops the persistence overrides of the current store's lists not in [live]. */
|
||||
suspend fun retainListPersistence(live: Set<Long>) = dataStore.edit { p ->
|
||||
val stored = p[LIST_PERSISTENT_REMINDERS] ?: return@edit
|
||||
val mode = modeOf(p).name
|
||||
val kept = stored.filterTo(HashSet()) { entry ->
|
||||
val id = entry.removePrefix("$mode:").takeIf { it != entry }?.substringBefore('=')?.toLongOrNull()
|
||||
id == null || id in live
|
||||
}
|
||||
if (kept.size != stored.size) p[LIST_PERSISTENT_REMINDERS] = kept
|
||||
}
|
||||
|
||||
suspend fun setSyncIntervalMinutes(minutes: Int) = dataStore.edit { it[SYNC_INTERVAL] = minutes }
|
||||
|
||||
suspend fun setPushEnabled(enabled: Boolean) = dataStore.edit { it[PUSH_ENABLED] = enabled }
|
||||
@@ -419,14 +441,14 @@ 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> =
|
||||
internal fun parseListPersistence(entries: Set<String>, mode: String): Map<Long, ListPersistence> =
|
||||
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
|
||||
ListPersistence.ALWAYS.name -> listId to ListPersistence.ALWAYS
|
||||
ListPersistence.NEVER.name -> listId to ListPersistence.NEVER
|
||||
else -> null
|
||||
}
|
||||
}.toMap()
|
||||
|
||||
+7
-5
@@ -76,8 +76,9 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
if (store == null) return@runCatching
|
||||
val settings = settingsPrefs.settings.first()
|
||||
startupGate.awaitReady()
|
||||
// Whether its list still keeps it persistent is up to the re-post, which reads the task.
|
||||
if (settings.anyPersistentReminders && notifier.isCurrentStore(store)) {
|
||||
val listId = intent.getLongExtra(EXTRA_LIST_ID, -1L).takeIf { it > 0L }
|
||||
val persistent = listId != null && settings.persistentRemindersFor(listId)
|
||||
if (persistent && notifier.isCurrentStore(store)) {
|
||||
notifier.repostRemembered(taskId, occurrence, settings)
|
||||
} else {
|
||||
notifier.forget(taskId, store, occurrence)
|
||||
@@ -106,6 +107,7 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
private const val EXTRA_TASK_ID = "de.jeanlucmakiola.agendula.extra.TASK_ID"
|
||||
private const val EXTRA_OCCURRENCE = "de.jeanlucmakiola.agendula.extra.OCCURRENCE_START"
|
||||
private const val EXTRA_STORE = "de.jeanlucmakiola.agendula.extra.STORE"
|
||||
private const val EXTRA_LIST_ID = "de.jeanlucmakiola.agendula.extra.LIST_ID"
|
||||
|
||||
/**
|
||||
* The data URI is what keeps two reminders' PendingIntents apart —
|
||||
@@ -119,8 +121,8 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
.putExtra(EXTRA_OCCURRENCE, occurrenceStart)
|
||||
.apply { if (store != null) putExtra(EXTRA_STORE, store) }
|
||||
|
||||
/** A persistent reminder's delete intent, naming the store its task id belongs to. */
|
||||
fun dismissedIntent(context: Context, taskId: Long, occurrenceStart: Long, store: String): Intent =
|
||||
intent(context, ACTION_DISMISSED, taskId, occurrenceStart, store)
|
||||
/** A persistent reminder's delete intent, naming the store its task id belongs to and its list. */
|
||||
fun dismissedIntent(context: Context, taskId: Long, occurrenceStart: Long, store: String, listId: Long): Intent =
|
||||
intent(context, ACTION_DISMISSED, taskId, occurrenceStart, store).putExtra(EXTRA_LIST_ID, listId)
|
||||
}
|
||||
}
|
||||
@@ -95,14 +95,19 @@ class TaskNotifier @Inject constructor(
|
||||
if (postedReminders().any { it.taskId == task.taskId }) return
|
||||
records.find(restoring.store, restoring.taskId, restoring.occurrence) ?: return
|
||||
}
|
||||
post(task, settings, alert, postedAt)
|
||||
// The latest settings, not the caller's snapshot: a list may have just stopped being persistent.
|
||||
val persistent = settingsPrefs.settings.first().persistentRemindersFor(task.listId)
|
||||
if (restoring != null && !persistent) {
|
||||
forget(restoring.taskId, restoring.store, restoring.occurrence)
|
||||
return
|
||||
}
|
||||
post(task, settings, alert, postedAt, persistent)
|
||||
}
|
||||
}
|
||||
|
||||
// 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.persistentRemindersFor(task.listId)
|
||||
private suspend fun post(task: Task, settings: Settings, alert: Boolean, postedAt: Long?, persistent: Boolean) {
|
||||
val occurrence = task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE
|
||||
val shownAt = postedAt ?: System.currentTimeMillis()
|
||||
val channelId = ensureListChannel(task.listId, task.listName)
|
||||
@@ -145,7 +150,7 @@ class TaskNotifier @Inject constructor(
|
||||
.setSilent(!alert)
|
||||
// A group of its own, so a silent re-post doesn't land in NotificationCompat's shared "silent" group.
|
||||
.apply { if (persistent) setGroup("$PERSISTENT_GROUP_PREFIX${task.taskId}") }
|
||||
.setDeleteIntent(if (persistent) dismissedIntent(task.taskId, occurrence) else null)
|
||||
.setDeleteIntent(if (persistent) dismissedIntent(task.taskId, occurrence, task.listId) else null)
|
||||
.setContentIntent(tapIntent)
|
||||
.addExtras(
|
||||
Bundle().apply {
|
||||
@@ -219,13 +224,21 @@ class TaskNotifier @Inject constructor(
|
||||
* alert again and keeps each reminder's content and time. One posted before
|
||||
* occurrence and due were recorded is left alone, as a re-post couldn't tell
|
||||
* its occurrence. Reads the settings under the lock, so the latest wins
|
||||
* however fast they flip.
|
||||
* however fast they flip. Each goes by its task's list now, like a re-post,
|
||||
* falling back to the list it was posted in.
|
||||
*/
|
||||
suspend fun restyleActive() = postLock.withLock {
|
||||
suspend fun restyleActive() {
|
||||
startupGate.awaitReady()
|
||||
postLock.withLock { restyleLocked() }
|
||||
}
|
||||
|
||||
private suspend fun restyleLocked() {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
postedReminders().forEach { posted ->
|
||||
val occurrence = posted.occurrence ?: return@forEach
|
||||
val persistent = settings.persistentRemindersFor(posted.listId ?: return@forEach)
|
||||
val listId = runCatching { dataSource.occurrence(posted.taskId, occurrence)?.listId }.getOrNull()
|
||||
?: posted.listId ?: return@forEach
|
||||
val persistent = settings.persistentRemindersFor(listId)
|
||||
val extras = posted.notification.extras
|
||||
if (!extras.containsKey(EXTRA_DUE)) return@forEach
|
||||
val due = extras.getLong(EXTRA_DUE).takeIf { it != NO_DUE }
|
||||
@@ -233,7 +246,7 @@ class TaskNotifier @Inject constructor(
|
||||
.setAutoCancel(!persistent)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setGroup(if (persistent) "$PERSISTENT_GROUP_PREFIX${posted.taskId}" else null)
|
||||
.setDeleteIntent(if (persistent) dismissedIntent(posted.taskId, occurrence) else null)
|
||||
.setDeleteIntent(if (persistent) dismissedIntent(posted.taskId, occurrence, listId) else null)
|
||||
.build()
|
||||
manager.notify(posted.taskId.toString(), NOTIFICATION_ID, notification)
|
||||
remember(posted.taskId, occurrence, due, posted.postedAt, persistent)
|
||||
@@ -308,8 +321,8 @@ class TaskNotifier @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun dismissedIntent(taskId: Long, occurrence: Long): PendingIntent =
|
||||
broadcast(taskId, ReminderActionReceiver.dismissedIntent(context, taskId, occurrence, currentStore))
|
||||
private fun dismissedIntent(taskId: Long, occurrence: Long, listId: Long): PendingIntent =
|
||||
broadcast(taskId, ReminderActionReceiver.dismissedIntent(context, taskId, occurrence, currentStore, listId))
|
||||
|
||||
private fun actionIntent(task: Task, action: String): PendingIntent = broadcast(
|
||||
task.taskId,
|
||||
@@ -377,6 +390,7 @@ class TaskNotifier @Inject constructor(
|
||||
manager.notificationChannels
|
||||
.filter { it.id.startsWith(prefix) && it.id !in live }
|
||||
.forEach { manager.deleteNotificationChannel(it.id) }
|
||||
if (lists.isNotEmpty()) settingsPrefs.retainListPersistence(lists.mapTo(HashSet()) { it.id })
|
||||
manager.getNotificationChannel(LEGACY_CHANNEL_ID)?.let { legacy ->
|
||||
channelPrefs.edit { putInt(KEY_LEGACY_IMPORTANCE, legacy.importance) }
|
||||
manager.deleteNotificationChannel(LEGACY_CHANNEL_ID)
|
||||
|
||||
@@ -238,6 +238,11 @@ internal fun NotificationsScreen(
|
||||
title = stringResource(R.string.settings_list_reminders_title),
|
||||
summary = listOfNotNull(
|
||||
if (ownAny > 0) pluralStringResource(R.plurals.settings_list_reminders_own_count, ownAny, ownAny) else null,
|
||||
if (ownPersistence > 0) {
|
||||
pluralStringResource(R.plurals.settings_list_persistent_own_count, ownPersistence, ownPersistence)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
stringResource(R.string.settings_list_reminders_hint),
|
||||
).joinToString("\n"),
|
||||
position = if (perListExpanded) Position.Top else Position.Alone,
|
||||
@@ -311,7 +316,8 @@ internal fun NotificationsScreen(
|
||||
state.settings.persistentReminders,
|
||||
),
|
||||
position = Position.Middle,
|
||||
onClick = { persistenceTarget = list.id },
|
||||
dimmed = !state.settings.remindersEnabled,
|
||||
onClick = if (state.settings.remindersEnabled) ({ persistenceTarget = list.id }) else null,
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_list_notifications),
|
||||
@@ -458,9 +464,12 @@ private fun listSourceSummary(own: Boolean, persistence: ListPersistence): Annot
|
||||
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)
|
||||
when {
|
||||
own -> withStyle(SpanStyle(color = primary)) { append(ownText) }
|
||||
persistenceText == null -> append(followsText)
|
||||
}
|
||||
if (persistenceText != null) {
|
||||
append("\n")
|
||||
if (own) append("\n")
|
||||
withStyle(SpanStyle(color = primary)) { append(persistenceText) }
|
||||
}
|
||||
}
|
||||
@@ -479,19 +488,21 @@ private fun listPersistenceLabel(persistence: ListPersistence): String = stringR
|
||||
@Composable
|
||||
private fun listPersistenceSummary(persistence: ListPersistence, appWide: Boolean): String =
|
||||
if (persistence == ListPersistence.INHERIT) {
|
||||
stringResource(R.string.settings_list_reminder_inherits, listPersistenceLabel(appWide.asPersistence()))
|
||||
stringResource(R.string.settings_list_reminder_inherits, appWideLabel(appWide))
|
||||
} else {
|
||||
listPersistenceLabel(persistence)
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun listPersistenceOptionSummary(persistence: ListPersistence, appWide: Boolean): String = when (persistence) {
|
||||
ListPersistence.INHERIT -> listPersistenceLabel(appWide.asPersistence())
|
||||
ListPersistence.INHERIT -> appWideLabel(appWide)
|
||||
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
|
||||
@Composable
|
||||
private fun appWideLabel(persistent: Boolean): String =
|
||||
stringResource(if (persistent) R.string.settings_list_persistent_on else R.string.settings_list_persistent_off)
|
||||
|
||||
/** Row summary for a list: its override, or the inherited global default. */
|
||||
@Composable
|
||||
|
||||
@@ -4,7 +4,6 @@ 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
|
||||
@@ -386,7 +385,7 @@ class TaskListViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private suspend fun forgetListSettings(listId: Long) {
|
||||
runCatching { settingsPrefs.setListPersistence(listId, ListPersistence.INHERIT) }
|
||||
runCatching { settingsPrefs.forgetList(listId) }
|
||||
}
|
||||
|
||||
/** Inline "add subtask" from an expanded list group — files it under [parent]. */
|
||||
|
||||
@@ -488,6 +488,10 @@
|
||||
<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>
|
||||
<plurals name="settings_list_persistent_own_count">
|
||||
<item quantity="one">%1$d list sets its own persistence</item>
|
||||
<item quantity="other">%1$d lists set their own persistence</item>
|
||||
</plurals>
|
||||
<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>
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.junit.jupiter.api.Test
|
||||
|
||||
class ListPersistenceTest {
|
||||
|
||||
private val overrides = mapOf(1L to true, 2L to false)
|
||||
private val overrides = mapOf(1L to ListPersistence.ALWAYS, 2L to ListPersistence.NEVER)
|
||||
|
||||
@Test
|
||||
fun `a list without an override follows the app setting`() {
|
||||
@@ -31,9 +31,9 @@ class ListPersistenceTest {
|
||||
|
||||
@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()
|
||||
assertThat(Settings(perListPersistentReminders = mapOf(1L to ListPersistence.ALWAYS)).anyPersistentReminders).isTrue()
|
||||
assertThat(Settings(perListPersistentReminders = mapOf(2L to ListPersistence.NEVER)).anyPersistentReminders).isFalse()
|
||||
assertThat(Settings(persistentReminders = true, perListPersistentReminders = mapOf(2L to ListPersistence.NEVER)).anyPersistentReminders).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -47,13 +47,13 @@ class ListPersistenceTest {
|
||||
@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))
|
||||
assertThat(parseListPersistence(stored, "OWN")).isEqualTo(overrides)
|
||||
assertThat(parseListPersistence(stored, "EXTERNAL")).isEqualTo(mapOf(1L to ListPersistence.NEVER, 5L to ListPersistence.ALWAYS))
|
||||
}
|
||||
|
||||
@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))
|
||||
assertThat(parseListPersistence(stored, "OWN")).isEqualTo(mapOf(7L to ListPersistence.ALWAYS))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user