Skip to content

Commit f9c01eb

Browse files
authored
[LOOP-4975] Update Open Loop Freshness Syncing Between StatusHUD and SettingsView (#705)
1 parent 9e1a8c9 commit f9c01eb

File tree

6 files changed

+56
-20
lines changed

6 files changed

+56
-20
lines changed

Loop Widget Extension/Widgets/SystemStatusWidget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct SystemStatusWidgetEntryView: View {
2626
} else {
2727
let mostRecentGlucoseDataDate = entry.mostRecentGlucoseDataDate ?? Date().addingTimeInterval(.minutes(16))
2828
let mostRecentPumpDataDate = entry.mostRecentPumpDataDate ?? Date().addingTimeInterval(.minutes(16))
29-
age = abs(max(min(0, mostRecentGlucoseDataDate.timeIntervalSinceNow), min(0, mostRecentPumpDataDate.timeIntervalSinceNow)))
29+
age = max(abs(min(0, mostRecentPumpDataDate.timeIntervalSinceNow)), abs(min(0, mostRecentGlucoseDataDate.timeIntervalSinceNow)))
3030
}
3131

3232
return LoopCompletionFreshness(age: age)

Loop/Managers/LoopAppManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ class LoopAppManager: NSObject {
290290

291291
loopDataManager = LoopDataManager(
292292
lastLoopCompleted: ExtensionDataManager.context?.lastLoopCompleted,
293+
publishedMostRecentGlucoseDataDate: ExtensionDataManager.context?.mostRecentGlucoseDataDate,
294+
publishedMostRecentPumpDataDate: ExtensionDataManager.context?.mostRecentPumpDataDate,
293295
temporaryPresetsManager: temporaryPresetsManager,
294296
settingsProvider: settingsManager,
295297
doseStore: doseStore,

Loop/Managers/LoopDataManager.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ final class LoopDataManager: ObservableObject {
9999
}
100100

101101
@Published private(set) var lastLoopCompleted: Date?
102+
@Published private(set) var publishedMostRecentGlucoseDataDate: Date?
103+
@Published private(set) var publishedMostRecentPumpDataDate: Date?
102104

103105
var deliveryDelegate: DeliveryDelegate?
104106

@@ -148,6 +150,8 @@ final class LoopDataManager: ObservableObject {
148150

149151
init(
150152
lastLoopCompleted: Date?,
153+
publishedMostRecentGlucoseDataDate: Date?,
154+
publishedMostRecentPumpDataDate: Date?,
151155
temporaryPresetsManager: TemporaryPresetsManager,
152156
settingsProvider: SettingsProvider,
153157
doseStore: DoseStoreProtocol,
@@ -163,6 +167,8 @@ final class LoopDataManager: ObservableObject {
163167
) {
164168

165169
self.lastLoopCompleted = lastLoopCompleted
170+
self.publishedMostRecentGlucoseDataDate = publishedMostRecentGlucoseDataDate
171+
self.publishedMostRecentPumpDataDate = publishedMostRecentPumpDataDate
166172
self.temporaryPresetsManager = temporaryPresetsManager
167173
self.settingsProvider = settingsProvider
168174
self.doseStore = doseStore
@@ -431,6 +437,8 @@ final class LoopDataManager: ObservableObject {
431437
logger.error("Error updating Loop state: %{public}@", String(describing: loopError))
432438
}
433439
displayState = newState
440+
publishedMostRecentGlucoseDataDate = mostRecentGlucoseDataDate
441+
publishedMostRecentPumpDataDate = mostRecentPumpDataDate
434442
await updateRemoteRecommendation()
435443
}
436444

Loop/View Controllers/StatusTableViewController.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ final class StatusTableViewController: LoopChartsTableViewController {
166166
}
167167
}
168168
.store(in: &cancellables)
169+
170+
loopManager.$lastLoopCompleted
171+
.receive(on: DispatchQueue.main)
172+
.sink { [weak self] lastLoopCompleted in
173+
self?.hudView?.loopCompletionHUD.lastLoopCompleted = lastLoopCompleted
174+
}
175+
.store(in: &cancellables)
176+
177+
loopManager.$publishedMostRecentGlucoseDataDate
178+
.receive(on: DispatchQueue.main)
179+
.sink { [weak self] mostRecentGlucoseDataDate in
180+
self?.hudView?.loopCompletionHUD.mostRecentGlucoseDataDate = mostRecentGlucoseDataDate
181+
}
182+
.store(in: &cancellables)
183+
184+
loopManager.$publishedMostRecentPumpDataDate
185+
.receive(on: DispatchQueue.main)
186+
.sink { [weak self] mostRecentPumpDataDate in
187+
self?.hudView?.loopCompletionHUD.mostRecentPumpDataDate = mostRecentPumpDataDate
188+
}
189+
.store(in: &cancellables)
169190

170191
if let gestureRecognizer = charts.gestureRecognizer {
171192
tableView.addGestureRecognizer(gestureRecognizer)
@@ -1627,8 +1648,8 @@ final class StatusTableViewController: LoopChartsTableViewController {
16271648
initialDosingEnabled: self.settingsManager.settings.dosingEnabled, automaticDosingStatus: self.automaticDosingStatus,
16281649
automaticDosingStrategy: self.settingsManager.settings.automaticDosingStrategy,
16291650
lastLoopCompletion: loopManager.$lastLoopCompleted,
1630-
mostRecentGlucoseDataDate: { [weak self] in self?.loopManager.mostRecentGlucoseDataDate },
1631-
mostRecentPumpDataDate: { [weak self] in self?.loopManager.mostRecentPumpDataDate },
1651+
mostRecentGlucoseDataDate: loopManager.$publishedMostRecentGlucoseDataDate,
1652+
mostRecentPumpDataDate: loopManager.$publishedMostRecentPumpDataDate,
16321653
availableSupports: supportManager.availableSupports,
16331654
isOnboardingComplete: onboardingManager.isComplete,
16341655
therapySettingsViewModelDelegate: deviceManager,

Loop/View Models/SettingsViewModel.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public class SettingsViewModel: ObservableObject {
8383
@Published private(set) var automaticDosingStatus: AutomaticDosingStatus
8484

8585
@Published private(set) var lastLoopCompletion: Date?
86-
let mostRecentGlucoseDataDate: () -> Date?
87-
let mostRecentPumpDataDate: () -> Date?
86+
@Published private(set) var mostRecentGlucoseDataDate: Date?
87+
@Published private(set) var mostRecentPumpDataDate: Date?
8888

8989
var closedLoopDescriptiveText: String? {
9090
return delegate?.closedLoopDescriptiveText
@@ -116,9 +116,9 @@ public class SettingsViewModel: ObservableObject {
116116
let lastLoopCompletion = lastLoopCompletion ?? Date().addingTimeInterval(.minutes(16))
117117
age = abs(min(0, lastLoopCompletion.timeIntervalSinceNow))
118118
} else {
119-
let mostRecentGlucoseDataDate = mostRecentGlucoseDataDate() ?? Date().addingTimeInterval(.minutes(16))
120-
let mostRecentPumpDataDate = mostRecentPumpDataDate() ?? Date().addingTimeInterval(.minutes(16))
121-
age = abs(max(min(0, mostRecentGlucoseDataDate.timeIntervalSinceNow), min(0, mostRecentPumpDataDate.timeIntervalSinceNow)))
119+
let mostRecentGlucoseDataDate = mostRecentGlucoseDataDate ?? Date().addingTimeInterval(.minutes(16))
120+
let mostRecentPumpDataDate = mostRecentPumpDataDate ?? Date().addingTimeInterval(.minutes(16))
121+
age = max(abs(min(0, mostRecentPumpDataDate.timeIntervalSinceNow)), abs(min(0, mostRecentGlucoseDataDate.timeIntervalSinceNow)))
122122
}
123123

124124
return LoopCompletionFreshness(age: age)
@@ -139,8 +139,8 @@ public class SettingsViewModel: ObservableObject {
139139
automaticDosingStatus: AutomaticDosingStatus,
140140
automaticDosingStrategy: AutomaticDosingStrategy,
141141
lastLoopCompletion: Published<Date?>.Publisher,
142-
mostRecentGlucoseDataDate: @escaping () -> Date?,
143-
mostRecentPumpDataDate: @escaping () -> Date?,
142+
mostRecentGlucoseDataDate: Published<Date?>.Publisher,
143+
mostRecentPumpDataDate: Published<Date?>.Publisher,
144144
availableSupports: [SupportUI],
145145
isOnboardingComplete: Bool,
146146
therapySettingsViewModelDelegate: TherapySettingsViewModelDelegate?,
@@ -159,8 +159,8 @@ public class SettingsViewModel: ObservableObject {
159159
self.automaticDosingStatus = automaticDosingStatus
160160
self.automaticDosingStrategy = automaticDosingStrategy
161161
self.lastLoopCompletion = nil
162-
self.mostRecentGlucoseDataDate = mostRecentGlucoseDataDate
163-
self.mostRecentPumpDataDate = mostRecentPumpDataDate
162+
self.mostRecentGlucoseDataDate = nil
163+
self.mostRecentPumpDataDate = nil
164164
self.availableSupports = availableSupports
165165
self.isOnboardingComplete = isOnboardingComplete
166166
self.therapySettingsViewModelDelegate = therapySettingsViewModelDelegate
@@ -190,7 +190,12 @@ public class SettingsViewModel: ObservableObject {
190190
lastLoopCompletion
191191
.assign(to: \.lastLoopCompletion, on: self)
192192
.store(in: &cancellables)
193-
193+
mostRecentGlucoseDataDate
194+
.assign(to: \.mostRecentGlucoseDataDate, on: self)
195+
.store(in: &cancellables)
196+
mostRecentPumpDataDate
197+
.assign(to: \.mostRecentPumpDataDate, on: self)
198+
.store(in: &cancellables)
194199
}
195200
}
196201

@@ -215,8 +220,8 @@ extension SettingsViewModel {
215220
automaticDosingStatus: AutomaticDosingStatus(automaticDosingEnabled: true, isAutomaticDosingAllowed: true),
216221
automaticDosingStrategy: .automaticBolus,
217222
lastLoopCompletion: FakeLastLoopCompletionPublisher().$mockLastLoopCompletion,
218-
mostRecentGlucoseDataDate: { nil },
219-
mostRecentPumpDataDate: { nil },
223+
mostRecentGlucoseDataDate: FakeLastLoopCompletionPublisher().$mockLastLoopCompletion,
224+
mostRecentPumpDataDate: FakeLastLoopCompletionPublisher().$mockLastLoopCompletion,
220225
availableSupports: [],
221226
isOnboardingComplete: false,
222227
therapySettingsViewModelDelegate: nil,

LoopUI/Views/LoopCompletionHUDView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ public final class LoopCompletionHUDView: BaseHUDView {
173173
caption?.text = ""
174174
accessibilityLabel = nil
175175
}
176-
} else if let mostRecentPumpDataDate, let mostRecentGlucoseDataDate {
177-
let ago = abs(max(min(0, mostRecentPumpDataDate.timeIntervalSinceNow), min(0, mostRecentGlucoseDataDate.timeIntervalSinceNow)))
176+
} else if !loopIconClosed, let mostRecentPumpDataDate, let mostRecentGlucoseDataDate {
177+
let ago = max(abs(min(0, mostRecentPumpDataDate.timeIntervalSinceNow)), abs(min(0, mostRecentGlucoseDataDate.timeIntervalSinceNow)))
178178

179179
freshness = LoopCompletionFreshness(age: ago)
180180

@@ -220,7 +220,7 @@ extension LoopCompletionHUDView {
220220
public var loopCompletionMessage: (title: String, message: String) {
221221
switch freshness {
222222
case .fresh:
223-
if loopStateView.open {
223+
if !loopIconClosed {
224224
let reason = closedLoopDisallowedLocalizedDescription ?? LocalizedString(
225225
"Tap Settings to toggle Closed Loop ON if you wish for the app to automate your insulin.",
226226
comment: "Instructions for user to close loop if it is allowed."
@@ -257,7 +257,7 @@ extension LoopCompletionHUDView {
257257
)
258258
}
259259
case .aging:
260-
if loopStateView.open {
260+
if !loopIconClosed {
261261
return (
262262
title: LocalizedString(
263263
"Caution",
@@ -285,7 +285,7 @@ extension LoopCompletionHUDView {
285285
)
286286
}
287287
case .stale:
288-
if loopStateView.open {
288+
if !loopIconClosed {
289289
return (
290290
title: LocalizedString(
291291
"Device Error",

0 commit comments

Comments
 (0)