Skip to content

Commit 45add01

Browse files
committed
iOS 10 UserNotifications.framework
Fixes #170
1 parent 8eb3036 commit 45add01

File tree

5 files changed

+58
-59
lines changed

5 files changed

+58
-59
lines changed

Loop/AppDelegate.swift

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,25 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
6161

6262
}
6363

64-
// MARK: - Notifications
64+
// MARK: - 3D Touch
6565

66-
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
67-
if application.applicationState == .active {
68-
if let message = notification.alertBody {
69-
window?.rootViewController?.presentAlertController(withTitle: notification.alertTitle, message: message, animated: true, completion: nil)
70-
}
71-
}
66+
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
67+
completionHandler(false)
7268
}
69+
}
7370

74-
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, withResponseInfo responseInfo: [AnyHashable: Any], completionHandler: @escaping () -> Void) {
7571

76-
switch identifier {
77-
case NotificationManager.Action.RetryBolus.rawValue?:
78-
if let units = notification.userInfo?[NotificationManager.UserInfoKey.BolusAmount.rawValue] as? Double,
79-
let startDate = notification.userInfo?[NotificationManager.UserInfoKey.BolusStartDate.rawValue] as? Date,
72+
extension AppDelegate: UNUserNotificationCenterDelegate {
73+
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
74+
switch response.actionIdentifier {
75+
case NotificationManager.Action.RetryBolus.rawValue:
76+
if let units = response.notification.request.content.userInfo[NotificationManager.UserInfoKey.BolusAmount.rawValue] as? Double,
77+
let startDate = response.notification.request.content.userInfo[NotificationManager.UserInfoKey.BolusStartDate.rawValue] as? Date,
8078
startDate.timeIntervalSinceNow >= TimeInterval(minutes: -5)
8179
{
8280
AnalyticsManager.sharedManager.didRetryBolus()
8381

84-
dataManager.enactBolus(units) { (error) in
82+
dataManager.enactBolus(units: units) { (error) in
8583
if error != nil {
8684
NotificationManager.sendBolusFailureNotificationForAmount(units, atDate: startDate)
8785
}
@@ -93,23 +91,10 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
9391
default:
9492
break
9593
}
96-
94+
9795
completionHandler()
9896
}
9997

100-
// MARK: - 3D Touch
101-
102-
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
103-
completionHandler(false)
104-
}
105-
}
106-
107-
108-
extension AppDelegate: UNUserNotificationCenterDelegate {
109-
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
110-
111-
}
112-
11398
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
11499
completionHandler([.badge, .sound, .alert])
115100
}

Loop/Managers/DeviceDataManager.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,12 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter
360360
}
361361
}
362362

363-
/**
364-
Send a bolus command and handle the result
365-
366-
- parameter completion: A closure called after the command is complete. This closure takes a single argument:
367-
- error: An error describing why the command failed
368-
*/
369-
func enactBolus(_ units: Double, completion: @escaping (_ error: Error?) -> Void) {
363+
/// Send a bolus command and handle the result
364+
///
365+
/// - parameter units: The number of units to deliver
366+
/// - parameter completion: A clsure called after the command is complete. This closure takes a single argument:
367+
/// - error: An error describing why the command failed
368+
func enactBolus(units: Double, completion: @escaping (_ error: Error?) -> Void) {
370369
guard units > 0 else {
371370
completion(nil)
372371
return
@@ -442,9 +441,7 @@ final class DeviceDataManager: CarbStoreDelegate, DoseStoreDelegate, Transmitter
442441
}
443442

444443
// MARK: - G5 Transmitter
445-
/**
446-
The G5 transmitter is a reliable heartbeat by which we can assert the loop state.
447-
*/
444+
/// The G5 transmitter is a reliable heartbeat by which we can assert the loop state.
448445

449446
// MARK: TransmitterDelegate
450447

Loop/Managers/NotificationManager.swift

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,33 +124,44 @@ struct NotificationManager {
124124
}
125125

126126
static func sendPumpBatteryLowNotification() {
127-
let notification = UILocalNotification()
127+
let notification = UNMutableNotificationContent()
128+
129+
notification.title = NSLocalizedString("Pump Battery Low", comment: "The notification title for a low pump battery")
130+
notification.body = NSLocalizedString("Change the pump battery immediately", comment: "The notification alert describing a low pump battery")
131+
notification.sound = UNNotificationSound.default()
132+
notification.categoryIdentifier = Category.PumpBatteryLow.rawValue
128133

129-
notification.alertTitle = NSLocalizedString("Pump Battery Low", comment: "The notification title for a low pump battery")
130-
notification.alertBody = NSLocalizedString("Change the pump battery immediately", comment: "The notification alert describing a low pump battery")
131-
notification.soundName = UILocalNotificationDefaultSoundName
132-
notification.category = Category.PumpBatteryLow.rawValue
134+
let request = UNNotificationRequest(
135+
identifier: Category.PumpBatteryLow.rawValue,
136+
content: notification,
137+
trigger: nil
138+
)
133139

134-
UIApplication.shared.presentLocalNotificationNow(notification)
140+
UNUserNotificationCenter.current().add(request)
135141
}
136142

137143
static func sendPumpReservoirEmptyNotification() {
138-
let notification = UILocalNotification()
144+
let notification = UNMutableNotificationContent()
139145

140-
notification.alertTitle = NSLocalizedString("Pump Reservoir Empty", comment: "The notification title for an empty pump reservoir")
141-
notification.alertBody = NSLocalizedString("Change the pump reservoir now", comment: "The notification alert describing an empty pump reservoir")
142-
notification.soundName = UILocalNotificationDefaultSoundName
143-
notification.category = Category.PumpReservoirEmpty.rawValue
146+
notification.title = NSLocalizedString("Pump Reservoir Empty", comment: "The notification title for an empty pump reservoir")
147+
notification.body = NSLocalizedString("Change the pump reservoir now", comment: "The notification alert describing an empty pump reservoir")
148+
notification.sound = UNNotificationSound.default()
149+
notification.categoryIdentifier = Category.PumpReservoirEmpty.rawValue
144150

145-
// TODO: Add an action to Suspend the pump
151+
let request = UNNotificationRequest(
152+
// Not a typo: this should replace any pump reservoir low notifications
153+
identifier: Category.PumpReservoirLow.rawValue,
154+
content: notification,
155+
trigger: nil
156+
)
146157

147-
UIApplication.shared.presentLocalNotificationNow(notification)
158+
UNUserNotificationCenter.current().add(request)
148159
}
149160

150161
static func sendPumpReservoirLowNotificationForAmount(_ units: Double, andTimeRemaining remaining: TimeInterval?) {
151-
let notification = UILocalNotification()
162+
let notification = UNMutableNotificationContent()
152163

153-
notification.alertTitle = NSLocalizedString("Pump Reservoir Low", comment: "The notification title for a low pump reservoir")
164+
notification.title = NSLocalizedString("Pump Reservoir Low", comment: "The notification title for a low pump reservoir")
154165

155166
let unitsString = NumberFormatter.localizedString(from: NSNumber(value: units), number: .decimal)
156167

@@ -162,14 +173,20 @@ struct NotificationManager {
162173
intervalFormatter.includesTimeRemainingPhrase = true
163174

164175
if let remaining = remaining, let timeString = intervalFormatter.string(from: remaining) {
165-
notification.alertBody = String(format: NSLocalizedString("%1$@ U left: %2$@", comment: "Low reservoir alert with time remaining format string. (1: Number of units remaining)(2: approximate time remaining)"), unitsString, timeString)
176+
notification.body = String(format: NSLocalizedString("%1$@ U left: %2$@", comment: "Low reservoir alert with time remaining format string. (1: Number of units remaining)(2: approximate time remaining)"), unitsString, timeString)
166177
} else {
167-
notification.alertBody = String(format: NSLocalizedString("%1$@ U left", comment: "Low reservoir alert format string. (1: Number of units remaining)"), unitsString)
178+
notification.body = String(format: NSLocalizedString("%1$@ U left", comment: "Low reservoir alert format string. (1: Number of units remaining)"), unitsString)
168179
}
169180

170-
notification.soundName = UILocalNotificationDefaultSoundName
171-
notification.category = Category.PumpReservoirLow.rawValue
181+
notification.sound = UNNotificationSound.default()
182+
notification.categoryIdentifier = Category.PumpReservoirLow.rawValue
172183

173-
UIApplication.shared.presentLocalNotificationNow(notification)
184+
let request = UNNotificationRequest(
185+
identifier: Category.PumpReservoirLow.rawValue,
186+
content: notification,
187+
trigger: nil
188+
)
189+
190+
UNUserNotificationCenter.current().add(request)
174191
}
175192
}

Loop/Managers/WatchDataManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ final class WatchDataManager: NSObject, WCSessionDelegate {
160160
}
161161
case SetBolusUserInfo.name?:
162162
if let bolus = SetBolusUserInfo(rawValue: message as SetBolusUserInfo.RawValue) {
163-
self.deviceDataManager.enactBolus(bolus.value) { (error) in
163+
self.deviceDataManager.enactBolus(units: bolus.value) { (error) in
164164
if error != nil {
165165
NotificationManager.sendBolusFailureNotificationForAmount(bolus.value, atDate: bolus.startDate)
166166
} else {

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ final class StatusTableViewController: UITableViewController, UIGestureRecognize
596596
if let bolusViewController = segue.source as? BolusViewController {
597597
if let bolus = bolusViewController.bolus, bolus > 0 {
598598
let startDate = Date()
599-
dataManager.enactBolus(bolus) { (error) in
599+
dataManager.enactBolus(units: bolus) { (error) in
600600
if error != nil {
601601
NotificationManager.sendBolusFailureNotificationForAmount(bolus, atDate: startDate)
602602
}

0 commit comments

Comments
 (0)