From cf38b56dc8902c27695760cce9e63172825acbe6 Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Sat, 24 Jun 2017 13:44:57 -0500 Subject: [PATCH 1/2] Restore last temp basal value from status extension context on launch --- Common/Models/StatusExtensionContext.swift | 12 ++++++++---- Loop Status Extension/StatusViewController.swift | 2 +- Loop.xcodeproj/project.pbxproj | 4 ++++ Loop/Managers/DeviceDataManager.swift | 3 ++- Loop/Managers/LoopDataManager.swift | 2 ++ Loop/Managers/StatusExtensionDataManager.swift | 7 ++++--- Loop/Models/NetBasal.swift | 12 ++++++++---- .../View Controllers/StatusTableViewController.swift | 2 +- 8 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Common/Models/StatusExtensionContext.swift b/Common/Models/StatusExtensionContext.swift index 0e3c476db7..23c4a2e6d5 100644 --- a/Common/Models/StatusExtensionContext.swift +++ b/Common/Models/StatusExtensionContext.swift @@ -25,7 +25,8 @@ struct LoopContext { struct NetBasalContext { let rate: Double let percentage: Double - let startDate: Date + let start: Date + let end: Date } struct SensorDisplayableContext: SensorDisplayable { @@ -122,7 +123,8 @@ extension NetBasalContext: RawRepresentable { return [ "rate": rate, "percentage": percentage, - "startDate": startDate + "start": start, + "end": end ] } @@ -130,14 +132,16 @@ extension NetBasalContext: RawRepresentable { guard let rate = rawValue["rate"] as? Double, let percentage = rawValue["percentage"] as? Double, - let startDate = rawValue["startDate"] as? Date + let start = rawValue["start"] as? Date, + let end = rawValue["end"] as? Date else { return nil } self.rate = rate self.percentage = percentage - self.startDate = startDate + self.start = start + self.end = end } } diff --git a/Loop Status Extension/StatusViewController.swift b/Loop Status Extension/StatusViewController.swift index 5069544e7b..c39369ba15 100644 --- a/Loop Status Extension/StatusViewController.swift +++ b/Loop Status Extension/StatusViewController.swift @@ -185,7 +185,7 @@ class StatusViewController: UIViewController, NCWidgetProviding { } if let netBasal = context.netBasal { - basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percentage, at: netBasal.startDate) + basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percentage, at: netBasal.start) } if let loop = context.loop { diff --git a/Loop.xcodeproj/project.pbxproj b/Loop.xcodeproj/project.pbxproj index 86207e33ba..e0ee11a31d 100644 --- a/Loop.xcodeproj/project.pbxproj +++ b/Loop.xcodeproj/project.pbxproj @@ -32,6 +32,7 @@ 432E73CB1D24B3D6009AD15D /* RemoteDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 432E73CA1D24B3D6009AD15D /* RemoteDataManager.swift */; }; 433EA4C21D9F39C900CD78FB /* PumpIDTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 433EA4C11D9F39C900CD78FB /* PumpIDTableViewController.swift */; }; 433EA4C41D9F71C800CD78FB /* CommandResponseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 433EA4C31D9F71C800CD78FB /* CommandResponseViewController.swift */; }; + 43441A9C1EDB34810087958C /* StatusExtensionContext+LoopKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */; }; 4346D1E71C77F5FE00ABAFE3 /* ChartTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4346D1E61C77F5FE00ABAFE3 /* ChartTableViewCell.swift */; }; 4346D1F61C78501000ABAFE3 /* ChartPoint+Loop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4346D1F51C78501000ABAFE3 /* ChartPoint+Loop.swift */; }; 434F54571D287FDB002A9274 /* NibLoadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434F54561D287FDB002A9274 /* NibLoadable.swift */; }; @@ -377,6 +378,7 @@ 4337615E1D52F487004A3647 /* GlucoseHUDView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseHUDView.swift; sourceTree = ""; }; 433EA4C11D9F39C900CD78FB /* PumpIDTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PumpIDTableViewController.swift; sourceTree = ""; }; 433EA4C31D9F71C800CD78FB /* CommandResponseViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommandResponseViewController.swift; sourceTree = ""; }; + 43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StatusExtensionContext+LoopKit.swift"; sourceTree = ""; }; 4346D1E61C77F5FE00ABAFE3 /* ChartTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartTableViewCell.swift; sourceTree = ""; }; 4346D1EF1C781BEA00ABAFE3 /* SwiftCharts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftCharts.framework; path = Carthage/Build/iOS/SwiftCharts.framework; sourceTree = ""; }; 4346D1F51C78501000ABAFE3 /* ChartPoint+Loop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChartPoint+Loop.swift"; sourceTree = ""; }; @@ -676,6 +678,7 @@ 438D42F81D7C88BC003244B0 /* PredictionInputEffect.swift */, 43D848AF1E7DCBE100DADCBC /* Result.swift */, 43C418B41CE0575200405B6A /* ShareGlucose+GlucoseKit.swift */, + 43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */, 4328E0311CFC068900E199AA /* WatchContext+LoopKit.swift */, ); path = Models; @@ -1429,6 +1432,7 @@ C17824A51E1AD4D100D9D25C /* BolusRecommendation.swift in Sources */, 4F70C2131DE90339006380B7 /* StatusExtensionContext.swift in Sources */, 434F54571D287FDB002A9274 /* NibLoadable.swift in Sources */, + 43441A9C1EDB34810087958C /* StatusExtensionContext+LoopKit.swift in Sources */, 4FF4D1001E18374700846527 /* WatchContext.swift in Sources */, 4315D28A1CA5F45E00589052 /* DiagnosticLogger+LoopKit.swift in Sources */, 43C418B51CE0575200405B6A /* ShareGlucose+GlucoseKit.swift in Sources */, diff --git a/Loop/Managers/DeviceDataManager.swift b/Loop/Managers/DeviceDataManager.swift index a18e681e67..1191a4b12f 100644 --- a/Loop/Managers/DeviceDataManager.swift +++ b/Loop/Managers/DeviceDataManager.swift @@ -627,7 +627,8 @@ final class DeviceDataManager { statusExtensionManager = StatusExtensionDataManager(deviceDataManager: self) loopManager = LoopDataManager( delegate: self, - lastLoopCompleted: statusExtensionManager.context?.loop?.lastCompleted + lastLoopCompleted: statusExtensionManager.context?.loop?.lastCompleted, + lastTempBasal: statusExtensionManager.context?.netBasal?.tempBasal ) watchManager = WatchDataManager(deviceDataManager: self) nightscoutDataManager = NightscoutDataManager(deviceDataManager: self) diff --git a/Loop/Managers/LoopDataManager.swift b/Loop/Managers/LoopDataManager.swift index c6eac376e9..9822e879a4 100644 --- a/Loop/Managers/LoopDataManager.swift +++ b/Loop/Managers/LoopDataManager.swift @@ -42,6 +42,7 @@ final class LoopDataManager { init( delegate: LoopDataManagerDelegate, lastLoopCompleted: Date?, + lastTempBasal: DoseEntry?, basalRateSchedule: BasalRateSchedule? = UserDefaults.standard.basalRateSchedule, carbRatioSchedule: CarbRatioSchedule? = UserDefaults.standard.carbRatioSchedule, insulinActionDuration: TimeInterval? = UserDefaults.standard.insulinActionDuration, @@ -50,6 +51,7 @@ final class LoopDataManager { ) { self.delegate = delegate self.lastLoopCompleted = lastLoopCompleted + self.lastTempBasal = lastTempBasal self.settings = settings carbStore = CarbStore( diff --git a/Loop/Managers/StatusExtensionDataManager.swift b/Loop/Managers/StatusExtensionDataManager.swift index 137c4be6fb..1ec54a2c95 100644 --- a/Loop/Managers/StatusExtensionDataManager.swift +++ b/Loop/Managers/StatusExtensionDataManager.swift @@ -55,8 +55,9 @@ final class StatusExtensionDataManager { context.netBasal = NetBasalContext( rate: 2.1, percentage: 0.6, - startDate: - Date(timeIntervalSinceNow: -250) + start: + Date(timeIntervalSinceNow: -250), + end: Date(timeIntervalSinceNow: .minutes(30)) ) context.predictedGlucose = PredictedGlucoseContext( values: (1...36).map { 89.123 + Double($0 * 5) }, // 3 hours of linear data @@ -120,7 +121,7 @@ final class StatusExtensionDataManager { scheduledBasal: scheduledBasal ) - context.netBasal = NetBasalContext(rate: netBasal.rate, percentage: netBasal.percent, startDate: netBasal.startDate) + context.netBasal = NetBasalContext(rate: netBasal.rate, percentage: netBasal.percent, start: netBasal.start, end: netBasal.end) } if let reservoir = manager.doseStore.lastReservoirValue, diff --git a/Loop/Models/NetBasal.swift b/Loop/Models/NetBasal.swift index d866e33a85..eb68ecbd91 100644 --- a/Loop/Models/NetBasal.swift +++ b/Loop/Models/NetBasal.swift @@ -13,12 +13,14 @@ import LoopKit struct NetBasal { let rate: Double let percent: Double - let startDate: Date + let start: Date + let end: Date init(lastTempBasal: DoseEntry?, maxBasal: Double?, scheduledBasal: AbsoluteScheduleValue) { if let lastTempBasal = lastTempBasal, lastTempBasal.endDate > Date(), let maxBasal = maxBasal { rate = lastTempBasal.unitsPerHour - scheduledBasal.value - startDate = lastTempBasal.startDate + start = lastTempBasal.startDate + end = lastTempBasal.endDate if rate < 0 { percent = rate / scheduledBasal.value @@ -30,9 +32,11 @@ struct NetBasal { percent = 0 if let lastTempBasal = lastTempBasal, lastTempBasal.endDate > scheduledBasal.startDate { - startDate = lastTempBasal.endDate + start = lastTempBasal.endDate + end = scheduledBasal.endDate } else { - startDate = scheduledBasal.startDate + start = scheduledBasal.startDate + end = scheduledBasal.endDate } } } diff --git a/Loop/View Controllers/StatusTableViewController.swift b/Loop/View Controllers/StatusTableViewController.swift index e19169b320..f66f457e0f 100644 --- a/Loop/View Controllers/StatusTableViewController.swift +++ b/Loop/View Controllers/StatusTableViewController.swift @@ -317,7 +317,7 @@ final class StatusTableViewController: ChartsTableViewController { scheduledBasal: scheduledBasal ) - self.hudView.basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percent, at: netBasal.startDate) + self.hudView.basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percent, at: netBasal.start) } // Fetch the current IOB subtitle From 2c0e48e1396692b13f8539ec6aec8ab3596443ab Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Sat, 24 Jun 2017 13:47:06 -0500 Subject: [PATCH 2/2] Add missing file --- .../StatusExtensionContext+LoopKit.swift | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Loop/Models/StatusExtensionContext+LoopKit.swift diff --git a/Loop/Models/StatusExtensionContext+LoopKit.swift b/Loop/Models/StatusExtensionContext+LoopKit.swift new file mode 100644 index 0000000000..d1601c1cdb --- /dev/null +++ b/Loop/Models/StatusExtensionContext+LoopKit.swift @@ -0,0 +1,24 @@ +// +// StatusExtensionContext+LoopKit.swift +// Loop +// +// Copyright © 2017 LoopKit Authors. All rights reserved. +// + +import InsulinKit + +extension NetBasalContext { + var tempBasal: DoseEntry? { + guard rate != 0 else { + return nil + } + + return DoseEntry( + type: .tempBasal, + startDate: start, + endDate: end, + value: rate, + unit: .unitsPerHour + ) + } +}