From a82fa01b7166f966b5e3d47d2816fc6ecfc87248 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 20 Nov 2026 12:28:37 +0100 Subject: [PATCH] Drop the persistent record before arming a snooze (#100) --- .../data/reminders/ReminderActionReceiver.kt | 28 +++++++++++++++---- .../data/reminders/ReminderActionStoreTest.kt | 8 ++++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionReceiver.kt b/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionReceiver.kt index a72500c..39e7de4 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionReceiver.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionReceiver.kt @@ -61,11 +61,12 @@ class ReminderActionReceiver : BroadcastReceiver() { ACTION_SNOOZE -> runCatching { snoozeArmedFirst( + forget = { forgetKnown(taskId, occurrence, store) }, arm = { val minutes = settingsPrefs.settings.first().snoozeMinutes snoozeScheduler.schedule(taskId, occurrence, System.currentTimeMillis() + minutes * 60_000L) }, - actsHere = { forgetPersistent(taskId, occurrence, store) }, + actsHere = { forgetGated(taskId, occurrence, store) }, disarm = { snoozeScheduler.cancel(taskId, occurrence) }, ) } @@ -102,7 +103,17 @@ class ReminderActionReceiver : BroadcastReceiver() { * whose task ids name other tasks here: the action then stops at this. */ private suspend fun forgetPersistent(taskId: Long, occurrence: Long, store: String?): Boolean { + forgetKnown(taskId, occurrence, store) + return forgetGated(taskId, occurrence, store) + } + + /** The record of a reminder whose store it names; needs no startup gate. */ + private suspend fun forgetKnown(taskId: Long, occurrence: Long, store: String?) { if (store != null) notifier.forget(taskId, store, occurrence) + } + + /** The rest of [forgetPersistent], once the current store is known. */ + private suspend fun forgetGated(taskId: Long, occurrence: Long, store: String?): Boolean { startupGate.awaitReady() if (store == null) notifier.forget(taskId, occurrence = occurrence) return actsHere(store, notifier::isCurrentStore) @@ -126,11 +137,18 @@ class ReminderActionReceiver : BroadcastReceiver() { postedIn == null || isCurrentStore(postedIn) /** - * Arms a snooze before anything waits on the startup gate, which may - * outlast the receiver's window, and disarms it once the reminder turns - * out to be another store's. + * Drops the persistent record first, so no restore re-posts what is being + * snoozed, then arms the snooze before anything waits on the startup gate, + * which may outlast the receiver's window; disarms it once the reminder + * turns out to be another store's. */ - internal suspend fun snoozeArmedFirst(arm: suspend () -> Unit, actsHere: suspend () -> Boolean, disarm: suspend () -> Unit) { + internal suspend fun snoozeArmedFirst( + forget: suspend () -> Unit, + arm: suspend () -> Unit, + actsHere: suspend () -> Boolean, + disarm: suspend () -> Unit, + ) { + forget() arm() if (!actsHere()) disarm() } diff --git a/app/src/test/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionStoreTest.kt b/app/src/test/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionStoreTest.kt index 7bcd37d..c7e5b94 100644 --- a/app/src/test/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionStoreTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/agendula/data/reminders/ReminderActionStoreTest.kt @@ -24,24 +24,26 @@ class ReminderActionStoreTest { } @Test - fun `a snooze is armed before the store check waits on startup`() = runBlocking { + fun `a snooze drops the persistent record, then arms, before the store check waits on startup`() = runBlocking { val steps = mutableListOf() ReminderActionReceiver.snoozeArmedFirst( + forget = { steps += "forget" }, arm = { steps += "arm" }, actsHere = { steps += "check"; true }, disarm = { steps += "disarm" }, ) - assertThat(steps).containsExactly("arm", "check").inOrder() + assertThat(steps).containsExactly("forget", "arm", "check").inOrder() } @Test fun `a snooze of another store's reminder is disarmed again`() = runBlocking { val steps = mutableListOf() ReminderActionReceiver.snoozeArmedFirst( + forget = { steps += "forget" }, arm = { steps += "arm" }, actsHere = { steps += "check"; false }, disarm = { steps += "disarm" }, ) - assertThat(steps).containsExactly("arm", "check", "disarm").inOrder() + assertThat(steps).containsExactly("forget", "arm", "check", "disarm").inOrder() } }