Persistent reminders: settle edited occurrences, skip restore when nothing can post, one restyle lock (#100)
This commit is contained in:
1 parent
1ccbd003ac
commit
3fecd313f5
4 files changed
+22
-20
No files matched your search
@@ -145,6 +145,7 @@ internal suspend fun TaskNotifier.postIfOpen(
|
||||
}
|
||||
if (task.isClosed || (exact && !task.isOccurrence(occurrenceStart)) || (restoring != null && task.postponedSince(restoring))) {
|
||||
forget(taskId, occurrence = occurrenceStart)
|
||||
if (task.taskId != taskId) forget(task.taskId, occurrence = occurrenceStart)
|
||||
return
|
||||
}
|
||||
postDue(task, settings, alert, postedAt, restoring)
|
||||
|
||||
+10
-7
@@ -64,13 +64,16 @@ class PersistentReminderStore @Inject constructor(
|
||||
* 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 missed(store: String, taskId: Long, occurrence: Long, now: Long) {
|
||||
find(store, taskId, occurrence) ?: return
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import de.jeanlucmakiola.agendula.MainActivity
|
||||
import de.jeanlucmakiola.agendula.R
|
||||
import de.jeanlucmakiola.agendula.data.prefs.ReminderDescription
|
||||
import de.jeanlucmakiola.agendula.data.prefs.Settings
|
||||
import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs
|
||||
import de.jeanlucmakiola.agendula.domain.readableDescription
|
||||
import de.jeanlucmakiola.agendula.data.prefs.is24Hour
|
||||
import de.jeanlucmakiola.agendula.data.di.ApplicationScope
|
||||
@@ -33,6 +34,7 @@ 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
|
||||
@@ -63,6 +65,7 @@ class TaskNotifier @Inject constructor(
|
||||
private val startupGate: StartupGate,
|
||||
@ApplicationScope private val scope: CoroutineScope,
|
||||
private val records: PersistentReminderStore,
|
||||
private val settingsPrefs: SettingsPrefs,
|
||||
) {
|
||||
private val manager get() = context.getSystemService(NotificationManager::class.java)
|
||||
private val channelPrefs get() = context.getSharedPreferences(CHANNEL_PREFS, Context.MODE_PRIVATE)
|
||||
@@ -215,9 +218,12 @@ 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
|
||||
* occurrence and due were recorded is left alone, as a re-post couldn't tell
|
||||
* its occurrence.
|
||||
* its occurrence. Reads the settings under the lock, so the latest wins
|
||||
* however fast they flip.
|
||||
*/
|
||||
suspend fun restyleActive(persistent: Boolean) = postLock.withLock {
|
||||
suspend fun restyleActive() = postLock.withLock {
|
||||
val settings = settingsPrefs.settings.first()
|
||||
val persistent = settings.remindersEnabled && settings.persistentReminders
|
||||
postedReminders().forEach { posted ->
|
||||
val occurrence = posted.occurrence ?: return@forEach
|
||||
val extras = posted.notification.extras
|
||||
@@ -274,6 +280,8 @@ class TaskNotifier @Inject constructor(
|
||||
startupGate.awaitReady()
|
||||
val store = currentStore
|
||||
records.removeIf { it.store != store }
|
||||
// Nothing could be shown; trying anyway would re-read every record on each sync.
|
||||
if (!canPost()) return
|
||||
val shown = postedReminders().mapTo(HashSet()) { it.taskId }
|
||||
records.all().filter { it.taskId !in shown }.forEach { runCatching { repost(it, settings) } }
|
||||
}
|
||||
|
||||
@@ -37,8 +37,6 @@ import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.time.DayOfWeek
|
||||
import javax.inject.Inject
|
||||
@@ -255,8 +253,6 @@ class SettingsViewModel @Inject constructor(
|
||||
prefs.setAllDayReminderMinuteOfDay(minuteOfDay)
|
||||
resyncReminders()
|
||||
}
|
||||
private val restyleLock = Mutex()
|
||||
|
||||
fun setRemindersEnabled(enabled: Boolean) = viewModelScope.launch {
|
||||
prefs.setRemindersEnabled(enabled)
|
||||
restyleReminders()
|
||||
@@ -286,14 +282,8 @@ class SettingsViewModel @Inject constructor(
|
||||
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 {
|
||||
val settings = prefs.settings.first()
|
||||
withContext(io) {
|
||||
runCatching { taskNotifier.restyleActive(settings.remindersEnabled && settings.persistentReminders) }
|
||||
}
|
||||
}
|
||||
withContext(io) { runCatching { taskNotifier.restyleActive() } }
|
||||
}
|
||||
fun setShowAddSubtaskRow(show: Boolean) = viewModelScope.launch { prefs.setShowAddSubtaskRow(show) }
|
||||
fun setBottomAddBar(enabled: Boolean) = viewModelScope.launch { prefs.setBottomAddBar(enabled) }
|
||||
|
||||
Reference in new issue
Block a user