Ver código fonte

Clear legacy 'not looping' notifications (addresses #1296)

dnzxy 2 dias atrás
pai
commit
a582dac8b2

+ 11 - 0
Trio/Sources/Services/UserNotifications/UserNotificationsManager.swift

@@ -55,6 +55,7 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
         Task { await updateGlucoseBadge() }
         configureNotificationCategories()
         clearLegacyCarbsRequiredNotification()
+        clearLegacyLoopNotifications()
         subscribeGlucoseUpdates()
     }
 
@@ -128,6 +129,16 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
         notificationCenter.removeDeliveredNotifications(withIdentifiers: [id])
     }
 
+    /// Purges the two "Last loop was X min ago" UNs the legacy
+    /// `scheduleMissingLoopNotifiactions` path scheduled. Their identifiers
+    /// aren't known to `NotLoopingMonitor`, so without this cleanup they'd
+    /// fire once each after upgrade — https://github.com/nightscout/Trio/issues/1296
+    private func clearLegacyLoopNotifications() {
+        let ids = ["Trio.noLoopFirstNotification", "Trio.noLoopSecondNotification"]
+        notificationCenter.removePendingNotificationRequests(withIdentifiers: ids)
+        notificationCenter.removeDeliveredNotifications(withIdentifiers: ids)
+    }
+
     private func fetchGlucoseIDs() async throws -> [NSManagedObjectID] {
         let context = CoreDataStack.shared.newTaskContext()
         context.name = "fetchGlucoseIDs"