Drop the persistent record before arming a snooze (#100)
This commit is contained in:
1 parent
d74831c607
commit
a82fa01b71
2 files changed
+28
-8
No files matched your search
+23
-5
@@ -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()
|
||||
}
|
||||
|
||||
+5
-3
@@ -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<String>()
|
||||
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<String>()
|
||||
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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user