99import UIKit
1010import UserNotifications
1111
12+ import RileyLinkKit
13+
1214
1315struct NotificationManager {
1416 enum Category : String {
15- case BolusFailure
16- case LoopNotRunning
17- case PumpBatteryLow
18- case PumpReservoirEmpty
19- case PumpReservoirLow
17+ case bolusFailure
18+ case loopNotRunning
19+ case pumpBatteryLow
20+ case pumpReservoirEmpty
21+ case pumpReservoirLow
2022 }
2123
2224 enum Action : String {
23- case RetryBolus
25+ case retryBolus
2426 }
2527
2628 enum UserInfoKey : String {
27- case BolusAmount
28- case BolusStartDate
29+ case bolusAmount
30+ case bolusStartDate
2931 }
3032
3133 private static var notificationCategories : Set < UNNotificationCategory > {
3234 var categories = [ UNNotificationCategory] ( )
3335
3436 let retryBolusAction = UNNotificationAction (
35- identifier: Action . RetryBolus . rawValue,
37+ identifier: Action . retryBolus . rawValue,
3638 title: NSLocalizedString ( " Retry " , comment: " The title of the notification action to retry a bolus command " ) ,
3739 options: [ ]
3840 )
3941
4042 categories. append ( UNNotificationCategory (
41- identifier: Category . BolusFailure . rawValue,
43+ identifier: Category . bolusFailure . rawValue,
4244 actions: [ retryBolusAction] ,
4345 intentIdentifiers: [ ] ,
4446 options: [ ]
@@ -57,25 +59,35 @@ struct NotificationManager {
5759
5860 // MARK: - Notifications
5961
60- static func sendBolusFailureNotificationForAmount ( _ units: Double , atStartDate startDate: Date ) {
62+ static func sendBolusFailureNotification ( for error : Error , units: Double , at startDate: Date ) {
6163 let notification = UNMutableNotificationContent ( )
6264
6365 notification. title = NSLocalizedString ( " Bolus " , comment: " The notification title for a bolus failure " )
64- notification. body = String ( format: NSLocalizedString ( " %@ U bolus may have failed. " , comment: " The notification alert describing a possible bolus failure. The substitution parameter is the size of the bolus in units. " ) , NumberFormatter . localizedString ( from: NSNumber ( value: units) , number: . decimal) )
66+
67+ switch error {
68+ case let error as RileyLinkKit . SetBolusError :
69+ notification. subtitle = error. errorDescriptionWithUnits ( units)
70+ notification. body = String ( format: " %@ %@ " , error. failureReason!, error. recoverySuggestion!)
71+ case let error as LocalizedError :
72+ notification. body = error. errorDescription ?? error. localizedDescription
73+ default :
74+ notification. body = error. localizedDescription
75+ }
76+
6577 notification. sound = UNNotificationSound . default ( )
6678
6779 if startDate. timeIntervalSinceNow >= TimeInterval ( minutes: - 5 ) {
68- notification. categoryIdentifier = Category . BolusFailure . rawValue
80+ notification. categoryIdentifier = Category . bolusFailure . rawValue
6981 }
7082
7183 notification. userInfo = [
72- UserInfoKey . BolusAmount . rawValue: units,
73- UserInfoKey . BolusStartDate . rawValue: startDate
84+ UserInfoKey . bolusAmount . rawValue: units,
85+ UserInfoKey . bolusStartDate . rawValue: startDate
7486 ]
7587
7688 let request = UNNotificationRequest (
7789 // Only support 1 bolus notification at once
78- identifier: Category . BolusFailure . rawValue,
90+ identifier: Category . bolusFailure . rawValue,
7991 content: notification,
8092 trigger: nil
8193 )
@@ -107,11 +119,11 @@ struct NotificationManager {
107119
108120 notification. title = NSLocalizedString ( " Loop Failure " , comment: " The notification title for a loop failure " )
109121 notification. sound = UNNotificationSound . default ( )
110- notification. categoryIdentifier = Category . LoopNotRunning . rawValue
111- notification. threadIdentifier = Category . LoopNotRunning . rawValue
122+ notification. categoryIdentifier = Category . loopNotRunning . rawValue
123+ notification. threadIdentifier = Category . loopNotRunning . rawValue
112124
113125 let request = UNNotificationRequest (
114- identifier: " \( Category . LoopNotRunning . rawValue) \( failureInterval) " ,
126+ identifier: " \( Category . loopNotRunning . rawValue) \( failureInterval) " ,
115127 content: notification,
116128 trigger: UNTimeIntervalNotificationTrigger (
117129 timeInterval: failureInterval + gracePeriod,
@@ -129,10 +141,10 @@ struct NotificationManager {
129141 notification. title = NSLocalizedString ( " Pump Battery Low " , comment: " The notification title for a low pump battery " )
130142 notification. body = NSLocalizedString ( " Change the pump battery immediately " , comment: " The notification alert describing a low pump battery " )
131143 notification. sound = UNNotificationSound . default ( )
132- notification. categoryIdentifier = Category . PumpBatteryLow . rawValue
144+ notification. categoryIdentifier = Category . pumpBatteryLow . rawValue
133145
134146 let request = UNNotificationRequest (
135- identifier: Category . PumpBatteryLow . rawValue,
147+ identifier: Category . pumpBatteryLow . rawValue,
136148 content: notification,
137149 trigger: nil
138150 )
@@ -146,11 +158,11 @@ struct NotificationManager {
146158 notification. title = NSLocalizedString ( " Pump Reservoir Empty " , comment: " The notification title for an empty pump reservoir " )
147159 notification. body = NSLocalizedString ( " Change the pump reservoir now " , comment: " The notification alert describing an empty pump reservoir " )
148160 notification. sound = UNNotificationSound . default ( )
149- notification. categoryIdentifier = Category . PumpReservoirEmpty . rawValue
161+ notification. categoryIdentifier = Category . pumpReservoirEmpty . rawValue
150162
151163 let request = UNNotificationRequest (
152164 // Not a typo: this should replace any pump reservoir low notifications
153- identifier: Category . PumpReservoirLow . rawValue,
165+ identifier: Category . pumpReservoirLow . rawValue,
154166 content: notification,
155167 trigger: nil
156168 )
@@ -179,10 +191,10 @@ struct NotificationManager {
179191 }
180192
181193 notification. sound = UNNotificationSound . default ( )
182- notification. categoryIdentifier = Category . PumpReservoirLow . rawValue
194+ notification. categoryIdentifier = Category . pumpReservoirLow . rawValue
183195
184196 let request = UNNotificationRequest (
185- identifier: Category . PumpReservoirLow . rawValue,
197+ identifier: Category . pumpReservoirLow . rawValue,
186198 content: notification,
187199 trigger: nil
188200 )
0 commit comments