Skip to content

Commit ba0ae98

Browse files
authored
Merge pull request #497 from LoopKit/restore-temp-basal
Restore last temp basal value from status extension context on launch
2 parents 569fc37 + 2c0e48e commit ba0ae98

File tree

9 files changed

+54
-14
lines changed

9 files changed

+54
-14
lines changed

Common/Models/StatusExtensionContext.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct LoopContext {
2525
struct NetBasalContext {
2626
let rate: Double
2727
let percentage: Double
28-
let startDate: Date
28+
let start: Date
29+
let end: Date
2930
}
3031

3132
struct SensorDisplayableContext: SensorDisplayable {
@@ -122,22 +123,25 @@ extension NetBasalContext: RawRepresentable {
122123
return [
123124
"rate": rate,
124125
"percentage": percentage,
125-
"startDate": startDate
126+
"start": start,
127+
"end": end
126128
]
127129
}
128130

129131
init?(rawValue: RawValue) {
130132
guard
131133
let rate = rawValue["rate"] as? Double,
132134
let percentage = rawValue["percentage"] as? Double,
133-
let startDate = rawValue["startDate"] as? Date
135+
let start = rawValue["start"] as? Date,
136+
let end = rawValue["end"] as? Date
134137
else {
135138
return nil
136139
}
137140

138141
self.rate = rate
139142
self.percentage = percentage
140-
self.startDate = startDate
143+
self.start = start
144+
self.end = end
141145
}
142146
}
143147

Loop Status Extension/StatusViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class StatusViewController: UIViewController, NCWidgetProviding {
185185
}
186186

187187
if let netBasal = context.netBasal {
188-
basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percentage, at: netBasal.startDate)
188+
basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percentage, at: netBasal.start)
189189
}
190190

191191
if let loop = context.loop {

Loop.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
432E73CB1D24B3D6009AD15D /* RemoteDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 432E73CA1D24B3D6009AD15D /* RemoteDataManager.swift */; };
3333
433EA4C21D9F39C900CD78FB /* PumpIDTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 433EA4C11D9F39C900CD78FB /* PumpIDTableViewController.swift */; };
3434
433EA4C41D9F71C800CD78FB /* CommandResponseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 433EA4C31D9F71C800CD78FB /* CommandResponseViewController.swift */; };
35+
43441A9C1EDB34810087958C /* StatusExtensionContext+LoopKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */; };
3536
4346D1E71C77F5FE00ABAFE3 /* ChartTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4346D1E61C77F5FE00ABAFE3 /* ChartTableViewCell.swift */; };
3637
4346D1F61C78501000ABAFE3 /* ChartPoint+Loop.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4346D1F51C78501000ABAFE3 /* ChartPoint+Loop.swift */; };
3738
434F54571D287FDB002A9274 /* NibLoadable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 434F54561D287FDB002A9274 /* NibLoadable.swift */; };
@@ -377,6 +378,7 @@
377378
4337615E1D52F487004A3647 /* GlucoseHUDView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GlucoseHUDView.swift; sourceTree = "<group>"; };
378379
433EA4C11D9F39C900CD78FB /* PumpIDTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PumpIDTableViewController.swift; sourceTree = "<group>"; };
379380
433EA4C31D9F71C800CD78FB /* CommandResponseViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommandResponseViewController.swift; sourceTree = "<group>"; };
381+
43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "StatusExtensionContext+LoopKit.swift"; sourceTree = "<group>"; };
380382
4346D1E61C77F5FE00ABAFE3 /* ChartTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChartTableViewCell.swift; sourceTree = "<group>"; };
381383
4346D1EF1C781BEA00ABAFE3 /* SwiftCharts.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftCharts.framework; path = Carthage/Build/iOS/SwiftCharts.framework; sourceTree = "<group>"; };
382384
4346D1F51C78501000ABAFE3 /* ChartPoint+Loop.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChartPoint+Loop.swift"; sourceTree = "<group>"; };
@@ -676,6 +678,7 @@
676678
438D42F81D7C88BC003244B0 /* PredictionInputEffect.swift */,
677679
43D848AF1E7DCBE100DADCBC /* Result.swift */,
678680
43C418B41CE0575200405B6A /* ShareGlucose+GlucoseKit.swift */,
681+
43441A9B1EDB34810087958C /* StatusExtensionContext+LoopKit.swift */,
679682
4328E0311CFC068900E199AA /* WatchContext+LoopKit.swift */,
680683
);
681684
path = Models;
@@ -1429,6 +1432,7 @@
14291432
C17824A51E1AD4D100D9D25C /* BolusRecommendation.swift in Sources */,
14301433
4F70C2131DE90339006380B7 /* StatusExtensionContext.swift in Sources */,
14311434
434F54571D287FDB002A9274 /* NibLoadable.swift in Sources */,
1435+
43441A9C1EDB34810087958C /* StatusExtensionContext+LoopKit.swift in Sources */,
14321436
4FF4D1001E18374700846527 /* WatchContext.swift in Sources */,
14331437
4315D28A1CA5F45E00589052 /* DiagnosticLogger+LoopKit.swift in Sources */,
14341438
43C418B51CE0575200405B6A /* ShareGlucose+GlucoseKit.swift in Sources */,

Loop/Managers/DeviceDataManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,8 @@ final class DeviceDataManager {
627627
statusExtensionManager = StatusExtensionDataManager(deviceDataManager: self)
628628
loopManager = LoopDataManager(
629629
delegate: self,
630-
lastLoopCompleted: statusExtensionManager.context?.loop?.lastCompleted
630+
lastLoopCompleted: statusExtensionManager.context?.loop?.lastCompleted,
631+
lastTempBasal: statusExtensionManager.context?.netBasal?.tempBasal
631632
)
632633
watchManager = WatchDataManager(deviceDataManager: self)
633634
nightscoutDataManager = NightscoutDataManager(deviceDataManager: self)

Loop/Managers/LoopDataManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ final class LoopDataManager {
4242
init(
4343
delegate: LoopDataManagerDelegate,
4444
lastLoopCompleted: Date?,
45+
lastTempBasal: DoseEntry?,
4546
basalRateSchedule: BasalRateSchedule? = UserDefaults.standard.basalRateSchedule,
4647
carbRatioSchedule: CarbRatioSchedule? = UserDefaults.standard.carbRatioSchedule,
4748
insulinActionDuration: TimeInterval? = UserDefaults.standard.insulinActionDuration,
@@ -50,6 +51,7 @@ final class LoopDataManager {
5051
) {
5152
self.delegate = delegate
5253
self.lastLoopCompleted = lastLoopCompleted
54+
self.lastTempBasal = lastTempBasal
5355
self.settings = settings
5456

5557
carbStore = CarbStore(

Loop/Managers/StatusExtensionDataManager.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ final class StatusExtensionDataManager {
5555
context.netBasal = NetBasalContext(
5656
rate: 2.1,
5757
percentage: 0.6,
58-
startDate:
59-
Date(timeIntervalSinceNow: -250)
58+
start:
59+
Date(timeIntervalSinceNow: -250),
60+
end: Date(timeIntervalSinceNow: .minutes(30))
6061
)
6162
context.predictedGlucose = PredictedGlucoseContext(
6263
values: (1...36).map { 89.123 + Double($0 * 5) }, // 3 hours of linear data
@@ -120,7 +121,7 @@ final class StatusExtensionDataManager {
120121
scheduledBasal: scheduledBasal
121122
)
122123

123-
context.netBasal = NetBasalContext(rate: netBasal.rate, percentage: netBasal.percent, startDate: netBasal.startDate)
124+
context.netBasal = NetBasalContext(rate: netBasal.rate, percentage: netBasal.percent, start: netBasal.start, end: netBasal.end)
124125
}
125126

126127
if let reservoir = manager.doseStore.lastReservoirValue,

Loop/Models/NetBasal.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import LoopKit
1313
struct NetBasal {
1414
let rate: Double
1515
let percent: Double
16-
let startDate: Date
16+
let start: Date
17+
let end: Date
1718

1819
init(lastTempBasal: DoseEntry?, maxBasal: Double?, scheduledBasal: AbsoluteScheduleValue<Double>) {
1920
if let lastTempBasal = lastTempBasal, lastTempBasal.endDate > Date(), let maxBasal = maxBasal {
2021
rate = lastTempBasal.unitsPerHour - scheduledBasal.value
21-
startDate = lastTempBasal.startDate
22+
start = lastTempBasal.startDate
23+
end = lastTempBasal.endDate
2224

2325
if rate < 0 {
2426
percent = rate / scheduledBasal.value
@@ -30,9 +32,11 @@ struct NetBasal {
3032
percent = 0
3133

3234
if let lastTempBasal = lastTempBasal, lastTempBasal.endDate > scheduledBasal.startDate {
33-
startDate = lastTempBasal.endDate
35+
start = lastTempBasal.endDate
36+
end = scheduledBasal.endDate
3437
} else {
35-
startDate = scheduledBasal.startDate
38+
start = scheduledBasal.startDate
39+
end = scheduledBasal.endDate
3640
}
3741
}
3842
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// StatusExtensionContext+LoopKit.swift
3+
// Loop
4+
//
5+
// Copyright © 2017 LoopKit Authors. All rights reserved.
6+
//
7+
8+
import InsulinKit
9+
10+
extension NetBasalContext {
11+
var tempBasal: DoseEntry? {
12+
guard rate != 0 else {
13+
return nil
14+
}
15+
16+
return DoseEntry(
17+
type: .tempBasal,
18+
startDate: start,
19+
endDate: end,
20+
value: rate,
21+
unit: .unitsPerHour
22+
)
23+
}
24+
}

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ final class StatusTableViewController: ChartsTableViewController {
317317
scheduledBasal: scheduledBasal
318318
)
319319

320-
self.hudView.basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percent, at: netBasal.startDate)
320+
self.hudView.basalRateHUD.setNetBasalRate(netBasal.rate, percent: netBasal.percent, at: netBasal.start)
321321
}
322322

323323
// Fetch the current IOB subtitle

0 commit comments

Comments
 (0)