Persistent reminders: restyle in place, keep records on failed reads, changelog (#100)
This commit is contained in:
1 parent
a67d43edda
commit
f999fb076c
6 files changed
+78
-42
No files matched your search
@@ -7,6 +7,11 @@ All notable changes to this project are documented here. The format follows
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### 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).
|
||||
|
||||
## [1.3.2] - 2026-11-13
|
||||
|
||||
### Added
|
||||
|
||||
@@ -12,6 +12,7 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
@@ -55,15 +56,16 @@ class BootReceiver : BroadcastReceiver() {
|
||||
val pending = goAsync()
|
||||
scope.launch {
|
||||
try {
|
||||
runCatching { scheduler.sync(afterReboot = afterReboot) }
|
||||
if (afterReboot) runCatching { snoozeScheduler.rearm() }
|
||||
// Before the sync, so a missed alarm it fires replaces an older restored occurrence, not the reverse.
|
||||
if (afterReboot) {
|
||||
runCatching {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
startupGate.awaitReady()
|
||||
notifier.restorePersistent(settings)
|
||||
withContext(Dispatchers.IO) { notifier.restorePersistent(settings) }
|
||||
}
|
||||
}
|
||||
runCatching { scheduler.sync(afterReboot = afterReboot) }
|
||||
if (afterReboot) runCatching { snoozeScheduler.rearm() }
|
||||
} finally {
|
||||
pending.finish()
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import androidx.core.net.toUri
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import de.jeanlucmakiola.agendula.data.prefs.Settings
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.data.tasks.StartupGate
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TaskQuery
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TasksDataSource
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
@@ -30,6 +31,7 @@ class DueReminderReceiver : BroadcastReceiver() {
|
||||
@Inject lateinit var notifier: TaskNotifier
|
||||
@Inject lateinit var settingsPrefs: SettingsPrefs
|
||||
@Inject lateinit var scheduler: ReminderScheduler
|
||||
@Inject lateinit var startupGate: StartupGate
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
@@ -46,7 +48,10 @@ class DueReminderReceiver : BroadcastReceiver() {
|
||||
}
|
||||
val settings = settingsPrefs.settings.first()
|
||||
if (!settings.remindersEnabled) return@launch
|
||||
runCatching { notifier.postIfOpen(dataSource, taskId, occurrence, settings) }
|
||||
runCatching {
|
||||
startupGate.awaitReady()
|
||||
notifier.postIfOpen(dataSource, taskId, occurrence, settings)
|
||||
}
|
||||
} finally {
|
||||
pending.finish()
|
||||
}
|
||||
@@ -115,7 +120,9 @@ internal fun reminderIsStale(occurrenceStart: Long?, current: Task?, missingMean
|
||||
* Posts the reminder for [taskId]'s occurrence if it is still open, then checks
|
||||
* once more: a completion landing between the read and the post may already
|
||||
* have had its withdrawal pass, which ran before there was anything to withdraw.
|
||||
* With [exact], a lookup that fell back to another occurrence posts nothing.
|
||||
* With [exact], a lookup that fell back to another occurrence posts nothing. A
|
||||
* persistent reminder that won't come back is forgotten, unless the store just
|
||||
* failed to find it.
|
||||
*/
|
||||
internal fun TaskNotifier.postIfOpen(
|
||||
dataSource: TasksDataSource,
|
||||
@@ -127,7 +134,11 @@ internal fun TaskNotifier.postIfOpen(
|
||||
postedAt: Long? = null,
|
||||
) {
|
||||
val task = dataSource.occurrence(taskId, occurrenceStart)
|
||||
if (task == null || task.isClosed || (exact && !task.isOccurrence(occurrenceStart))) {
|
||||
if (task == null) {
|
||||
if (missingMeansGone()) forget(taskId)
|
||||
return
|
||||
}
|
||||
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart))) {
|
||||
forget(taskId)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.jeanlucmakiola.agendula.data.reminders
|
||||
|
||||
import android.Manifest
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationChannelGroup
|
||||
import android.app.NotificationManager
|
||||
@@ -26,6 +27,7 @@ import de.jeanlucmakiola.agendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.agendula.data.di.ApplicationScope
|
||||
import de.jeanlucmakiola.agendula.data.tasks.ProviderResolver
|
||||
import de.jeanlucmakiola.agendula.data.tasks.StartupGate
|
||||
import de.jeanlucmakiola.agendula.data.tasks.StorageMode
|
||||
import de.jeanlucmakiola.agendula.data.tasks.TasksDataSource
|
||||
import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.calendarDate
|
||||
@@ -129,10 +131,14 @@ class TaskNotifier @Inject constructor(
|
||||
)
|
||||
.build()
|
||||
NotificationManagerCompat.from(context).notify(task.taskId.toString(), NOTIFICATION_ID, notification)
|
||||
remember(task.taskId, occurrence, shownAt, persistent)
|
||||
}
|
||||
|
||||
private fun remember(taskId: Long, occurrence: Long, postedAt: Long, persistent: Boolean) {
|
||||
if (persistent) {
|
||||
persistentPrefs.edit { putString(persistentKey(currentStore, task.taskId), encodePersistent(occurrence, shownAt)) }
|
||||
persistentPrefs.edit { putString(persistentKey(currentStore, taskId), encodePersistent(occurrence, postedAt)) }
|
||||
} else {
|
||||
forget(task.taskId)
|
||||
forget(taskId)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,11 +159,15 @@ class TaskNotifier @Inject constructor(
|
||||
|
||||
/** Drops [taskId] from the persistent reminders remembered for [store], the current one by default. */
|
||||
fun forget(taskId: Long, store: String? = null) {
|
||||
persistentPrefs.edit { remove(persistentKey(store ?: currentStore, taskId)) }
|
||||
val key = persistentKey(store ?: currentStore, taskId)
|
||||
if (persistentPrefs.contains(key)) persistentPrefs.edit { remove(key) }
|
||||
}
|
||||
|
||||
fun isCurrentStore(store: String?): Boolean = store == currentStore
|
||||
|
||||
/** Whether a task the store can't find is really gone; an external provider that fails a read finds nothing too. */
|
||||
fun missingMeansGone(): Boolean = resolver.mode() == StorageMode.OWN
|
||||
|
||||
/**
|
||||
* Takes down every posted reminder [isStale] says is done with — completed,
|
||||
* cancelled or deleted since it went off, here or on another device. The
|
||||
@@ -167,25 +177,22 @@ class TaskNotifier @Inject constructor(
|
||||
postedReminders().forEach { if (isStale(it.taskId, it.occurrence)) cancel(it.taskId) }
|
||||
}
|
||||
|
||||
/** Re-posts every shown reminder without alerting, so a change to [Settings.persistentReminders] reaches it. */
|
||||
fun repostActive(settings: Settings) {
|
||||
scope.launch {
|
||||
runCatching {
|
||||
startupGate.awaitReady()
|
||||
postedReminders().forEach { posted ->
|
||||
runCatching {
|
||||
postIfOpen(
|
||||
dataSource,
|
||||
posted.taskId,
|
||||
posted.occurrence ?: ScheduledReminder.NO_OCCURRENCE,
|
||||
settings,
|
||||
alert = false,
|
||||
exact = true,
|
||||
postedAt = posted.postedAt,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Turns the shown reminders persistent or back, in place: the update doesn't
|
||||
* alert again and keeps each reminder's content and time.
|
||||
*/
|
||||
@Synchronized
|
||||
fun restyleActive(persistent: Boolean) {
|
||||
postedReminders().forEach { posted ->
|
||||
val occurrence = posted.occurrence ?: ScheduledReminder.NO_OCCURRENCE
|
||||
val notification = Notification.Builder.recoverBuilder(context, posted.notification)
|
||||
.setAutoCancel(!persistent)
|
||||
.setOnlyAlertOnce(true)
|
||||
.setGroup(if (persistent) "$PERSISTENT_GROUP_PREFIX${posted.taskId}" else null)
|
||||
.setDeleteIntent(if (persistent) dismissedIntent(posted.taskId, occurrence, posted.postedAt) else null)
|
||||
.build()
|
||||
manager.notify(posted.taskId.toString(), NOTIFICATION_ID, notification)
|
||||
remember(posted.taskId, occurrence, posted.postedAt, persistent)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,7 +219,7 @@ class TaskNotifier @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private class Posted(val taskId: Long, val occurrence: Long?, val postedAt: Long)
|
||||
private class Posted(val taskId: 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> {
|
||||
@@ -223,27 +230,27 @@ 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(taskId, occurrence, posted.notification.`when`, posted.notification)
|
||||
}
|
||||
}
|
||||
|
||||
private fun dismissedIntent(taskId: Long, occurrence: Long, postedAt: Long): PendingIntent =
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
taskId.toInt(),
|
||||
ReminderActionReceiver.dismissedIntent(context, taskId, occurrence, currentStore, postedAt),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
broadcast(taskId, ReminderActionReceiver.dismissedIntent(context, taskId, occurrence, currentStore, postedAt))
|
||||
|
||||
private fun actionIntent(task: Task, action: String): PendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
task.taskId.toInt(),
|
||||
private fun actionIntent(task: Task, action: String): PendingIntent = broadcast(
|
||||
task.taskId,
|
||||
ReminderActionReceiver.intent(
|
||||
context,
|
||||
action,
|
||||
task.taskId,
|
||||
task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE,
|
||||
),
|
||||
)
|
||||
|
||||
private fun broadcast(taskId: Long, intent: Intent): PendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
taskId.toInt(),
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
|
||||
@@ -255,7 +262,7 @@ class TaskNotifier @Inject constructor(
|
||||
*/
|
||||
fun ensureListChannel(listId: Long, listName: String?): String {
|
||||
ensureGroup()
|
||||
val id = "$LIST_CHANNEL_PREFIX${resolver.mode().name.lowercase()}_$listId"
|
||||
val id = "$LIST_CHANNEL_PREFIX${currentStore}_$listId"
|
||||
val importance = manager.getNotificationChannel(id)?.importance ?: legacyImportance()
|
||||
manager.createNotificationChannel(
|
||||
NotificationChannel(
|
||||
@@ -290,7 +297,7 @@ class TaskNotifier @Inject constructor(
|
||||
startupGate.awaitReady()
|
||||
val lists = dataSource.taskLists()
|
||||
lists.forEach { ensureListChannel(it.id, it.name) }
|
||||
val prefix = "$LIST_CHANNEL_PREFIX${resolver.mode().name.lowercase()}_"
|
||||
val prefix = "$LIST_CHANNEL_PREFIX${currentStore}_"
|
||||
val live = lists.map { "$prefix${it.id}" }.toSet()
|
||||
manager.notificationChannels
|
||||
.filter { it.id.startsWith(prefix) && it.id !in live }
|
||||
|
||||
@@ -216,6 +216,7 @@ internal fun NotificationsScreen(
|
||||
checked = state.settings.persistentReminders,
|
||||
onCheckedChange = viewModel::setPersistentReminders,
|
||||
position = Position.Middle,
|
||||
enabled = state.settings.remindersEnabled,
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_snooze_duration),
|
||||
|
||||
@@ -37,6 +37,8 @@ import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.time.DayOfWeek
|
||||
import javax.inject.Inject
|
||||
@@ -253,6 +255,8 @@ class SettingsViewModel @Inject constructor(
|
||||
prefs.setAllDayReminderMinuteOfDay(minuteOfDay)
|
||||
resyncReminders()
|
||||
}
|
||||
private val restyleLock = Mutex()
|
||||
|
||||
fun setRemindersEnabled(enabled: Boolean) = viewModelScope.launch {
|
||||
prefs.setRemindersEnabled(enabled)
|
||||
resyncReminders()
|
||||
@@ -264,7 +268,13 @@ class SettingsViewModel @Inject constructor(
|
||||
fun setReminderDescription(pref: ReminderDescription) = viewModelScope.launch { prefs.setReminderDescription(pref) }
|
||||
fun setPersistentReminders(enabled: Boolean) = viewModelScope.launch {
|
||||
prefs.setPersistentReminders(enabled)
|
||||
taskNotifier.repostActive(prefs.settings.first())
|
||||
// Under the lock the latest setting wins, however fast it is flipped.
|
||||
restyleLock.withLock {
|
||||
val settings = prefs.settings.first()
|
||||
withContext(io) {
|
||||
runCatching { taskNotifier.restyleActive(settings.remindersEnabled && settings.persistentReminders) }
|
||||
}
|
||||
}
|
||||
}
|
||||
fun setShowAddSubtaskRow(show: Boolean) = viewModelScope.launch { prefs.setShowAddSubtaskRow(show) }
|
||||
fun setBottomAddBar(enabled: Boolean) = viewModelScope.launch { prefs.setBottomAddBar(enabled) }
|
||||
|
||||
Reference in new issue
Block a user