Arm a snooze before waiting on startup, call it off for another store's reminder (#100)
This commit is contained in:
1 parent
5d45647005
commit
94805c5d91
3 files changed
+55
-9
No files matched your search
+18
-3
@@ -60,9 +60,14 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
}
|
||||
|
||||
ACTION_SNOOZE -> runCatching {
|
||||
if (!forgetPersistent(taskId, occurrence, store)) return@runCatching
|
||||
val minutes = settingsPrefs.settings.first().snoozeMinutes
|
||||
snoozeScheduler.schedule(taskId, occurrence, System.currentTimeMillis() + minutes * 60_000L)
|
||||
snoozeArmedFirst(
|
||||
arm = {
|
||||
val minutes = settingsPrefs.settings.first().snoozeMinutes
|
||||
snoozeScheduler.schedule(taskId, occurrence, System.currentTimeMillis() + minutes * 60_000L)
|
||||
},
|
||||
actsHere = { forgetPersistent(taskId, occurrence, store) },
|
||||
disarm = { snoozeScheduler.cancel(taskId, occurrence) },
|
||||
)
|
||||
}
|
||||
|
||||
ACTION_SHOW -> runCatching {
|
||||
@@ -120,6 +125,16 @@ class ReminderActionReceiver : BroadcastReceiver() {
|
||||
internal fun actsHere(postedIn: String?, isCurrentStore: (String) -> Boolean): Boolean =
|
||||
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.
|
||||
*/
|
||||
internal suspend fun snoozeArmedFirst(arm: suspend () -> Unit, actsHere: suspend () -> Boolean, disarm: suspend () -> Unit) {
|
||||
arm()
|
||||
if (!actsHere()) disarm()
|
||||
}
|
||||
|
||||
/**
|
||||
* The data URI is what keeps two reminders' PendingIntents apart —
|
||||
* `filterEquals` never compares extras.
|
||||
|
||||
+14
-6
@@ -41,6 +41,12 @@ class ReminderSnoozeScheduler @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
/** Calls off a snooze that has not re-shown yet. */
|
||||
suspend fun cancel(taskId: Long, occurrenceStart: Long) {
|
||||
clear(taskId, occurrenceStart)
|
||||
context.getSystemService(AlarmManager::class.java)?.cancel(showIntent(taskId, occurrenceStart))
|
||||
}
|
||||
|
||||
suspend fun rearm() {
|
||||
val now = System.currentTimeMillis()
|
||||
dataStore.data.first()[KEY].orEmpty().forEach { entry ->
|
||||
@@ -52,12 +58,7 @@ class ReminderSnoozeScheduler @Inject constructor(
|
||||
|
||||
private fun arm(taskId: Long, occurrenceStart: Long, triggerAtMillis: Long) {
|
||||
val alarmManager = context.getSystemService(AlarmManager::class.java) ?: return
|
||||
val pendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
taskId.toInt(),
|
||||
ReminderActionReceiver.intent(context, ReminderActionReceiver.ACTION_SHOW, taskId, occurrenceStart),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
val pendingIntent = showIntent(taskId, occurrenceStart)
|
||||
val canExact = Build.VERSION.SDK_INT < Build.VERSION_CODES.S || alarmManager.canScheduleExactAlarms()
|
||||
try {
|
||||
if (canExact) {
|
||||
@@ -70,6 +71,13 @@ class ReminderSnoozeScheduler @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun showIntent(taskId: Long, occurrenceStart: Long): PendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
taskId.toInt(),
|
||||
ReminderActionReceiver.intent(context, ReminderActionReceiver.ACTION_SHOW, taskId, occurrenceStart),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
|
||||
private companion object {
|
||||
/** `taskId|occurrenceStart|triggerAt` per pending snooze. */
|
||||
val KEY = stringSetPreferencesKey("pending_snoozes")
|
||||
|
||||
+23
@@ -1,6 +1,7 @@
|
||||
package de.jeanlucmakiola.agendula.data.reminders
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class ReminderActionStoreTest {
|
||||
@@ -21,4 +22,26 @@ class ReminderActionStoreTest {
|
||||
fun `a reminder posted before the store was recorded still acts`() {
|
||||
assertThat(ReminderActionReceiver.actsHere(null, current)).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a snooze is armed before the store check waits on startup`() = runBlocking {
|
||||
val steps = mutableListOf<String>()
|
||||
ReminderActionReceiver.snoozeArmedFirst(
|
||||
arm = { steps += "arm" },
|
||||
actsHere = { steps += "check"; true },
|
||||
disarm = { steps += "disarm" },
|
||||
)
|
||||
assertThat(steps).containsExactly("arm", "check").inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a snooze of another store's reminder is disarmed again`() = runBlocking {
|
||||
val steps = mutableListOf<String>()
|
||||
ReminderActionReceiver.snoozeArmedFirst(
|
||||
arm = { steps += "arm" },
|
||||
actsHere = { steps += "check"; false },
|
||||
disarm = { steps += "disarm" },
|
||||
)
|
||||
assertThat(steps).containsExactly("arm", "check", "disarm").inOrder()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user