Leave tasks alone on Done or Snooze from another store's reminder (#100)

This commit is contained in:
makiolaj committed 2026-11-18 20:59:50 +01:00
1 parent 235d16a6f0
commit 3531e169cf
2 files changed
+41 -4

No files matched your search

@@ -22,6 +22,7 @@ import kotlin.time.Instant
* elapses. All app-internal intents, so the receiver is not exported.
*
* - **Done** completes the task — only the reminded occurrence, for a series.
* Done and Snooze from a reminder of another store only take it down.
* - **Snooze** hides the notification and arms [ReminderSnoozeScheduler]'s
* alarm, which lives outside [ScheduledReminderStore] so no scheduler pass
* can cancel it.
@@ -52,14 +53,14 @@ class ReminderActionReceiver : BroadcastReceiver() {
try {
when (action) {
ACTION_DONE -> runCatching {
forgetPersistent(taskId, occurrence, store)
if (!forgetPersistent(taskId, occurrence, store)) return@runCatching
val anchor = occurrence.takeIf { it != ScheduledReminder.NO_OCCURRENCE }
?.let(Instant::fromEpochMilliseconds)
repository.setCompleted(taskId, anchor, completed = true)
}
ACTION_SNOOZE -> runCatching {
forgetPersistent(taskId, occurrence, store)
if (!forgetPersistent(taskId, occurrence, store)) return@runCatching
val minutes = settingsPrefs.settings.first().snoozeMinutes
snoozeScheduler.schedule(taskId, occurrence, System.currentTimeMillis() + minutes * 60_000L)
}
@@ -90,11 +91,16 @@ class ReminderActionReceiver : BroadcastReceiver() {
}
}
/** Before the action's own work, so a restore running meanwhile finds nothing to bring back. */
private suspend fun forgetPersistent(taskId: Long, occurrence: Long, store: String?) {
/**
* Before the action's own work, so a restore running meanwhile finds nothing
* to bring back. False when the reminder was posted from another store,
* whose task ids name other tasks here: the action then stops at this.
*/
private suspend fun forgetPersistent(taskId: Long, occurrence: Long, store: String?): Boolean {
if (store != null) notifier.forget(taskId, store, occurrence)
startupGate.awaitReady()
if (store == null) notifier.forget(taskId, occurrence = occurrence)
return actsHere(store, notifier::isCurrentStore)
}
companion object {
@@ -107,6 +113,13 @@ class ReminderActionReceiver : BroadcastReceiver() {
private const val EXTRA_OCCURRENCE = "de.jeanlucmakiola.agendula.extra.OCCURRENCE_START"
private const val EXTRA_STORE = "de.jeanlucmakiola.agendula.extra.STORE"
/**
* Whether a reminder posted from [postedIn] may act on its task id in the
* current store; one from before the store was recorded may.
*/
internal fun actsHere(postedIn: String?, isCurrentStore: (String) -> Boolean): Boolean =
postedIn == null || isCurrentStore(postedIn)
/**
* The data URI is what keeps two reminders' PendingIntents apart —
* `filterEquals` never compares extras.
@@ -0,0 +1,24 @@
package de.jeanlucmakiola.agendula.data.reminders
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test
class ReminderActionStoreTest {
private val current = { store: String -> store == "own" }
@Test
fun `a reminder from the current store acts on its task`() {
assertThat(ReminderActionReceiver.actsHere("own", current)).isTrue()
}
@Test
fun `a reminder from another store leaves the task with its id alone`() {
assertThat(ReminderActionReceiver.actsHere("provider", current)).isFalse()
}
@Test
fun `a reminder posted before the store was recorded still acts`() {
assertThat(ReminderActionReceiver.actsHere(null, current)).isTrue()
}
}