Persistent reminders: own record store and key, wait for the startup gate, skip postponed tasks (#100)

This commit is contained in:
makiolaj committed 2026-11-17 10:54:07 +01:00
1 parent 150f10eead
commit 1ccbd003ac
6 files changed
+229 -131

No files matched your search

@@ -122,9 +122,11 @@ internal fun Task.isOccurrence(occurrenceStart: Long?): Boolean = when (occurren
* Posts the reminder for [taskId]'s occurrence if it is still open, then checks
* 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.
* With [exact], a lookup that fell back to another occurrence posts nothing. A
* persistent reminder that won't come back is forgotten; one the store just
* failed to find counts a miss.
* With [exact], a lookup that fell back to another occurrence posts nothing.
*
* [restoring] brings back a persistent reminder, unless its task was moved to a
* later due since. A persistent reminder that won't come back is forgotten; one
* whose task an external store just failed to find is kept for a while.
*/
internal suspend fun TaskNotifier.postIfOpen(
dataSource: TasksDataSource,
@@ -134,18 +136,29 @@ internal suspend fun TaskNotifier.postIfOpen(
alert: Boolean = true,
exact: Boolean = false,
postedAt: Long? = null,
ifAbsent: Boolean = false,
restoring: PersistentRecord? = null,
) {
val task = dataSource.occurrence(taskId, occurrenceStart)
if (task == null) {
if (missingMeansGone()) forget(taskId, occurrence = occurrenceStart) else missed(taskId, occurrenceStart)
return
}
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart))) {
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart)) || (restoring != null && task.postponedSince(restoring))) {
forget(taskId, occurrence = occurrenceStart)
return
}
postDue(task, settings, alert, postedAt, ifAbsent)
postDue(task, settings, alert, postedAt, restoring)
// An edited occurrence is a row of its own, posted and remembered under its own id.
if (task.taskId != taskId) forget(taskId, occurrence = occurrenceStart)
val now = runCatching { dataSource.occurrence(taskId, occurrenceStart) }.getOrElse { return }
if ((now == null && missingMeansGone()) || now?.isClosed == true) cancel(taskId)
when {
now?.isClosed == true -> cancel(task.taskId)
now == null -> if (missingMeansGone()) cancel(task.taskId) else dismiss(task.taskId)
}
}
/** Whether the task now falls due later than when [record]'s reminder went off, and not yet. */
private fun Task.postponedSince(record: PersistentRecord): Boolean {
val due = due?.toEpochMilliseconds()
return due != record.due && (due == null || due > System.currentTimeMillis())
}
@@ -0,0 +1,93 @@
package de.jeanlucmakiola.agendula.data.reminders
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringSetPreferencesKey
import kotlinx.coroutines.flow.first
import javax.inject.Inject
import javax.inject.Singleton
/**
* A persistent reminder that is up: its store and task, the occurrence and due
* it was shown for, when it was first shown, and since when its task can't be
* found (0 while it can).
*/
data class PersistentRecord(
val store: String,
val taskId: Long,
val occurrence: Long,
val due: Long?,
val postedAt: Long,
val missingSince: Long = 0L,
) {
fun encode(): String = "$store|$taskId|$occurrence|${due ?: ""}|$postedAt|$missingSince"
companion object {
fun decode(value: String): PersistentRecord? {
val parts = value.split('|').takeIf { it.size == 6 } ?: return null
return PersistentRecord(
store = parts[0].ifEmpty { return null },
taskId = parts[1].toLongOrNull() ?: return null,
occurrence = parts[2].toLongOrNull() ?: return null,
due = if (parts[3].isEmpty()) null else parts[3].toLongOrNull() ?: return null,
postedAt = parts[4].toLongOrNull() ?: return null,
missingSince = parts[5].toLongOrNull() ?: return null,
)
}
}
}
/**
* The persistent reminders on screen, so a reboot or a re-post that couldn't
* read its task can bring them back. One per task and store.
*/
@Singleton
class PersistentReminderStore @Inject constructor(
private val dataStore: DataStore<Preferences>,
) {
suspend fun all(): List<PersistentRecord> = dataStore.data.first()[KEY].orEmpty().mapNotNull(PersistentRecord::decode)
suspend fun find(store: String, taskId: Long, occurrence: Long): PersistentRecord? =
all().firstOrNull { it.store == store && it.taskId == taskId && it.occurrence == occurrence }
suspend fun put(record: PersistentRecord) = edit { records ->
records.filterNot { it.store == record.store && it.taskId == record.taskId } + record
}
suspend fun removeIf(predicate: (PersistentRecord) -> Boolean) {
if (all().any(predicate)) edit { records -> records.filterNot(predicate) }
}
/**
* Notes that [taskId]'s occurrence wasn't found at [now]; one missing for
* longer than [MISSING_GRACE_MS] is dropped. Time, not a count, as a sync
* can run several times in a second.
*/
suspend fun missed(store: String, taskId: Long, occurrence: Long, now: Long) = edit { records ->
records.mapNotNull { record ->
when {
record.store != store || record.taskId != taskId || record.occurrence != occurrence -> record
record.missingSince == 0L -> record.copy(missingSince = now)
now - record.missingSince >= MISSING_GRACE_MS -> null
else -> record
}
}
}
suspend fun clear() {
dataStore.edit { it.remove(KEY) }
}
private suspend fun edit(transform: (List<PersistentRecord>) -> List<PersistentRecord>) {
dataStore.edit { p ->
p[KEY] = transform(p[KEY].orEmpty().mapNotNull(PersistentRecord::decode)).mapTo(HashSet()) { it.encode() }
}
}
private companion object {
/** Not `persistent_reminders`: that is the setting's key in the same file. */
val KEY = stringSetPreferencesKey("persistent_reminder_records")
const val MISSING_GRACE_MS = 24 * 60 * 60 * 1000L
}
}
@@ -44,24 +44,24 @@ class ReminderActionReceiver : BroadcastReceiver() {
val taskId = intent.getLongExtra(EXTRA_TASK_ID, -1L).takeIf { it > 0L } ?: return
val occurrence = intent.getLongExtra(EXTRA_OCCURRENCE, ScheduledReminder.NO_OCCURRENCE)
val action = intent.action ?: return
// The store the reminder was posted in; known before the startup gate, unlike the current one.
val store = intent.getStringExtra(EXTRA_STORE)
if (action == ACTION_DONE || action == ACTION_SNOOZE) notifier.dismiss(taskId)
val pending = goAsync()
scope.launch {
try {
when (action) {
ACTION_DONE -> runCatching {
startupGate.awaitReady()
forgetPersistent(taskId, occurrence, store)
val anchor = occurrence.takeIf { it != ScheduledReminder.NO_OCCURRENCE }
?.let(Instant::fromEpochMilliseconds)
repository.setCompleted(taskId, anchor, completed = true)
notifier.forget(taskId, occurrence = occurrence)
}
ACTION_SNOOZE -> runCatching {
forgetPersistent(taskId, occurrence, store)
val minutes = settingsPrefs.settings.first().snoozeMinutes
snoozeScheduler.schedule(taskId, occurrence, System.currentTimeMillis() + minutes * 60_000L)
startupGate.awaitReady()
notifier.forget(taskId, occurrence = occurrence)
}
ACTION_SHOW -> runCatching {
@@ -73,22 +73,14 @@ class ReminderActionReceiver : BroadcastReceiver() {
}
ACTION_DISMISSED -> runCatching {
val store = intent.getStringExtra(EXTRA_STORE)
if (store == null) return@runCatching
val settings = settingsPrefs.settings.first()
startupGate.awaitReady()
if (!settings.remindersEnabled || !settings.persistentReminders || !notifier.isCurrentStore(store)) {
if (store != null) notifier.forget(taskId, store, occurrence)
return@runCatching
if (settings.remindersEnabled && settings.persistentReminders && notifier.isCurrentStore(store)) {
notifier.repostRemembered(taskId, occurrence, settings)
} else {
notifier.forget(taskId, store, occurrence)
}
notifier.postIfOpen(
dataSource,
taskId,
occurrence,
settings,
alert = false,
exact = true,
postedAt = intent.getLongExtra(EXTRA_POSTED_AT, 0L).takeIf { it > 0L },
)
}
}
} finally {
@@ -97,6 +89,13 @@ 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?) {
if (store != null) notifier.forget(taskId, store, occurrence)
startupGate.awaitReady()
if (store == null) notifier.forget(taskId, occurrence = occurrence)
}
companion object {
const val ACTION_DONE = "de.jeanlucmakiola.agendula.reminders.DONE"
const val ACTION_SNOOZE = "de.jeanlucmakiola.agendula.reminders.SNOOZE"
@@ -106,23 +105,21 @@ class ReminderActionReceiver : BroadcastReceiver() {
private const val EXTRA_TASK_ID = "de.jeanlucmakiola.agendula.extra.TASK_ID"
private const val EXTRA_OCCURRENCE = "de.jeanlucmakiola.agendula.extra.OCCURRENCE_START"
private const val EXTRA_STORE = "de.jeanlucmakiola.agendula.extra.STORE"
private const val EXTRA_POSTED_AT = "de.jeanlucmakiola.agendula.extra.POSTED_AT"
/**
* The data URI is what keeps two reminders' PendingIntents apart —
* `filterEquals` never compares extras.
*/
fun intent(context: Context, action: String, taskId: Long, occurrenceStart: Long): Intent =
fun intent(context: Context, action: String, taskId: Long, occurrenceStart: Long, store: String? = null): Intent =
Intent(context, ReminderActionReceiver::class.java)
.setAction(action)
.setData("agendula://reminder-action/$taskId/$occurrenceStart".toUri())
.putExtra(EXTRA_TASK_ID, taskId)
.putExtra(EXTRA_OCCURRENCE, occurrenceStart)
.apply { if (store != null) putExtra(EXTRA_STORE, store) }
/** A persistent reminder's delete intent, naming the store its task id belongs to and when it was first shown. */
fun dismissedIntent(context: Context, taskId: Long, occurrenceStart: Long, store: String, postedAt: Long): Intent =
intent(context, ACTION_DISMISSED, taskId, occurrenceStart)
.putExtra(EXTRA_STORE, store)
.putExtra(EXTRA_POSTED_AT, postedAt)
/** A persistent reminder's delete intent, naming the store its task id belongs to. */
fun dismissedIntent(context: Context, taskId: Long, occurrenceStart: Long, store: String): Intent =
intent(context, ACTION_DISMISSED, taskId, occurrenceStart, store)
}
}
@@ -18,10 +18,6 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringSetPreferencesKey
import de.jeanlucmakiola.agendula.MainActivity
import de.jeanlucmakiola.agendula.R
import de.jeanlucmakiola.agendula.data.prefs.ReminderDescription
@@ -37,7 +33,6 @@ 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 kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@@ -67,7 +62,7 @@ class TaskNotifier @Inject constructor(
private val dataSource: TasksDataSource,
private val startupGate: StartupGate,
@ApplicationScope private val scope: CoroutineScope,
private val dataStore: DataStore<Preferences>,
private val records: PersistentReminderStore,
) {
private val manager get() = context.getSystemService(NotificationManager::class.java)
private val channelPrefs get() = context.getSharedPreferences(CHANNEL_PREFS, Context.MODE_PRIVATE)
@@ -88,11 +83,15 @@ class TaskNotifier @Inject constructor(
settings: Settings,
alert: Boolean = true,
postedAt: Long? = null,
ifAbsent: Boolean = false,
restoring: PersistentRecord? = null,
) {
if (!canPost()) return
postLock.withLock {
if (ifAbsent && postedReminders().any { it.taskId == task.taskId }) return
// A restore yields to a reminder already up and to a Done or Snooze that dropped the record meanwhile.
if (restoring != null) {
if (postedReminders().any { it.taskId == task.taskId }) return
records.find(restoring.store, restoring.taskId, restoring.occurrence) ?: return
}
post(task, settings, alert, postedAt)
}
}
@@ -143,9 +142,14 @@ class TaskNotifier @Inject constructor(
.setSilent(!alert)
// A group of its own, so a silent re-post doesn't land in NotificationCompat's shared "silent" group.
.apply { if (persistent) setGroup("$PERSISTENT_GROUP_PREFIX${task.taskId}") }
.setDeleteIntent(if (persistent) dismissedIntent(task.taskId, occurrence, shownAt) else null)
.setDeleteIntent(if (persistent) dismissedIntent(task.taskId, occurrence) else null)
.setContentIntent(tapIntent)
.addExtras(Bundle().apply { putLong(EXTRA_OCCURRENCE, occurrence) })
.addExtras(
Bundle().apply {
putLong(EXTRA_OCCURRENCE, occurrence)
putLong(EXTRA_DUE, task.due?.toEpochMilliseconds() ?: NO_DUE)
},
)
.addAction(0, context.getString(R.string.reminder_action_done), actionIntent(task, ReminderActionReceiver.ACTION_DONE))
.addAction(
0,
@@ -154,22 +158,11 @@ class TaskNotifier @Inject constructor(
)
.build()
NotificationManagerCompat.from(context).notify(task.taskId.toString(), NOTIFICATION_ID, notification)
remember(task.taskId, occurrence, shownAt, persistent)
remember(task.taskId, occurrence, task.due?.toEpochMilliseconds(), shownAt, persistent)
}
private suspend fun remember(taskId: Long, occurrence: Long, postedAt: Long, persistent: Boolean) {
if (!persistent) return forget(taskId)
val store = currentStore
editRecords { records ->
records.filterNot { it.store == store && it.taskId == taskId } + PersistentRecord(store, taskId, occurrence, postedAt)
}
}
private suspend fun editRecords(transform: (List<PersistentRecord>) -> List<PersistentRecord>) {
dataStore.edit { p ->
p[PERSISTENT_KEY] = transform(p[PERSISTENT_KEY].orEmpty().mapNotNull(PersistentRecord::decode))
.mapTo(HashSet()) { it.encode() }
}
private suspend fun remember(taskId: Long, occurrence: Long, due: Long?, postedAt: Long, persistent: Boolean) {
if (persistent) records.put(PersistentRecord(currentStore, taskId, occurrence, due, postedAt)) else forget(taskId)
}
/** Characters per line of an expanded notification on this screen, at the user's font size. */
@@ -197,21 +190,12 @@ class TaskNotifier @Inject constructor(
* [occurrence], only when it is that occurrence's.
*/
suspend fun forget(taskId: Long, store: String = currentStore, occurrence: Long? = null) {
editRecords { records ->
records.filterNot { it.store == store && it.taskId == taskId && (occurrence == null || it.occurrence == occurrence) }
}
records.removeIf { it.store == store && it.taskId == taskId && (occurrence == null || it.occurrence == occurrence) }
}
/** Counts a failed lookup of a remembered reminder, dropping it after [MAX_MISSES] in a row. */
suspend fun missed(taskId: Long, occurrence: Long) {
val store = currentStore
editRecords { records ->
records.mapNotNull { record ->
if (record.store != store || record.taskId != taskId || record.occurrence != occurrence) return@mapNotNull record
record.copy(misses = record.misses + 1).takeIf { it.misses < MAX_MISSES }
}
}
}
/** Notes that a remembered reminder's task wasn't found; one gone long enough is dropped. */
suspend fun missed(taskId: Long, occurrence: Long) =
records.missed(currentStore, taskId, occurrence, System.currentTimeMillis())
fun isCurrentStore(store: String?): Boolean = store == currentStore
@@ -230,22 +214,54 @@ class TaskNotifier @Inject constructor(
/**
* Turns the shown reminders persistent or back, in place: the update doesn't
* alert again and keeps each reminder's content and time. One posted before
* occurrences were recorded is left alone, as a re-post can't find its occurrence.
* occurrence and due were recorded is left alone, as a re-post couldn't tell
* its occurrence.
*/
suspend fun restyleActive(persistent: Boolean) = postLock.withLock {
postedReminders().forEach { posted ->
val occurrence = posted.occurrence ?: return@forEach
val extras = posted.notification.extras
if (!extras.containsKey(EXTRA_DUE)) return@forEach
val due = extras.getLong(EXTRA_DUE).takeIf { it != NO_DUE }
val notification = Notification.Builder.recoverBuilder(context, posted.notification)
.setAutoCancel(!persistent)
.setOnlyAlertOnce(true)
.setGroup(if (persistent) "$PERSISTENT_GROUP_PREFIX${posted.taskId}" else null)
.setDeleteIntent(if (persistent) dismissedIntent(posted.taskId, occurrence, posted.postedAt) else null)
.setDeleteIntent(if (persistent) dismissedIntent(posted.taskId, occurrence) else null)
.build()
manager.notify(posted.taskId.toString(), NOTIFICATION_ID, notification)
remember(posted.taskId, occurrence, posted.postedAt, persistent)
remember(posted.taskId, occurrence, due, posted.postedAt, persistent)
}
}
/** Re-posts the shown reminders silently, so a changed snooze length or description shows on them. */
suspend fun refreshShown(settings: Settings) {
startupGate.awaitReady()
postedReminders().forEach { posted ->
val occurrence = posted.occurrence ?: return@forEach
runCatching {
postIfOpen(dataSource, posted.taskId, occurrence, settings, alert = false, exact = true, postedAt = posted.postedAt)
}
}
}
/** Re-posts [taskId]'s persistent reminder after a swipe, if it is still remembered. */
suspend fun repostRemembered(taskId: Long, occurrence: Long, settings: Settings) {
val record = records.find(currentStore, taskId, occurrence) ?: return
repost(record, settings)
}
private suspend fun repost(record: PersistentRecord, settings: Settings) = postIfOpen(
dataSource,
record.taskId,
record.occurrence,
settings,
alert = false,
exact = true,
postedAt = record.postedAt,
restoring = record,
)
/**
* Brings back, silently and at their old time, the persistent reminders no
* longer shown: taken down by a reboot or a force-stop, or by a swipe whose
@@ -253,29 +269,13 @@ class TaskNotifier @Inject constructor(
* other tasks here.
*/
suspend fun restorePersistent(settings: Settings) {
if (!settings.remindersEnabled || !settings.persistentReminders) {
dataStore.edit { it.remove(PERSISTENT_KEY) }
return
}
if (!settings.remindersEnabled || !settings.persistentReminders) return records.clear()
// Before it, the store is a guess, and a wrong one would drop every record.
startupGate.awaitReady()
val store = currentStore
records.removeIf { it.store != store }
val shown = postedReminders().mapTo(HashSet()) { it.taskId }
dataStore.data.first()[PERSISTENT_KEY].orEmpty().mapNotNull(PersistentRecord::decode).forEach { record ->
when {
record.store != store -> forget(record.taskId, record.store)
record.taskId !in shown -> runCatching {
postIfOpen(
dataSource,
record.taskId,
record.occurrence,
settings,
alert = false,
exact = true,
postedAt = record.postedAt,
ifAbsent = true,
)
}
}
}
records.all().filter { it.taskId !in shown }.forEach { runCatching { repost(it, settings) } }
}
private class Posted(val taskId: Long, val occurrence: Long?, val postedAt: Long, val notification: Notification)
@@ -293,8 +293,8 @@ class TaskNotifier @Inject constructor(
}
}
private fun dismissedIntent(taskId: Long, occurrence: Long, postedAt: Long): PendingIntent =
broadcast(taskId, ReminderActionReceiver.dismissedIntent(context, taskId, occurrence, currentStore, postedAt))
private fun dismissedIntent(taskId: Long, occurrence: Long): PendingIntent =
broadcast(taskId, ReminderActionReceiver.dismissedIntent(context, taskId, occurrence, currentStore))
private fun actionIntent(task: Task, action: String): PendingIntent = broadcast(
task.taskId,
@@ -303,6 +303,7 @@ class TaskNotifier @Inject constructor(
action,
task.taskId,
task.occurrenceStart?.toEpochMilliseconds() ?: ScheduledReminder.NO_OCCURRENCE,
store = currentStore,
),
)
@@ -377,8 +378,8 @@ class TaskNotifier @Inject constructor(
private const val EXTRA_OCCURRENCE = "de.jeanlucmakiola.agendula.extra.OCCURRENCE_START"
private const val CHANNEL_PREFS = "reminder_channels"
private const val KEY_LEGACY_IMPORTANCE = "legacy_importance"
private val PERSISTENT_KEY = stringSetPreferencesKey("persistent_reminders")
private const val MAX_MISSES = 3
private const val EXTRA_DUE = "de.jeanlucmakiola.agendula.extra.DUE"
private const val NO_DUE = Long.MIN_VALUE
private const val PERSISTENT_GROUP_PREFIX = "reminder_"
/**
@@ -393,33 +394,6 @@ class TaskNotifier @Inject constructor(
}
}
/**
* A persistent reminder remembered past a reboot: its store and task, the
* occurrence and time it was first shown, and how many lookups in a row failed.
*/
internal data class PersistentRecord(
val store: String,
val taskId: Long,
val occurrence: Long,
val postedAt: Long,
val misses: Int = 0,
) {
fun encode(): String = "$store|$taskId|$occurrence|$postedAt|$misses"
companion object {
fun decode(value: String): PersistentRecord? {
val parts = value.split('|').takeIf { it.size == 5 } ?: return null
return PersistentRecord(
store = parts[0].ifEmpty { return null },
taskId = parts[1].toLongOrNull() ?: return null,
occurrence = parts[2].toLongOrNull() ?: return null,
postedAt = parts[3].toLongOrNull() ?: return null,
misses = parts[4].toIntOrNull() ?: return null,
)
}
}
}
/** The words a reminder uses for the days either side of today. */
internal class RelativeDays(val today: String, val tomorrow: String, val yesterday: String)
@@ -263,15 +263,29 @@ class SettingsViewModel @Inject constructor(
resyncReminders()
}
fun setAutofocusTitle(enabled: Boolean) = viewModelScope.launch { prefs.setAutofocusTitle(enabled) }
fun setTimeFormat(pref: TimeFormatPref) = viewModelScope.launch { prefs.setTimeFormat(pref) }
fun setTimeFormat(pref: TimeFormatPref) = viewModelScope.launch {
prefs.setTimeFormat(pref)
refreshShownReminders()
}
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 setSnoozeMinutes(minutes: Int) = viewModelScope.launch {
prefs.setSnoozeMinutes(minutes)
refreshShownReminders()
}
fun setReminderDescription(pref: ReminderDescription) = viewModelScope.launch {
prefs.setReminderDescription(pref)
refreshShownReminders()
}
fun setPersistentReminders(enabled: Boolean) = viewModelScope.launch {
prefs.setPersistentReminders(enabled)
restyleReminders()
}
private suspend fun refreshShownReminders() {
val settings = prefs.settings.first()
withContext(io) { runCatching { taskNotifier.refreshShown(settings) } }
}
/** Makes the shown reminders match the settings; under the lock the latest wins, however fast they flip. */
private suspend fun restyleReminders() {
restyleLock.withLock {
@@ -7,15 +7,22 @@ class PersistentRecordTest {
@Test
fun `record round-trips`() {
val record = PersistentRecord("own", 42L, ScheduledReminder.NO_OCCURRENCE, 1_700_000_000_000L, misses = 2)
val record = PersistentRecord("own", 42L, 1_700_000_000_000L, due = 1_700_000_900_000L, postedAt = 1_700_000_100_000L, missingSince = 5L)
assertThat(PersistentRecord.decode(record.encode())).isEqualTo(record)
}
@Test
fun `record without due or occurrence round-trips`() {
val record = PersistentRecord("external", 7L, ScheduledReminder.NO_OCCURRENCE, due = null, postedAt = 1L)
assertThat(PersistentRecord.decode(record.encode())).isEqualTo(record)
}
@Test
fun `malformed records decode to null`() {
assertThat(PersistentRecord.decode("own|42|1|2")).isNull()
assertThat(PersistentRecord.decode("|42|1|2|0")).isNull()
assertThat(PersistentRecord.decode("own|x|1|2|0")).isNull()
assertThat(PersistentRecord.decode("own|42|1|2|0|9")).isNull()
assertThat(PersistentRecord.decode("own|42|1||2")).isNull()
assertThat(PersistentRecord.decode("|42|1||2|0")).isNull()
assertThat(PersistentRecord.decode("own|x|1||2|0")).isNull()
assertThat(PersistentRecord.decode("own|42|1|x|2|0")).isNull()
assertThat(PersistentRecord.decode("own|42|1||2|0|9")).isNull()
}
}