Skip to content

Remote overrides #1131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 13, 2019
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
4 changes: 2 additions & 2 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github "LoopKit/Amplitude-iOS" "2137d5fd44bf630ed33e1e72d7af6d8f8612f270"
github "LoopKit/CGMBLEKit" "7417605dd898bf89378171941126c85d85dc642c"
github "LoopKit/G4ShareSpy" "e62d296067180c6659166272ff9cc406f470ec9e"
github "LoopKit/LoopKit" "18a5a04afd310e945ac54f8c43a44838a16503c2"
github "LoopKit/LoopKit" "a08405ec7a4e38fa96e39ae079114db30f393a2a"
github "LoopKit/MKRingProgressView" "f548a5c64832be2d37d7c91b5800e284887a2a0a"
github "LoopKit/dexcom-share-client-swift" "c4f3d48e56e5b3ad786486ccd1bbc753034096d2"
github "i-schuetz/SwiftCharts" "0.6.5"
github "ps2/rileylink_ios" "4b756c126317320209474f34e909ecf254d8a5b9"
github "ps2/rileylink_ios" "51d6a20cc63c0c0b2071a4a78b4065e9a11ba81f"
4 changes: 4 additions & 0 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@
C136AA2423109CC6008A320D /* LoopPlugins.swift in Sources */ = {isa = PBXBuildFile; fileRef = C16DA84122E8E112008624C2 /* LoopPlugins.swift */; };
C13BAD941E8009B000050CB5 /* NumberFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43BFF0B31E45C1BE00FF19A9 /* NumberFormatter.swift */; };
C15713821DAC6983005BC4D2 /* MealBolusNightscoutTreatment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C15713811DAC6983005BC4D2 /* MealBolusNightscoutTreatment.swift */; };
C165B8CE23302C5D0004112E /* RemoteCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C165B8CD23302C5D0004112E /* RemoteCommand.swift */; };
C16DA84222E8E112008624C2 /* LoopPlugins.swift in Sources */ = {isa = PBXBuildFile; fileRef = C16DA84122E8E112008624C2 /* LoopPlugins.swift */; };
C178249A1E1999FA00D9D25C /* CaseCountable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C17824991E1999FA00D9D25C /* CaseCountable.swift */; };
C17824A01E19CF9800D9D25C /* GlucoseThresholdTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C178249F1E19CF9800D9D25C /* GlucoseThresholdTableViewController.swift */; };
Expand Down Expand Up @@ -1024,6 +1025,7 @@
C12CB9B823106A6300F84978 /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/Intents.strings; sourceTree = "<group>"; };
C12F21A61DFA79CB00748193 /* recommend_temp_basal_very_low_end_in_range.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = recommend_temp_basal_very_low_end_in_range.json; sourceTree = "<group>"; };
C15713811DAC6983005BC4D2 /* MealBolusNightscoutTreatment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MealBolusNightscoutTreatment.swift; sourceTree = "<group>"; };
C165B8CD23302C5D0004112E /* RemoteCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteCommand.swift; sourceTree = "<group>"; };
C16DA84122E8E112008624C2 /* LoopPlugins.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoopPlugins.swift; sourceTree = "<group>"; };
C17824991E1999FA00D9D25C /* CaseCountable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CaseCountable.swift; sourceTree = "<group>"; };
C178249F1E19CF9800D9D25C /* GlucoseThresholdTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseThresholdTableViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1239,6 +1241,7 @@
438D42F81D7C88BC003244B0 /* PredictionInputEffect.swift */,
43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */,
4328E0311CFC068900E199AA /* WatchContext+LoopKit.swift */,
C165B8CD23302C5D0004112E /* RemoteCommand.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -2604,6 +2607,7 @@
434FF1EE1CF27EEF000DB779 /* UITableViewCell.swift in Sources */,
439BED2A1E76093C00B0AED5 /* CGMManager.swift in Sources */,
C18C8C511D5A351900E043FB /* NightscoutDataManager.swift in Sources */,
C165B8CE23302C5D0004112E /* RemoteCommand.swift in Sources */,
438849EA1D297CB6003B3F23 /* NightscoutService.swift in Sources */,
438172D91F4E9E37003C3328 /* NewPumpEvent.swift in Sources */,
4389916B1E91B689000EEF90 /* ChartSettings+Loop.swift in Sources */,
Expand Down
35 changes: 34 additions & 1 deletion Loop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
NotificationManager.authorize(delegate: self)

log.info(#function)

AnalyticsManager.shared.application(application, didFinishLaunchingWithOptions: launchOptions)

rootViewController.rootViewController.deviceManager = deviceManager

let notificationOption = launchOptions?[.remoteNotification]

if let notification = notificationOption as? [String: AnyObject] {
deviceManager.handleRemoteNotification(notification)
}

return true
}
Expand Down Expand Up @@ -74,6 +80,32 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
return false
}
}

// MARK: - Remote notifications
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
log.default("RemoteNotifications device token: \(token)")
deviceManager.loopManager.settings.deviceToken = deviceToken
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
log.error("Failed to register: \(error)")
}

func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
) {
guard let notification = userInfo as? [String: AnyObject] else {
completionHandler(.failed)
return
}

deviceManager.handleRemoteNotification(notification)
completionHandler(.noData)
}

}


Expand Down Expand Up @@ -102,4 +134,5 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.badge, .sound, .alert])
}

}
1 change: 1 addition & 0 deletions Loop/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
Expand Down
2 changes: 2 additions & 0 deletions Loop/Loop.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.healthkit</key>
<true/>
<key>com.apple.developer.healthkit.access</key>
Expand Down
4 changes: 2 additions & 2 deletions Loop/Managers/AnalyticsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ final class AnalyticsManager: IdentifiableClass {
logEvent("CGM Fetch", outOfSession: true)
}

func loopDidSucceed() {
logEvent("Loop success", outOfSession: true)
func loopDidSucceed(_ duration: TimeInterval) {
logEvent("Loop success", withProperties: ["duration": duration], outOfSession: true)
}

func loopDidError() {
Expand Down
18 changes: 18 additions & 0 deletions Loop/Managers/DeviceDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -653,3 +653,21 @@ extension Notification.Name {
static let PumpEventsAdded = Notification.Name(rawValue: "com.loopKit.notification.PumpEventsAdded")
}

// MARK: - Remote Notification Handling
extension DeviceDataManager {
func handleRemoteNotification(_ notification: [String: AnyObject]) {

if let command = RemoteCommand(notification: notification, allowedPresets: loopManager.settings.overridePresets) {
switch command {
case .temporaryScheduleOverride(let override):
log.default("Enacting remote temporary override: \(override)")
loopManager.settings.scheduleOverride = override
case .cancelTemporaryOverride:
log.default("Canceling temporary override from remote command")
loopManager.settings.scheduleOverride = nil
}
} else {
log.info("Unhandled remote notification: \(notification)")
}
}
}
21 changes: 14 additions & 7 deletions Loop/Managers/LoopDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ final class LoopDataManager {
private let lockedBasalDeliveryState: Locked<PumpManagerStatus.BasalDeliveryState?>

fileprivate var lastRequestedBolus: DoseEntry?

private var lastLoopStarted: Date?

/// The last date at which a loop completed, from prediction to dose (if dosing is enabled)
var lastLoopCompleted: Date? {
Expand All @@ -224,11 +226,6 @@ final class LoopDataManager {
}
set {
lockedLastLoopCompleted.value = newValue

NotificationManager.clearLoopNotRunningNotifications()
NotificationManager.scheduleLoopNotRunningNotifications()
AnalyticsManager.shared.loopDidSucceed()
NotificationCenter.default.post(name: .LoopCompleted, object: self)
}
}
private let lockedLastLoopCompleted: Locked<Date?>
Expand Down Expand Up @@ -269,6 +266,15 @@ final class LoopDataManager {
backgroundTask = .invalid
}
}

private func loopDidComplete(date: Date, duration: TimeInterval) {
lastLoopCompleted = date
NotificationManager.clearLoopNotRunningNotifications()
NotificationManager.scheduleLoopNotRunningNotifications()
AnalyticsManager.shared.loopDidSucceed(duration)
NotificationCenter.default.post(name: .LoopCompleted, object: self)

}
}

// MARK: Background task management
Expand Down Expand Up @@ -635,6 +641,7 @@ extension LoopDataManager {
NotificationCenter.default.post(name: .LoopRunning, object: self)

self.lastLoopError = nil
let startDate = Date()

do {
try self.update()
Expand All @@ -646,7 +653,7 @@ extension LoopDataManager {
if let error = error {
self.logger.error(error)
} else {
self.lastLoopCompleted = Date()
self.loopDidComplete(date: Date(), duration: -startDate.timeIntervalSinceNow)
}
self.logger.default("Loop ended")
self.notify(forChange: .tempBasal)
Expand All @@ -655,7 +662,7 @@ extension LoopDataManager {
// Delay the notification until we know the result of the temp basal
return
} else {
self.lastLoopCompleted = Date()
self.loopDidComplete(date: Date(), duration: -startDate.timeIntervalSinceNow)
}
} catch let error {
self.lastLoopError = error
Expand Down
Loading