Add persistent reminders setting (#100)
This commit is contained in:
1 parent
1463b08d6f
commit
5c619b8de9
7 files changed
+77
-11
No files matched your search
@@ -107,6 +107,8 @@ data class Settings(
|
||||
/** How long a reminder's "Snooze" action puts it off. */
|
||||
val snoozeMinutes: Int = DEFAULT_SNOOZE_MINUTES,
|
||||
val reminderDescription: ReminderDescription = ReminderDescription.SHORT,
|
||||
/** A swiped-away reminder comes straight back until the task is done or snoozed. */
|
||||
val persistentReminders: Boolean = false,
|
||||
val timeFormat: TimeFormatPref = TimeFormatPref.AUTO,
|
||||
/** The first day of the week; `null` follows the locale. */
|
||||
val weekStart: DayOfWeek? = null,
|
||||
@@ -186,6 +188,7 @@ class SettingsPrefs @Inject constructor(
|
||||
reminderDescription = p[REMINDER_DESCRIPTION]
|
||||
?.let { runCatching { ReminderDescription.valueOf(it) }.getOrNull() }
|
||||
?: ReminderDescription.SHORT,
|
||||
persistentReminders = p[PERSISTENT_REMINDERS] ?: false,
|
||||
timeFormat = p[TIME_FORMAT]?.let { runCatching { TimeFormatPref.valueOf(it) }.getOrNull() }
|
||||
?: TimeFormatPref.AUTO,
|
||||
autofocusTitle = p[AUTOFOCUS_TITLE] ?: true,
|
||||
@@ -217,6 +220,8 @@ class SettingsPrefs @Inject constructor(
|
||||
suspend fun setReminderDescription(pref: ReminderDescription) =
|
||||
dataStore.edit { it[REMINDER_DESCRIPTION] = pref.name }
|
||||
|
||||
suspend fun setPersistentReminders(enabled: Boolean) = dataStore.edit { it[PERSISTENT_REMINDERS] = enabled }
|
||||
|
||||
suspend fun setSyncIntervalMinutes(minutes: Int) = dataStore.edit { it[SYNC_INTERVAL] = minutes }
|
||||
|
||||
suspend fun setPushEnabled(enabled: Boolean) = dataStore.edit { it[PUSH_ENABLED] = enabled }
|
||||
@@ -364,6 +369,7 @@ class SettingsPrefs @Inject constructor(
|
||||
val SORT_ORDER = stringPreferencesKey("sort_order")
|
||||
val SNOOZE_MINUTES = intPreferencesKey("snooze_minutes")
|
||||
val REMINDER_DESCRIPTION = stringPreferencesKey("reminder_description")
|
||||
val PERSISTENT_REMINDERS = booleanPreferencesKey("persistent_reminders")
|
||||
val TIME_FORMAT = stringPreferencesKey("time_format")
|
||||
val WEEK_START = stringPreferencesKey("week_start")
|
||||
val AUTOFOCUS_TITLE = booleanPreferencesKey("autofocus_title")
|
||||
|
||||
@@ -116,10 +116,16 @@ internal fun reminderIsStale(occurrenceStart: Long?, current: Task?, missingMean
|
||||
* 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.
|
||||
*/
|
||||
internal fun TaskNotifier.postIfOpen(dataSource: TasksDataSource, taskId: Long, occurrenceStart: Long, settings: Settings) {
|
||||
internal fun TaskNotifier.postIfOpen(
|
||||
dataSource: TasksDataSource,
|
||||
taskId: Long,
|
||||
occurrenceStart: Long,
|
||||
settings: Settings,
|
||||
alert: Boolean = true,
|
||||
) {
|
||||
val task = dataSource.occurrence(taskId, occurrenceStart) ?: return
|
||||
if (task.isClosed) return
|
||||
postDue(task, settings)
|
||||
postDue(task, settings, alert)
|
||||
val now = runCatching { dataSource.occurrence(taskId, occurrenceStart) }.getOrElse { return }
|
||||
if (now == null || now.isClosed) cancel(taskId)
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import kotlin.time.Instant
|
||||
* alarm, which lives outside [ScheduledReminderStore] so no scheduler pass
|
||||
* can cancel it.
|
||||
* - **Show** re-posts it, if the task is still open by then.
|
||||
* - **Dismissed** puts a persistent reminder straight back, silently.
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class ReminderActionReceiver : BroadcastReceiver() {
|
||||
@@ -67,6 +68,13 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
startupGate.awaitReady()
|
||||
notifier.postIfOpen(dataSource, taskId, occurrence, settings)
|
||||
}
|
||||
|
||||
ACTION_DISMISSED -> runCatching {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
if (!settings.remindersEnabled || !settings.persistentReminders) return@runCatching
|
||||
startupGate.awaitReady()
|
||||
notifier.postIfOpen(dataSource, taskId, occurrence, settings, alert = false)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pending.finish()
|
||||
@@ -78,6 +86,7 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
const val ACTION_DONE = "de.jeanlucmakiola.agendula.reminders.DONE"
|
||||
const val ACTION_SNOOZE = "de.jeanlucmakiola.agendula.reminders.SNOOZE"
|
||||
const val ACTION_SHOW = "de.jeanlucmakiola.agendula.reminders.SHOW"
|
||||
const val ACTION_DISMISSED = "de.jeanlucmakiola.agendula.reminders.DISMISSED"
|
||||
|
||||
private const val EXTRA_TASK_ID = "de.jeanlucmakiola.agendula.extra.TASK_ID"
|
||||
private const val EXTRA_OCCURRENCE = "de.jeanlucmakiola.agendula.extra.OCCURRENCE_START"
|
||||
|
||||
@@ -21,6 +21,7 @@ import de.jeanlucmakiola.agendula.MainActivity
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.data.prefs.ReminderDescription
|
||||
import de.jeanlucmakiola.agendula.data.prefs.Settings
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.domain.readableDescription
|
||||
import de.jeanlucmakiola.agendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.agendula.data.di.ApplicationScope
|
||||
@@ -31,6 +32,7 @@ import de.jeanlucmakiola.agendula.domain.Task
|
||||
import de.jeanlucmakiola.agendula.domain.calendarDate
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.BreakIterator
|
||||
import java.time.Instant as JInstant
|
||||
@@ -48,7 +50,8 @@ 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]).
|
||||
* [MainActivity.taskIntent]). With [Settings.persistentReminders] a reminder is
|
||||
* ongoing and re-posts silently when swiped away.
|
||||
*/
|
||||
@Singleton
|
||||
class TaskNotifier @Inject constructor(
|
||||
@@ -56,6 +59,7 @@ class TaskNotifier @Inject constructor(
|
||||
private val resolver: ProviderResolver,
|
||||
private val dataSource: TasksDataSource,
|
||||
private val startupGate: StartupGate,
|
||||
private val settingsPrefs: SettingsPrefs,
|
||||
@ApplicationScope private val scope: CoroutineScope,
|
||||
) {
|
||||
private val manager get() = context.getSystemService(NotificationManager::class.java)
|
||||
@@ -70,7 +74,7 @@ class TaskNotifier @Inject constructor(
|
||||
|
||||
// canPost() checks POST_NOTIFICATIONS before we ever call notify().
|
||||
@SuppressLint("MissingPermission")
|
||||
fun postDue(task: Task, settings: Settings) {
|
||||
fun postDue(task: Task, settings: Settings, alert: Boolean = true) {
|
||||
if (!canPost()) return
|
||||
val channelId = ensureListChannel(task.listId, task.listName)
|
||||
val title = task.title.ifBlank { context.getString(R.string.task_untitled) }
|
||||
@@ -107,8 +111,15 @@ class TaskNotifier @Inject constructor(
|
||||
}
|
||||
.setCategory(NotificationCompat.CATEGORY_REMINDER)
|
||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||
.setAutoCancel(true)
|
||||
.setAutoCancel(!settings.persistentReminders)
|
||||
.setOngoing(settings.persistentReminders)
|
||||
.setSilent(!alert)
|
||||
.setContentIntent(tapIntent)
|
||||
.apply {
|
||||
if (settings.persistentReminders) {
|
||||
setDeleteIntent(actionIntent(task, ReminderActionReceiver.ACTION_DISMISSED))
|
||||
}
|
||||
}
|
||||
.addExtras(
|
||||
Bundle().apply {
|
||||
putLong(EXTRA_OCCURRENCE, task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE)
|
||||
@@ -144,15 +155,34 @@ class TaskNotifier @Inject constructor(
|
||||
* occurrence is null for a reminder posted before it was recorded.
|
||||
*/
|
||||
fun withdrawStale(isStale: (taskId: Long, occurrenceStart: Long?) -> Boolean) {
|
||||
// Only the current store's: another store's task ids name other tasks.
|
||||
postedReminders().forEach { (taskId, occurrence) -> if (isStale(taskId, occurrence)) cancel(taskId) }
|
||||
}
|
||||
|
||||
/** Re-posts every shown reminder without alerting, so a change to [Settings.persistentReminders] reaches it. */
|
||||
fun repostActive() {
|
||||
scope.launch {
|
||||
runCatching {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
if (!settings.remindersEnabled) return@runCatching
|
||||
startupGate.awaitReady()
|
||||
postedReminders().forEach { (taskId, occurrence) ->
|
||||
runCatching {
|
||||
postIfOpen(dataSource, taskId, occurrence ?: ScheduledReminder.NO_OCCURRENCE, settings, alert = false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The current store's posted reminders as task id and occurrence; another store's ids name other tasks. */
|
||||
private fun postedReminders(): List<Pair<Long, Long?>> {
|
||||
val prefix = "$LIST_CHANNEL_PREFIX${resolver.mode().name.lowercase()}_"
|
||||
manager.activeNotifications
|
||||
return manager.activeNotifications
|
||||
.filter { it.id == NOTIFICATION_ID && it.notification.channelId?.startsWith(prefix) == true }
|
||||
.forEach { posted ->
|
||||
val taskId = posted.tag?.toLongOrNull() ?: return@forEach
|
||||
.mapNotNull { posted ->
|
||||
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
|
||||
if (isStale(taskId, occurrence)) cancel(taskId)
|
||||
taskId to (if (extras.containsKey(EXTRA_OCCURRENCE)) extras.getLong(EXTRA_OCCURRENCE) else null)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -209,6 +209,15 @@ internal fun NotificationsScreen(
|
||||
trailing = if (batteryExempt) ({ SelectedCheck() }) else null,
|
||||
onClick = { context.openBatteryOptimizationSettings() },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_persistent_reminders),
|
||||
summary = stringResource(R.string.settings_persistent_reminders_hint),
|
||||
position = Position.Middle,
|
||||
trailing = {
|
||||
Switch(checked = state.settings.persistentReminders, onCheckedChange = viewModel::setPersistentReminders)
|
||||
},
|
||||
onClick = { viewModel.setPersistentReminders(!state.settings.persistentReminders) },
|
||||
)
|
||||
GroupedRow(
|
||||
title = stringResource(R.string.settings_snooze_duration),
|
||||
summary = snoozeLabel(state.settings.snoozeMinutes),
|
||||
|
||||
@@ -261,6 +261,10 @@ class SettingsViewModel @Inject constructor(
|
||||
fun setWeekStart(day: DayOfWeek?) = viewModelScope.launch { prefs.setWeekStart(day) }
|
||||
fun setSnoozeMinutes(minutes: Int) = viewModelScope.launch { prefs.setSnoozeMinutes(minutes) }
|
||||
fun setReminderDescription(pref: ReminderDescription) = viewModelScope.launch { prefs.setReminderDescription(pref) }
|
||||
fun setPersistentReminders(enabled: Boolean) = viewModelScope.launch {
|
||||
prefs.setPersistentReminders(enabled)
|
||||
taskNotifier.repostActive()
|
||||
}
|
||||
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) }
|
||||
|
||||
@@ -482,6 +482,8 @@
|
||||
<string name="settings_exact_alarms_blocked">Blocked — tap to allow exact reminders</string>
|
||||
<string name="settings_reliable_delivery">Reliable delivery</string>
|
||||
<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_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>
|
||||
|
||||
Reference in new issue
Block a user