Skip to content

Commit 2c1d82f

Browse files
authored
Add change context for the LoopDataUpdatedNotification (#112)
* Add change context for the LoopDataUpdatedNotification for @ps2 * Using the loop update context in WatchDataManager cc @ps2
1 parent 664a4a3 commit 2c1d82f

File tree

4 files changed

+41
-25
lines changed

4 files changed

+41
-25
lines changed

Loop/Extensions/BatteryIndicator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ extension BatteryIndicator {
2020
return nil
2121
}
2222
}
23-
}
23+
}

Loop/Managers/DeviceDataManager.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,6 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate, ReceiverDelegat
853853

854854
private(set) var watchManager: WatchDataManager!
855855

856-
@objc private func loopDataDidUpdateNotification(_: NSNotification) {
857-
watchManager.updateWatch()
858-
}
859-
860856
// MARK: - Initialization
861857

862858
static let sharedManager = DeviceDataManager()
@@ -913,8 +909,6 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate, ReceiverDelegat
913909
}
914910

915911
loopManager = LoopDataManager(deviceDataManager: self)
916-
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(loopDataDidUpdateNotification(_:)), name: LoopDataManager.LoopDataUpdatedNotification, object: loopManager)
917-
918912
watchManager = WatchDataManager(deviceDataManager: self)
919913

920914
carbStore?.delegate = self

Loop/Managers/LoopDataManager.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@ import MinimedKit
1414

1515

1616
class LoopDataManager {
17+
enum LoopUpdateContext: Int {
18+
case Bolus
19+
case Carbs
20+
case Glucose
21+
case Preferences
22+
case TempBasal
23+
}
24+
1725
static let LoopDataUpdatedNotification = "com.loudnate.Naterade.notification.LoopDataUpdated"
1826

1927
static let LoopRunningNotification = "com.loudnate.Naterade.notification.LoopRunning"
2028

29+
static let LoopUpdateContextKey = "com.loudnate.Loop.LoopDataManager.LoopUpdateContext"
30+
2131
typealias TempBasalRecommendation = (recommendedDate: NSDate, rate: Double, duration: NSTimeInterval)
2232

2333
unowned let deviceDataManager: DeviceDataManager
@@ -26,7 +36,7 @@ class LoopDataManager {
2636
didSet {
2737
NSUserDefaults.standardUserDefaults().dosingEnabled = dosingEnabled
2838

29-
notify()
39+
notify(forChange: .Preferences)
3040
}
3141
}
3242

@@ -47,7 +57,7 @@ class LoopDataManager {
4757
center.addObserverForName(DeviceDataManager.GlucoseUpdatedNotification, object: deviceDataManager, queue: nil) { (note) -> Void in
4858
dispatch_async(self.dataAccessQueue) {
4959
self.glucoseMomentumEffect = nil
50-
self.notify()
60+
self.notify(forChange: .Glucose)
5161
}
5262
},
5363
center.addObserverForName(DeviceDataManager.PumpStatusUpdatedNotification, object: deviceDataManager, queue: nil) { (note) -> Void in
@@ -61,7 +71,7 @@ class LoopDataManager {
6171
notificationObservers.append(center.addObserverForName(CarbStore.CarbEntriesDidUpdateNotification, object: nil, queue: nil) { (note) -> Void in
6272
dispatch_async(self.dataAccessQueue) {
6373
self.carbEffect = nil
64-
self.notify()
74+
self.notify(forChange: .Carbs)
6575
}
6676
})
6777
}
@@ -84,7 +94,7 @@ class LoopDataManager {
8494
self.lastLoopCompleted = NSDate()
8595
}
8696

87-
self.notify()
97+
self.notify(forChange: .TempBasal)
8898
}
8999

90100
// Delay the notification until we know the result of the temp basal
@@ -96,7 +106,7 @@ class LoopDataManager {
96106
lastLoopError = error
97107
}
98108

99-
notify()
109+
notify(forChange: .TempBasal)
100110
}
101111

102112
// References to registered notification center observers
@@ -160,8 +170,11 @@ class LoopDataManager {
160170
}
161171
}
162172

163-
private func notify() {
164-
NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.LoopDataUpdatedNotification, object: self)
173+
private func notify(forChange context: LoopUpdateContext) {
174+
NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.LoopDataUpdatedNotification,
175+
object: self,
176+
userInfo: [self.dynamicType.LoopUpdateContextKey: context.rawValue]
177+
)
165178
}
166179

167180
/**
@@ -481,7 +494,7 @@ class LoopDataManager {
481494
func recordBolus(units: Double, atDate date: NSDate) {
482495
dispatch_async(dataAccessQueue) {
483496
self.lastBolus = (units: units, date: date)
484-
self.notify()
497+
self.notify(forChange: .Bolus)
485498
}
486499
}
487500
}

Loop/Managers/WatchDataManager.swift

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class WatchDataManager: NSObject, WCSessionDelegate {
2323

2424
super.init()
2525

26+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(updateWatch(_:)), name: LoopDataManager.LoopDataUpdatedNotification, object: deviceDataManager.loopManager)
27+
2628
watchSession?.delegate = self
2729
watchSession?.activateSession()
2830
}
@@ -35,16 +37,23 @@ class WatchDataManager: NSObject, WCSessionDelegate {
3537
}
3638
}()
3739

38-
func updateWatch() {
39-
if let session = watchSession {
40-
switch session.activationState {
41-
case .NotActivated, .Inactive:
42-
session.activateSession()
43-
case .Activated:
44-
createWatchContext { (context) in
45-
if let context = context {
46-
self.sendWatchContext(context)
47-
}
40+
@objc private func updateWatch(notification: NSNotification) {
41+
guard
42+
let rawContext = notification.userInfo?[LoopDataManager.LoopUpdateContextKey] as? LoopDataManager.LoopUpdateContext.RawValue,
43+
let context = LoopDataManager.LoopUpdateContext(rawValue: rawContext),
44+
case .TempBasal = context,
45+
let session = watchSession
46+
else {
47+
return
48+
}
49+
50+
switch session.activationState {
51+
case .NotActivated, .Inactive:
52+
session.activateSession()
53+
case .Activated:
54+
createWatchContext { (context) in
55+
if let context = context {
56+
self.sendWatchContext(context)
4857
}
4958
}
5059
}

0 commit comments

Comments
 (0)