Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Loop/Extensions/BatteryIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ extension BatteryIndicator {
return nil
}
}
}
}
6 changes: 0 additions & 6 deletions Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -853,10 +853,6 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate, ReceiverDelegat

private(set) var watchManager: WatchDataManager!

@objc private func loopDataDidUpdateNotification(_: NSNotification) {
watchManager.updateWatch()
}

// MARK: - Initialization

static let sharedManager = DeviceDataManager()
Expand Down Expand Up @@ -913,8 +909,6 @@ class DeviceDataManager: CarbStoreDelegate, TransmitterDelegate, ReceiverDelegat
}

loopManager = LoopDataManager(deviceDataManager: self)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(loopDataDidUpdateNotification(_:)), name: LoopDataManager.LoopDataUpdatedNotification, object: loopManager)

watchManager = WatchDataManager(deviceDataManager: self)

carbStore?.delegate = self
Expand Down
29 changes: 21 additions & 8 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@ import MinimedKit


class LoopDataManager {
enum LoopUpdateContext: Int {
case Bolus
case Carbs
case Glucose
case Preferences
case TempBasal
}

static let LoopDataUpdatedNotification = "com.loudnate.Naterade.notification.LoopDataUpdated"

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

static let LoopUpdateContextKey = "com.loudnate.Loop.LoopDataManager.LoopUpdateContext"

typealias TempBasalRecommendation = (recommendedDate: NSDate, rate: Double, duration: NSTimeInterval)

unowned let deviceDataManager: DeviceDataManager
Expand All @@ -26,7 +36,7 @@ class LoopDataManager {
didSet {
NSUserDefaults.standardUserDefaults().dosingEnabled = dosingEnabled

notify()
notify(forChange: .Preferences)
}
}

Expand All @@ -47,7 +57,7 @@ class LoopDataManager {
center.addObserverForName(DeviceDataManager.GlucoseUpdatedNotification, object: deviceDataManager, queue: nil) { (note) -> Void in
dispatch_async(self.dataAccessQueue) {
self.glucoseMomentumEffect = nil
self.notify()
self.notify(forChange: .Glucose)
}
},
center.addObserverForName(DeviceDataManager.PumpStatusUpdatedNotification, object: deviceDataManager, queue: nil) { (note) -> Void in
Expand All @@ -61,7 +71,7 @@ class LoopDataManager {
notificationObservers.append(center.addObserverForName(CarbStore.CarbEntriesDidUpdateNotification, object: nil, queue: nil) { (note) -> Void in
dispatch_async(self.dataAccessQueue) {
self.carbEffect = nil
self.notify()
self.notify(forChange: .Carbs)
}
})
}
Expand All @@ -84,7 +94,7 @@ class LoopDataManager {
self.lastLoopCompleted = NSDate()
}

self.notify()
self.notify(forChange: .TempBasal)
}

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

notify()
notify(forChange: .TempBasal)
}

// References to registered notification center observers
Expand Down Expand Up @@ -160,8 +170,11 @@ class LoopDataManager {
}
}

private func notify() {
NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.LoopDataUpdatedNotification, object: self)
private func notify(forChange context: LoopUpdateContext) {
NSNotificationCenter.defaultCenter().postNotificationName(self.dynamicType.LoopDataUpdatedNotification,
object: self,
userInfo: [self.dynamicType.LoopUpdateContextKey: context.rawValue]
)
}

/**
Expand Down Expand Up @@ -481,7 +494,7 @@ class LoopDataManager {
func recordBolus(units: Double, atDate date: NSDate) {
dispatch_async(dataAccessQueue) {
self.lastBolus = (units: units, date: date)
self.notify()
self.notify(forChange: .Bolus)
}
}
}
29 changes: 19 additions & 10 deletions Loop/Managers/WatchDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class WatchDataManager: NSObject, WCSessionDelegate {

super.init()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(updateWatch(_:)), name: LoopDataManager.LoopDataUpdatedNotification, object: deviceDataManager.loopManager)

watchSession?.delegate = self
watchSession?.activateSession()
}
Expand All @@ -35,16 +37,23 @@ class WatchDataManager: NSObject, WCSessionDelegate {
}
}()

func updateWatch() {
if let session = watchSession {
switch session.activationState {
case .NotActivated, .Inactive:
session.activateSession()
case .Activated:
createWatchContext { (context) in
if let context = context {
self.sendWatchContext(context)
}
@objc private func updateWatch(notification: NSNotification) {
guard
let rawContext = notification.userInfo?[LoopDataManager.LoopUpdateContextKey] as? LoopDataManager.LoopUpdateContext.RawValue,
let context = LoopDataManager.LoopUpdateContext(rawValue: rawContext),
case .TempBasal = context,
let session = watchSession
else {
return
}

switch session.activationState {
case .NotActivated, .Inactive:
session.activateSession()
case .Activated:
createWatchContext { (context) in
if let context = context {
self.sendWatchContext(context)
}
}
}
Expand Down