Persistent reminders: second per-list review fixes (#100)
This commit is contained in:
1 parent
bae04e1839
commit
8138ae4454
5 files changed
+37
-39
No files matched your search
@@ -244,31 +244,20 @@ class SettingsPrefs @Inject constructor(
|
||||
|
||||
/** 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 prefix = listPersistencePrefix(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}"
|
||||
}
|
||||
|
||||
/** Drops the per-list settings of a list deleted from the current store. */
|
||||
/**
|
||||
* Drops the store-bound settings of a list deleted from the current store.
|
||||
* The reminder overrides are keyed by bare id, shared by every store, so they stay.
|
||||
*/
|
||||
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[LIST_PERSISTENT_REMINDERS] = p[LIST_PERSISTENT_REMINDERS].orEmpty()
|
||||
.filterNotTo(HashSet()) { it.startsWith(listPersistencePrefix(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 }
|
||||
@@ -440,6 +429,8 @@ class SettingsPrefs @Inject constructor(
|
||||
*/
|
||||
private val reminderCodec = ReminderOverrideCodec.DEFAULT
|
||||
|
||||
private fun listPersistencePrefix(mode: String, listId: Long) = "$mode:$listId="
|
||||
|
||||
/** `MODE:listId=ALWAYS|NEVER` entries, [mode]'s only. */
|
||||
internal fun parseListPersistence(entries: Set<String>, mode: String): Map<Long, ListPersistence> =
|
||||
entries.mapNotNull { entry ->
|
||||
|
||||
@@ -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 or its list no longer keeps reminders persistent. A persistent reminder that won't come back is forgotten; one
|
||||
* later due since (or, see [TaskNotifier.postDue], its list stopped being 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,8 +143,7 @@ internal suspend fun TaskNotifier.postIfOpen(
|
||||
if (missingMeansGone()) forget(taskId, occurrence = occurrenceStart) else missed(taskId, occurrenceStart)
|
||||
return
|
||||
}
|
||||
val dropRestore = restoring != null && (task.postponedSince(restoring) || !settings.persistentRemindersFor(task.listId))
|
||||
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart)) || dropRestore) {
|
||||
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart)) || (restoring != null && task.postponedSince(restoring))) {
|
||||
forget(taskId, occurrence = occurrenceStart)
|
||||
if (task.taskId != taskId) forget(task.taskId, occurrence = occurrenceStart)
|
||||
return
|
||||
|
||||
+2
-1
@@ -77,7 +77,8 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
startupGate.awaitReady()
|
||||
val listId = intent.getLongExtra(EXTRA_LIST_ID, -1L).takeIf { it > 0L }
|
||||
val persistent = listId != null && settings.persistentRemindersFor(listId)
|
||||
// An intent without a list predates it; the re-post checks the task's list anyway.
|
||||
val persistent = listId?.let(settings::persistentRemindersFor) ?: settings.anyPersistentReminders
|
||||
if (persistent && notifier.isCurrentStore(store)) {
|
||||
notifier.repostRemembered(taskId, occurrence, settings)
|
||||
} else {
|
||||
|
||||
@@ -33,6 +33,7 @@ import de.jeanlucmakiola.agendula.data.tasks.TasksDataSource
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.calendarDate
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -95,7 +96,7 @@ class TaskNotifier @Inject constructor(
|
||||
if (postedReminders().any { it.taskId == task.taskId }) return
|
||||
records.find(restoring.store, restoring.taskId, restoring.occurrence) ?: return
|
||||
}
|
||||
// The latest settings, not the caller's snapshot: a list may have just stopped being persistent.
|
||||
// The one place persistence is decided, on the latest settings: a list may have just changed.
|
||||
val persistent = settingsPrefs.settings.first().persistentRemindersFor(task.listId)
|
||||
if (restoring != null && !persistent) {
|
||||
forget(restoring.taskId, restoring.store, restoring.occurrence)
|
||||
@@ -229,19 +230,26 @@ class TaskNotifier @Inject constructor(
|
||||
*/
|
||||
suspend fun restyleActive() {
|
||||
startupGate.awaitReady()
|
||||
postLock.withLock { restyleLocked() }
|
||||
// Read before the lock, so posts don't wait on the store.
|
||||
val taskLists = postedReminders().filter { it.restylable }.associate { posted ->
|
||||
posted.taskId to try {
|
||||
dataSource.occurrence(posted.taskId, posted.occurrence!!)?.listId
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
postLock.withLock { restyleLocked(taskLists) }
|
||||
}
|
||||
|
||||
private suspend fun restyleLocked() {
|
||||
private suspend fun restyleLocked(taskLists: Map<Long, Long?>) {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
postedReminders().forEach { posted ->
|
||||
postedReminders().filter { it.restylable }.forEach { posted ->
|
||||
val occurrence = posted.occurrence ?: return@forEach
|
||||
val listId = runCatching { dataSource.occurrence(posted.taskId, occurrence)?.listId }.getOrNull()
|
||||
?: posted.listId ?: return@forEach
|
||||
val listId = taskLists[posted.taskId] ?: 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 }
|
||||
val due = posted.notification.extras.getLong(EXTRA_DUE).takeIf { it != NO_DUE }
|
||||
val notification = Notification.Builder.recoverBuilder(context, posted.notification)
|
||||
.setAutoCancel(!persistent)
|
||||
.setOnlyAlertOnce(true)
|
||||
@@ -305,7 +313,10 @@ class TaskNotifier @Inject constructor(
|
||||
val occurrence: Long?,
|
||||
val postedAt: Long,
|
||||
val notification: Notification,
|
||||
)
|
||||
) {
|
||||
/** Posted with occurrence and due, so a restyle can keep both. */
|
||||
val restylable: Boolean get() = occurrence != null && notification.extras.containsKey(EXTRA_DUE)
|
||||
}
|
||||
|
||||
/** The current store's posted reminders; another store's ids name other tasks. */
|
||||
private fun postedReminders(): List<Posted> {
|
||||
@@ -390,7 +401,6 @@ 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)
|
||||
|
||||
@@ -464,12 +464,9 @@ 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 {
|
||||
when {
|
||||
own -> withStyle(SpanStyle(color = primary)) { append(ownText) }
|
||||
persistenceText == null -> append(followsText)
|
||||
}
|
||||
if (own) withStyle(SpanStyle(color = primary)) { append(ownText) } else append(followsText)
|
||||
if (persistenceText != null) {
|
||||
if (own) append("\n")
|
||||
append("\n")
|
||||
withStyle(SpanStyle(color = primary)) { append(persistenceText) }
|
||||
}
|
||||
}
|
||||
@@ -502,7 +499,7 @@ private fun listPersistenceOptionSummary(persistence: ListPersistence, appWide:
|
||||
|
||||
@Composable
|
||||
private fun appWideLabel(persistent: Boolean): String =
|
||||
stringResource(if (persistent) R.string.settings_list_persistent_on else R.string.settings_list_persistent_off)
|
||||
listPersistenceLabel(if (persistent) ListPersistence.ALWAYS else ListPersistence.NEVER)
|
||||
|
||||
/** Row summary for a list: its override, or the inherited global default. */
|
||||
@Composable
|
||||
|
||||
Reference in new issue
Block a user