|
@@ -11,6 +11,7 @@ import UserNotifications
|
|
|
protocol UserNotificationsManager {
|
|
protocol UserNotificationsManager {
|
|
|
func getNotificationSettings(completionHandler: @escaping (UNNotificationSettings) -> Void)
|
|
func getNotificationSettings(completionHandler: @escaping (UNNotificationSettings) -> Void)
|
|
|
func requestNotificationPermissions(completion: @escaping (Bool) -> Void)
|
|
func requestNotificationPermissions(completion: @escaping (Bool) -> Void)
|
|
|
|
|
+ @MainActor func applySnooze(for duration: TimeInterval) async
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
enum GlucoseSourceKey: String {
|
|
enum GlucoseSourceKey: String {
|
|
@@ -40,6 +41,11 @@ protocol pumpNotificationObserver {
|
|
|
func pumpRemoveNotification()
|
|
func pumpRemoveNotification()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// MARK: - SnoozeObserver Protocol
|
|
|
|
|
+protocol SnoozeObserver {
|
|
|
|
|
+ func snoozeDidChange(_ untilDate: Date)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, Injectable {
|
|
final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, Injectable {
|
|
|
enum Identifier: String {
|
|
enum Identifier: String {
|
|
|
case glucoseNotification = "Trio.glucoseNotification"
|
|
case glucoseNotification = "Trio.glucoseNotification"
|
|
@@ -61,6 +67,9 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
@Injected(as: FetchGlucoseManager.self) private var sourceInfoProvider: SourceInfoProvider!
|
|
@Injected(as: FetchGlucoseManager.self) private var sourceInfoProvider: SourceInfoProvider!
|
|
|
|
|
|
|
|
@Persisted(key: "UserNotificationsManager.snoozeUntilDate") private var snoozeUntilDate: Date = .distantPast
|
|
@Persisted(key: "UserNotificationsManager.snoozeUntilDate") private var snoozeUntilDate: Date = .distantPast
|
|
|
|
|
+ // The glucose notification observers below (Core Data saves and the storage publisher) can fire for the same
|
|
|
|
|
+ // reading, so we persist the last alert token to avoid enqueueing identical high/low notifications multiple times.
|
|
|
|
|
+ @Persisted(key: "UserNotificationsManager.lastGlucoseAlertToken") private var lastGlucoseAlertToken: String = ""
|
|
|
|
|
|
|
|
private let notificationCenter = UNUserNotificationCenter.current()
|
|
private let notificationCenter = UNUserNotificationCenter.current()
|
|
|
private var lifetime = Lifetime()
|
|
private var lifetime = Lifetime()
|
|
@@ -95,11 +104,48 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
Task {
|
|
Task {
|
|
|
await sendGlucoseNotification()
|
|
await sendGlucoseNotification()
|
|
|
}
|
|
}
|
|
|
|
|
+ configureNotificationCategories()
|
|
|
registerHandlers()
|
|
registerHandlers()
|
|
|
registerSubscribers()
|
|
registerSubscribers()
|
|
|
subscribeOnLoop()
|
|
subscribeOnLoop()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private func configureNotificationCategories() {
|
|
|
|
|
+ notificationCenter.getNotificationCategories { [weak self] existingCategories in
|
|
|
|
|
+ guard let self else { return }
|
|
|
|
|
+
|
|
|
|
|
+ let snoozeActions = NotificationResponseAction.allCases.map { action in
|
|
|
|
|
+ UNNotificationAction(
|
|
|
|
|
+ identifier: action.rawValue,
|
|
|
|
|
+ title: self.title(for: action),
|
|
|
|
|
+ options: []
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let glucoseCategory = UNNotificationCategory(
|
|
|
|
|
+ identifier: NotificationCategoryIdentifier.glucoseAlert.rawValue,
|
|
|
|
|
+ actions: snoozeActions,
|
|
|
|
|
+ intentIdentifiers: [],
|
|
|
|
|
+ options: []
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ var categories = existingCategories
|
|
|
|
|
+ categories.update(with: glucoseCategory)
|
|
|
|
|
+ self.notificationCenter.setNotificationCategories(categories)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private func title(for action: NotificationResponseAction) -> String {
|
|
|
|
|
+ switch action {
|
|
|
|
|
+ case .snooze20:
|
|
|
|
|
+ return String(localized: "Snooze 20 min", comment: "Snooze glucose alerts for 20 minutes")
|
|
|
|
|
+ case .snooze40:
|
|
|
|
|
+ return String(localized: "Snooze 40 min", comment: "Snooze glucose alerts for 40 minutes")
|
|
|
|
|
+ case .snooze60:
|
|
|
|
|
+ return String(localized: "Snooze 1 hr", comment: "Snooze glucose alerts for 60 minutes")
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private func subscribeOnLoop() {
|
|
private func subscribeOnLoop() {
|
|
|
apsManager.lastLoopDateSubject
|
|
apsManager.lastLoopDateSubject
|
|
|
.sink { [weak self] date in
|
|
.sink { [weak self] date in
|
|
@@ -271,6 +317,10 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
try viewContext.existingObject(with: id) as? GlucoseStored
|
|
try viewContext.existingObject(with: id) as? GlucoseStored
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if glucoseStorage.alarm == .none {
|
|
|
|
|
+ lastGlucoseAlertToken = ""
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
guard let lastReading = glucoseObjects.first?.glucose,
|
|
guard let lastReading = glucoseObjects.first?.glucose,
|
|
|
let secondLastReading = glucoseObjects.dropFirst().first?.glucose,
|
|
let secondLastReading = glucoseObjects.dropFirst().first?.glucose,
|
|
|
let lastDirection = glucoseObjects.first?.directionEnum?.symbol else { return }
|
|
let lastDirection = glucoseObjects.first?.directionEnum?.symbol else { return }
|
|
@@ -305,6 +355,10 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
titles.append(String(localized: "(Snoozed)", comment: "(Snoozed)"))
|
|
titles.append(String(localized: "(Snoozed)", comment: "(Snoozed)"))
|
|
|
notificationAlarm = false
|
|
notificationAlarm = false
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+ let token = alertToken(from: glucoseObjects.first)
|
|
|
|
|
+ if notificationAlarm, token == lastGlucoseAlertToken {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
titles.append(body)
|
|
titles.append(body)
|
|
|
let content = UNMutableNotificationContent()
|
|
let content = UNMutableNotificationContent()
|
|
|
content.title = titles.joined(separator: " ")
|
|
content.title = titles.joined(separator: " ")
|
|
@@ -313,6 +367,7 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
if notificationAlarm {
|
|
if notificationAlarm {
|
|
|
content.sound = .default
|
|
content.sound = .default
|
|
|
content.userInfo[NotificationAction.key] = NotificationAction.snooze.rawValue
|
|
content.userInfo[NotificationAction.key] = NotificationAction.snooze.rawValue
|
|
|
|
|
+ content.categoryIdentifier = NotificationCategoryIdentifier.glucoseAlert.rawValue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
addRequest(
|
|
addRequest(
|
|
@@ -323,6 +378,9 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
messageSubtype: .glucose,
|
|
messageSubtype: .glucose,
|
|
|
action: NotificationAction.snooze
|
|
action: NotificationAction.snooze
|
|
|
)
|
|
)
|
|
|
|
|
+ if notificationAlarm {
|
|
|
|
|
+ lastGlucoseAlertToken = token
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
} catch {
|
|
} catch {
|
|
|
debugPrint(
|
|
debugPrint(
|
|
@@ -331,6 +389,18 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private func alertToken(from glucose: GlucoseStored?) -> String {
|
|
|
|
|
+ if let id = glucose?.id?.uuidString {
|
|
|
|
|
+ return id
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if let date = glucose?.date {
|
|
|
|
|
+ return "date-\(date.timeIntervalSince1970)"
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return UUID().uuidString
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private func glucoseText(glucoseValue: Int, delta: Int?, direction: String?) -> String {
|
|
private func glucoseText(glucoseValue: Int, delta: Int?, direction: String?) -> String {
|
|
|
let units = settingsManager.settings.units
|
|
let units = settingsManager.settings.units
|
|
|
let glucoseText = glucoseFormatter
|
|
let glucoseText = glucoseFormatter
|
|
@@ -409,6 +479,18 @@ final class BaseUserNotificationsManager: NSObject, UserNotificationsManager, In
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @MainActor func applySnooze(for duration: TimeInterval) async {
|
|
|
|
|
+ let untilDate = Date().addingTimeInterval(duration)
|
|
|
|
|
+ snoozeUntilDate = untilDate
|
|
|
|
|
+ lastGlucoseAlertToken = ""
|
|
|
|
|
+ removeGlucoseNotifications()
|
|
|
|
|
+
|
|
|
|
|
+ // Notify observers that snooze was applied
|
|
|
|
|
+ broadcaster.notify(SnoozeObserver.self, on: .main) { (observer: SnoozeObserver) in
|
|
|
|
|
+ observer.snoozeDidChange(untilDate)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private func addRequest(
|
|
private func addRequest(
|
|
|
identifier: Identifier,
|
|
identifier: Identifier,
|
|
|
content: UNMutableNotificationContent,
|
|
content: UNMutableNotificationContent,
|
|
@@ -571,6 +653,12 @@ extension BaseUserNotificationsManager: pumpNotificationObserver {
|
|
|
self.notificationCenter.removePendingNotificationRequests(withIdentifiers: [identifier.rawValue])
|
|
self.notificationCenter.removePendingNotificationRequests(withIdentifiers: [identifier.rawValue])
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ private func removeGlucoseNotifications() {
|
|
|
|
|
+ let identifier = Identifier.glucoseNotification.rawValue
|
|
|
|
|
+ notificationCenter.removeDeliveredNotifications(withIdentifiers: [identifier])
|
|
|
|
|
+ notificationCenter.removePendingNotificationRequests(withIdentifiers: [identifier])
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
extension BaseUserNotificationsManager: DeterminationObserver {
|
|
extension BaseUserNotificationsManager: DeterminationObserver {
|
|
@@ -601,6 +689,14 @@ extension BaseUserNotificationsManager: UNUserNotificationCenterDelegate {
|
|
|
withCompletionHandler completionHandler: @escaping () -> Void
|
|
withCompletionHandler completionHandler: @escaping () -> Void
|
|
|
) {
|
|
) {
|
|
|
defer { completionHandler() }
|
|
defer { completionHandler() }
|
|
|
|
|
+
|
|
|
|
|
+ if let quickAction = NotificationResponseAction(rawValue: response.actionIdentifier) {
|
|
|
|
|
+ Task { @MainActor in
|
|
|
|
|
+ await self.applySnooze(for: quickAction.duration)
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
guard let actionRaw = response.notification.request.content.userInfo[NotificationAction.key] as? String,
|
|
guard let actionRaw = response.notification.request.content.userInfo[NotificationAction.key] as? String,
|
|
|
let action = NotificationAction(rawValue: actionRaw)
|
|
let action = NotificationAction(rawValue: actionRaw)
|
|
|
else { return }
|
|
else { return }
|