@@ -63,12 +63,25 @@ struct NotificationManager {
6363
6464 notification. title = NSLocalizedString ( " Bolus " , comment: " The notification title for a bolus failure " )
6565
66+ let sentenceFormat = NSLocalizedString ( " %@. " , comment: " Appends a full-stop to a statement " )
67+
6668 switch error {
6769 case let error as SetBolusError :
6870 notification. subtitle = error. errorDescriptionWithUnits ( units)
69- notification. body = String ( format: " %@ %@ " , error. failureReason!, error. recoverySuggestion!)
71+
72+ let body = [ error. failureReason, error. recoverySuggestion] . compactMap ( { $0 } ) . map ( {
73+ String ( format: sentenceFormat, $0)
74+ } ) . joined ( separator: " " )
75+
76+ notification. body = body
7077 case let error as LocalizedError :
71- notification. body = error. errorDescription ?? error. localizedDescription
78+ if let subtitle = error. errorDescription {
79+ notification. subtitle = subtitle
80+ }
81+ let message = [ error. failureReason, error. recoverySuggestion] . compactMap ( { $0 } ) . map ( {
82+ String ( format: sentenceFormat, $0)
83+ } ) . joined ( separator: " \n " )
84+ notification. body = message. isEmpty ? String ( describing: error) : message
7285 default :
7386 notification. body = error. localizedDescription
7487 }
@@ -134,6 +147,19 @@ struct NotificationManager {
134147 }
135148 }
136149
150+ static func clearLoopNotRunningNotifications( ) {
151+ // Clear out any existing not-running notifications
152+ UNUserNotificationCenter . current ( ) . getDeliveredNotifications { ( notifications) in
153+ let loopNotRunningIdentifiers = notifications. filter ( {
154+ $0. request. content. categoryIdentifier == Category . loopNotRunning. rawValue
155+ } ) . map ( {
156+ $0. request. identifier
157+ } )
158+
159+ UNUserNotificationCenter . current ( ) . removeDeliveredNotifications ( withIdentifiers: loopNotRunningIdentifiers)
160+ }
161+ }
162+
137163 static func sendPumpBatteryLowNotification( ) {
138164 let notification = UNMutableNotificationContent ( )
139165
@@ -151,6 +177,10 @@ struct NotificationManager {
151177 UNUserNotificationCenter . current ( ) . add ( request)
152178 }
153179
180+ static func clearPumpBatteryLowNotification( ) {
181+ UNUserNotificationCenter . current ( ) . removeDeliveredNotifications ( withIdentifiers: [ Category . pumpBatteryLow. rawValue] )
182+ }
183+
154184 static func sendPumpReservoirEmptyNotification( ) {
155185 let notification = UNMutableNotificationContent ( )
156186
@@ -200,4 +230,8 @@ struct NotificationManager {
200230
201231 UNUserNotificationCenter . current ( ) . add ( request)
202232 }
233+
234+ static func clearPumpReservoirNotification( ) {
235+ UNUserNotificationCenter . current ( ) . removeDeliveredNotifications ( withIdentifiers: [ Category . pumpReservoirLow. rawValue] )
236+ }
203237}
0 commit comments