From 065be79355acc081a3fb2b26d027d4afe26f2da6 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 9 May 2024 23:40:14 -0500 Subject: [PATCH] Update notification work logic --- .../java/org/tasks/jobs/NotificationWork.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/tasks/jobs/NotificationWork.kt b/app/src/main/java/org/tasks/jobs/NotificationWork.kt index 7a51f5d22..93e7273cf 100644 --- a/app/src/main/java/org/tasks/jobs/NotificationWork.kt +++ b/app/src/main/java/org/tasks/jobs/NotificationWork.kt @@ -12,6 +12,7 @@ import dagger.assisted.AssistedInject import org.tasks.Notifier import org.tasks.R import org.tasks.analytics.Firebase +import org.tasks.data.Alarm.Companion.TYPE_RANDOM import org.tasks.data.Alarm.Companion.TYPE_SNOOZE import org.tasks.data.AlarmDao import org.tasks.date.DateTimeUtils.toDateTime @@ -37,12 +38,8 @@ class NotificationWork @AssistedInject constructor( nextAlarm = preferences.adjustForQuietHours(now()) return Result.success() } - repeat(3) { - val (overdue, future) = alarmService.getAlarms() - nextAlarm = future.minOfOrNull { it.time } ?: 0 - if (overdue.isEmpty()) { - return Result.success() - } + val (overdue, _) = alarmService.getAlarms() + if (overdue.isNotEmpty()) { overdue .sortedBy { it.time } .also { alarms -> @@ -54,8 +51,14 @@ class NotificationWork @AssistedInject constructor( .map { it.toNotification() } .let { notifier.triggerNotifications(it) } } - firebase.reportException(IllegalStateException("Should have returned already")) - return Result.failure() + val alreadyTriggered = overdue.map { it.taskId }.toSet() + val (moreOverdue, future) = alarmService.getAlarms() + nextAlarm = moreOverdue + .filterNot { it.type == TYPE_RANDOM || alreadyTriggered.contains(it.taskId)} + .plus(future) + .minOfOrNull { it.time } + ?: 0 + return Result.success() } override suspend fun scheduleNext() {