Skip to content

Commit 4f33e44

Browse files
authored
Merge pull request #368 from loopandlearn/enacted-suggested
Adapt LoopFollow to Trio 1.0’s Updated Device Status Upload Behavior
2 parents 51d3013 + e8abd99 commit 4f33e44

File tree

5 files changed

+227
-261
lines changed

5 files changed

+227
-261
lines changed

LoopFollow/Controllers/Nightscout/DeviceStatus.swift

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ import UIKit
1111
import Charts
1212

1313
extension MainViewController {
14-
// NS Device Status Web Call
1514
func webLoadNSDeviceStatus() {
16-
let count = ObservableUserDefaults.shared.device.value == "Trio" && Observable.shared.isLastDeviceStatusSuggested.value ? "5" : "1"
17-
if count != "1" {
18-
LogManager.shared.log(category: .deviceStatus, message: "Fetching \(count) device status records")
19-
}
20-
21-
let parameters: [String: String] = ["count": count]
15+
let parameters: [String: String] = ["count": "1"]
2216
NightscoutUtils.executeDynamicRequest(eventType: .deviceStatus, parameters: parameters) { result in
2317
switch result {
2418
case .success(let json):
@@ -40,45 +34,52 @@ extension MainViewController {
4034
DispatchQueue.main.async {
4135
TaskScheduler.shared.rescheduleTask(id: .deviceStatus, to: Date().addingTimeInterval(10))
4236
}
37+
38+
evaluateNotLooping()
4339
}
4440

45-
func evaluateNotLooping(lastLoopTime: TimeInterval) {
46-
if let statusStackView = LoopStatusLabel.superview as? UIStackView {
47-
if ((TimeInterval(Date().timeIntervalSince1970) - lastLoopTime) / 60) > 15 {
48-
IsNotLooping = true
49-
// Change the distribution to 'fill' to allow manual resizing of arranged subviews
50-
statusStackView.distribution = .fill
51-
52-
// Hide PredictionLabel and expand LoopStatusLabel to fill the entire stack view
53-
PredictionLabel.isHidden = true
54-
LoopStatusLabel.frame = CGRect(x: 0, y: 0, width: statusStackView.frame.width, height: statusStackView.frame.height)
55-
56-
// Update LoopStatusLabel's properties to display Not Looping
57-
LoopStatusLabel.textAlignment = .center
58-
LoopStatusLabel.text = "⚠️ Not Looping!"
59-
LoopStatusLabel.textColor = UIColor.systemYellow
60-
LoopStatusLabel.font = UIFont.boldSystemFont(ofSize: 18)
61-
41+
func evaluateNotLooping() {
42+
guard let statusStackView = LoopStatusLabel.superview as? UIStackView else { return }
43+
44+
let now = TimeInterval(Date().timeIntervalSince1970)
45+
let lastLoopTime = UserDefaultsRepository.alertLastLoopTime.value
46+
let isAlarmEnabled = UserDefaultsRepository.alertNotLoopingActive.value
47+
let nonLoopingTimeThreshold: TimeInterval
48+
49+
if isAlarmEnabled {
50+
nonLoopingTimeThreshold = Double(UserDefaultsRepository.alertNotLooping.value * 60)
51+
} else {
52+
nonLoopingTimeThreshold = 15 * 60
53+
}
54+
55+
if IsNightscoutEnabled(), (now - lastLoopTime) >= nonLoopingTimeThreshold, lastLoopTime > 0 {
56+
IsNotLooping = true
57+
statusStackView.distribution = .fill
58+
59+
PredictionLabel.isHidden = true
60+
LoopStatusLabel.frame = CGRect(x: 0, y: 0, width: statusStackView.frame.width, height: statusStackView.frame.height)
61+
62+
LoopStatusLabel.textAlignment = .center
63+
LoopStatusLabel.text = "⚠️ Not Looping!"
64+
LoopStatusLabel.textColor = UIColor.systemYellow
65+
LoopStatusLabel.font = UIFont.boldSystemFont(ofSize: 18)
66+
67+
} else {
68+
IsNotLooping = false
69+
statusStackView.distribution = .fillEqually
70+
PredictionLabel.isHidden = false
71+
72+
LoopStatusLabel.textAlignment = .right
73+
LoopStatusLabel.font = UIFont.systemFont(ofSize: 17)
74+
75+
if UserDefaultsRepository.forceDarkMode.value {
76+
LoopStatusLabel.textColor = UIColor.white
6277
} else {
63-
IsNotLooping = false
64-
// Restore the original distribution and visibility of labels
65-
statusStackView.distribution = .fillEqually
66-
PredictionLabel.isHidden = false
67-
68-
// Reset LoopStatusLabel's properties
69-
LoopStatusLabel.textAlignment = .right
70-
LoopStatusLabel.font = UIFont.systemFont(ofSize: 17)
71-
72-
if UserDefaultsRepository.forceDarkMode.value {
73-
LoopStatusLabel.textColor = UIColor.white
74-
} else {
75-
LoopStatusLabel.textColor = UIColor.black
76-
}
78+
LoopStatusLabel.textColor = UIColor.black
7779
}
7880
}
79-
latestLoopTime = lastLoopTime
8081
}
81-
82+
8283
// NS Device Status Response Processor
8384
func updateDeviceStatusDisplay(jsonDeviceStatus: [[String:AnyObject]]) {
8485
infoManager.clearInfoData(types: [.iob, .cob, .override, .battery, .pump, .target, .isf, .carbRatio, .updated, .recBolus, .tdd])
@@ -146,12 +147,12 @@ extension MainViewController {
146147

147148
// OpenAPS - handle new data
148149
if let lastLoopRecord = lastDeviceStatus?["openaps"] as! [String : AnyObject]? {
149-
DeviceStatusOpenAPS(formatter: formatter, lastDeviceStatus: lastDeviceStatus, lastLoopRecord: lastLoopRecord, jsonDeviceStatus: jsonDeviceStatus)
150+
DeviceStatusOpenAPS(formatter: formatter, lastDeviceStatus: lastDeviceStatus, lastLoopRecord: lastLoopRecord)
150151
}
151152

152153
// Start the timer based on the timestamp
153154
let now = dateTimeUtils.getNowTimeIntervalUTC()
154-
let secondsAgo = now - latestLoopTime
155+
let secondsAgo = now - UserDefaultsRepository.alertLastLoopTime.value
155156

156157
DispatchQueue.main.async {
157158
if secondsAgo >= (20 * 60) {
@@ -185,6 +186,8 @@ extension MainViewController {
185186
)
186187
}
187188
}
189+
190+
evaluateNotLooping()
188191
LogManager.shared.log(category: .deviceStatus, message: "Update Device Status done", isDebug: true)
189192
}
190193
}

LoopFollow/Controllers/Nightscout/DeviceStatusLoop.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extension MainViewController {
2020
}
2121

2222
if let lastLoopTime = formatter.date(from: (lastLoopRecord["timestamp"] as! String))?.timeIntervalSince1970 {
23+
let previousLastLoopTime = UserDefaultsRepository.alertLastLoopTime.value
2324
UserDefaultsRepository.alertLastLoopTime.value = lastLoopTime
2425
if let failure = lastLoopRecord["failureReason"] {
2526
LoopStatusLabel.text = "X"
@@ -71,7 +72,7 @@ extension MainViewController {
7172
let prediction = predictdata["values"] as! [Double]
7273
PredictionLabel.text = Localizer.toDisplayUnits(String(Int(prediction.last!)))
7374
PredictionLabel.textColor = UIColor.systemPurple
74-
if UserDefaultsRepository.downloadPrediction.value && latestLoopTime < lastLoopTime {
75+
if UserDefaultsRepository.downloadPrediction.value && previousLastLoopTime < lastLoopTime {
7576
predictionData.removeAll()
7677
var predictionTime = lastLoopTime
7778
let toLoad = Int(UserDefaultsRepository.predictionToLoad.value * 12)
@@ -128,8 +129,6 @@ extension MainViewController {
128129
}
129130

130131
}
131-
132-
evaluateNotLooping(lastLoopTime: lastLoopTime)
133132
}
134133
}
135134
}

0 commit comments

Comments
 (0)