-
Notifications
You must be signed in to change notification settings - Fork 6
/
ABSwitcherPatch.txt
269 lines (252 loc) · 13.9 KB
/
ABSwitcherPatch.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
From 21c3fa7ae880b90484386004f7e2af9834a4d8e4 Mon Sep 17 00:00:00 2001
From: Jon Fawcett <[email protected]>
Date: Tue, 21 Sep 2021 12:20:40 -0400
Subject: [PATCH] AB Dosing Automatic Switcher Patch
---
Loop/Managers/LoopDataManager.swift | 8 ++
.../SettingsTableViewController.swift | 97 +++++++++++++++++--
LoopCore/LoopSettings.swift | 20 +++-
3 files changed, 114 insertions(+), 11 deletions(-)
diff --git a/Loop/Managers/LoopDataManager.swift b/Loop/Managers/LoopDataManager.swift
index ef861a680a..d8ece1a20a 100644
--- a/Loop/Managers/LoopDataManager.swift
+++ b/Loop/Managers/LoopDataManager.swift
@@ -1200,6 +1200,14 @@ extension LoopDataManager {
self.predictedGlucose = predictedGlucose
let predictedGlucoseIncludingPendingInsulin = try predictGlucose(using: settings.enabledEffects, includingPendingInsulin: true)
self.predictedGlucoseIncludingPendingInsulin = predictedGlucoseIncludingPendingInsulin
+
+ if( settings.dosingStrategyAutomationEnabled && settings.dosingStrategyThreshold?.rawValue != nil){
+ if( glucose.quantity > HKQuantity(unit : settings.glucoseUnit ?? .milligramsPerDeciliter, doubleValue: settings.dosingStrategyThreshold!.value)){
+ settings.dosingStrategy = .automaticBolus
+ } else {
+ settings.dosingStrategy = .tempBasalOnly
+ }
+ }
guard
let maxBasal = settings.maximumBasalRatePerHour,
diff --git a/Loop/View Controllers/SettingsTableViewController.swift b/Loop/View Controllers/SettingsTableViewController.swift
index c8a0af8513..933404139d 100644
--- a/Loop/View Controllers/SettingsTableViewController.swift
+++ b/Loop/View Controllers/SettingsTableViewController.swift
@@ -46,6 +46,7 @@ final class SettingsTableViewController: UITableViewController {
case pump
case cgm
case configuration
+ case strategy
case services
case testingPumpDataDeletion
case testingCGMDataDeletion
@@ -70,10 +71,15 @@ final class SettingsTableViewController: UITableViewController {
case basalRate
case deliveryLimits
case insulinModel
- case dosingStrategy
case carbRatio
case insulinSensitivity
}
+
+ fileprivate enum StrategyRow: Int, CaseCountable {
+ case dosingStrategy = 0
+ case dosingStrategyAutomationEnabled
+ case dosingStrategyThreshold
+ }
fileprivate enum ServiceRow: Int, CaseCountable {
case nightscout = 0
@@ -147,6 +153,8 @@ final class SettingsTableViewController: UITableViewController {
return CGMRow.count
case .configuration:
return ConfigurationRow.count
+ case .strategy:
+ return StrategyRow.count
case .services:
return ServiceRow.count
case .testingPumpDataDeletion, .testingCGMDataDeletion:
@@ -269,9 +277,6 @@ final class SettingsTableViewController: UITableViewController {
} else {
configCell.detailTextLabel?.text = SettingsTableViewCell.TapToSetString
}
- case .dosingStrategy:
- configCell.textLabel?.text = NSLocalizedString("Dosing Strategy", comment: "The title text for the dosing strategy setting row")
- configCell.detailTextLabel?.text = dataManager.loopManager.settings.dosingStrategy.title
case .deliveryLimits:
configCell.textLabel?.text = NSLocalizedString("Delivery Limits", comment: "Title text for delivery limits")
@@ -290,6 +295,36 @@ final class SettingsTableViewController: UITableViewController {
}
}
+ configCell.accessoryType = .disclosureIndicator
+ return configCell
+ case .strategy:
+ let configCell = tableView.dequeueReusableCell(withIdentifier: SettingsTableViewCell.className, for: indexPath)
+
+ switch StrategyRow(rawValue: indexPath.row)! {
+ case .dosingStrategy:
+ configCell.textLabel?.text = NSLocalizedString("Dosing Strategy", comment: "The title text for the dosing strategy setting row")
+ configCell.detailTextLabel?.text = dataManager.loopManager.settings.dosingStrategy.title
+ case .dosingStrategyAutomationEnabled:
+ let switchCell = tableView.dequeueReusableCell(withIdentifier: SwitchTableViewCell.className, for: indexPath) as! SwitchTableViewCell
+
+ switchCell.selectionStyle = .none
+ switchCell.switch?.isOn = dataManager.loopManager.settings.dosingStrategyAutomationEnabled
+ switchCell.textLabel?.text = NSLocalizedString("Auto Strategy Switching", comment: "The title text for the Dosing Strategy enabled switch cell")
+
+ switchCell.switch?.addTarget(self, action: #selector(dosingStrategyAutomationEnabledChanged(_:)), for: .valueChanged)
+
+ return switchCell
+ case .dosingStrategyThreshold:
+ configCell.textLabel?.text = NSLocalizedString("Dose Switching Threshold", comment: "The title text in settings")
+
+ if let dosingStrategyThreshold = dataManager.loopManager.settings.dosingStrategyThreshold {
+ let value = valueNumberFormatter.string(from: dosingStrategyThreshold.value, unit: dosingStrategyThreshold.unit) ?? SettingsTableViewCell.TapToSetString
+ configCell.detailTextLabel?.text = value
+ } else {
+ configCell.detailTextLabel?.text = SettingsTableViewCell.TapToSetString
+ }
+ }
+
configCell.accessoryType = .disclosureIndicator
return configCell
case .services:
@@ -342,6 +377,8 @@ final class SettingsTableViewController: UITableViewController {
return NSLocalizedString("Continuous Glucose Monitor", comment: "The title of the continuous glucose monitor section in settings")
case .configuration:
return NSLocalizedString("Configuration", comment: "The title of the configuration section in settings")
+ case .strategy:
+ return NSLocalizedString("Dosing Strategy", comment: "The title of the strategy section in settings")
case .services:
return NSLocalizedString("Services", comment: "The title of the services section in settings")
case .testingPumpDataDeletion, .testingCGMDataDeletion:
@@ -514,8 +551,6 @@ final class SettingsTableViewController: UITableViewController {
}
case .insulinModel:
performSegue(withIdentifier: InsulinModelSettingsViewController.className, sender: sender)
- case .dosingStrategy:
- performSegue(withIdentifier: DosingStrategySelectionViewController.className, sender: sender)
case .deliveryLimits:
let vc = DeliveryLimitSettingsTableViewController(style: .grouped)
@@ -558,6 +593,32 @@ final class SettingsTableViewController: UITableViewController {
show(vc, sender: sender)
}
+ case .strategy:
+ let row = StrategyRow(rawValue: indexPath.row)!
+ switch row {
+ case .dosingStrategy:
+ performSegue(withIdentifier: DosingStrategySelectionViewController.className, sender: sender)
+ case .dosingStrategyAutomationEnabled:
+ break
+ case .dosingStrategyThreshold:
+ if let dosingStrategyThreshold = dataManager.loopManager.settings.dosingStrategyThreshold {
+ let vc = GlucoseThresholdTableViewController(threshold: dosingStrategyThreshold.value, glucoseUnit: dosingStrategyThreshold.unit)
+ vc.delegate = self
+ vc.indexPath = indexPath
+ vc.title = sender?.textLabel?.text
+ vc.placeholder = "Enter switching threshold"
+ vc.contextHelp = "If Auto Strategy Switching is enabled, when current glucose is above the switching threshold, Loop will use Automatic Bolus strategy. When glucose is at or below the switching threshold, Loop will use Temp Basal Only strategy."
+ self.show(vc, sender: sender)
+ } else if let unit = dataManager.loopManager.glucoseStore.preferredUnit {
+ let vc = GlucoseThresholdTableViewController(threshold: nil, glucoseUnit: unit)
+ vc.delegate = self
+ vc.indexPath = indexPath
+ vc.title = sender?.textLabel?.text
+ vc.placeholder = "Enter switching threshold"
+ vc.contextHelp = "If Auto Strategy Switching is enabled, when current glucose is above the switching threshold, Loop will use Automatic Bolus strategy. When glucose is at or below the switching threshold, Loop will use Temp Basal Only strategy."
+ self.show(vc, sender: sender)
+ }
+ }
case .loop:
switch LoopRow(rawValue: indexPath.row)! {
case .diagnostic:
@@ -617,6 +678,10 @@ final class SettingsTableViewController: UITableViewController {
@objc private func dosingEnabledChanged(_ sender: UISwitch) {
dataManager.loopManager.settings.dosingEnabled = sender.isOn
}
+
+ @objc private func dosingStrategyAutomationEnabledChanged(_ sender: UISwitch) {
+ dataManager.loopManager.settings.dosingStrategyAutomationEnabled = sender.isOn
+ }
}
// MARK: - DeviceManager view controller delegation
@@ -810,8 +875,8 @@ extension SettingsTableViewController: DosingStrategySelectionViewControllerDele
}
switch sections[indexPath.section] {
- case .configuration:
- switch ConfigurationRow(rawValue: indexPath.row)! {
+ case .strategy:
+ switch StrategyRow(rawValue: indexPath.row)! {
case .dosingStrategy:
if let strategy = controller.dosingStrategy {
dataManager.loopManager.settings.dosingStrategy = strategy
@@ -844,8 +909,20 @@ extension SettingsTableViewController: LoopKitUI.TextFieldTableViewControllerDel
default:
assertionFailure()
}
- default:
- assertionFailure()
+ case .strategy:
+ switch StrategyRow(rawValue: indexPath.row)! {
+ case .dosingStrategyThreshold:
+ if let controller = controller as? GlucoseThresholdTableViewController,
+ let value = controller.value, let dosingStrategyThreshold = valueNumberFormatter.number(from: value)?.doubleValue {
+ dataManager.loopManager.settings.dosingStrategyThreshold = GlucoseThreshold(unit: controller.glucoseUnit, value: dosingStrategyThreshold)
+ } else {
+ dataManager.loopManager.settings.dosingStrategyThreshold = nil
+ }
+ default:
+ assertionFailure()
+ }
+ default:
+ assertionFailure()
}
}
diff --git a/LoopCore/LoopSettings.swift b/LoopCore/LoopSettings.swift
index 34859c8d6c..3223350ffc 100644
--- a/LoopCore/LoopSettings.swift
+++ b/LoopCore/LoopSettings.swift
@@ -68,6 +68,10 @@ public struct LoopSettings: Equatable {
public let retrospectiveCorrectionEnabled = true
public var dosingStrategy: DosingStrategy = .tempBasalOnly
+
+ public var dosingStrategyAutomationEnabled = false
+
+ public var dosingStrategyThreshold: GlucoseThreshold? = nil
/// The interval over which to aggregate changes in glucose for retrospective correction
public let retrospectiveCorrectionGroupingInterval = TimeInterval(minutes: 30)
@@ -132,13 +136,17 @@ public struct LoopSettings: Equatable {
glucoseTargetRangeSchedule: GlucoseRangeSchedule? = nil,
maximumBasalRatePerHour: Double? = nil,
maximumBolus: Double? = nil,
- suspendThreshold: GlucoseThreshold? = nil
+ suspendThreshold: GlucoseThreshold? = nil,
+ dosingStrategyAutomationEnabled: Bool = false,
+ dosingStrategyThreshold: GlucoseThreshold? = nil
) {
self.dosingEnabled = dosingEnabled
self.glucoseTargetRangeSchedule = glucoseTargetRangeSchedule
self.maximumBasalRatePerHour = maximumBasalRatePerHour
self.maximumBolus = maximumBolus
self.suspendThreshold = suspendThreshold
+ self.dosingStrategyAutomationEnabled = dosingStrategyAutomationEnabled
+ self.dosingStrategyThreshold = dosingStrategyThreshold
}
}
@@ -277,6 +285,14 @@ extension LoopSettings: RawRepresentable {
let dosingStrategy = DosingStrategy(rawValue: rawDosingStrategy) {
self.dosingStrategy = dosingStrategy
}
+
+ if let dosingStrategyAutomationEnabled = rawValue["dosingStrategyAutomationEnabled"] as? Bool {
+ self.dosingStrategyAutomationEnabled = dosingStrategyAutomationEnabled
+ }
+
+ if let rawDosingStrategyThreshold = rawValue["dosingStrategyThreshold"] as? GlucoseThreshold.RawValue {
+ self.dosingStrategyThreshold = GlucoseThreshold(rawValue: rawDosingStrategyThreshold)
+ }
}
@@ -295,6 +311,8 @@ extension LoopSettings: RawRepresentable {
raw["maximumBolus"] = maximumBolus
raw["minimumBGGuard"] = suspendThreshold?.rawValue
raw["dosingStrategy"] = dosingStrategy.rawValue
+ raw["dosingStrategyAutomationEnabled"] = dosingStrategyAutomationEnabled
+ raw["dosingStrategyThreshold"] = dosingStrategyThreshold?.rawValue
return raw
}