Skip to content

Commit 84e778f

Browse files
authored
Merge pull request #2026 from dm61/suspend-insulin-effect
add effect of suspending insulin delivery
2 parents 9ff534f + d233818 commit 84e778f

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Loop/Managers/LoopDataManager.swift

+68
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ final class LoopDataManager {
372372
}
373373

374374
private var retrospectiveGlucoseDiscrepanciesSummed: [GlucoseChange]?
375+
376+
private var suspendInsulinDeliveryEffect: [GlucoseEffect] = []
375377

376378
fileprivate var predictedGlucose: [PredictedGlucoseValue]? {
377379
didSet {
@@ -1112,6 +1114,12 @@ extension LoopDataManager {
11121114
warnings.append(.fetchDataWarning(.retrospectiveGlucoseEffect(error: error)))
11131115
}
11141116
}
1117+
1118+
do {
1119+
try updateSuspendInsulinDeliveryEffect()
1120+
} catch let error {
1121+
logger.error("%{public}@", String(describing: error))
1122+
}
11151123

11161124
dosingDecision.appendWarnings(warnings.value)
11171125

@@ -1310,6 +1318,11 @@ extension LoopDataManager {
13101318
effects.append(retrospectiveGlucoseEffect)
13111319
}
13121320
}
1321+
1322+
// Append effect of suspending insulin delivery when selected by the user on the Predicted Glucose screen (for information purposes only)
1323+
if inputs.contains(.suspend) {
1324+
effects.append(suspendInsulinDeliveryEffect)
1325+
}
13131326

13141327
var prediction = LoopMath.predictGlucose(startingAt: glucose, momentum: momentum, effects: effects)
13151328

@@ -1575,6 +1588,61 @@ extension LoopDataManager {
15751588
)
15761589
}
15771590

1591+
/// Generates a glucose prediction effect of suspending insulin delivery over duration of insulin action starting at current date
1592+
///
1593+
/// - Throws: LoopError.configurationError
1594+
private func updateSuspendInsulinDeliveryEffect() throws {
1595+
dispatchPrecondition(condition: .onQueue(dataAccessQueue))
1596+
1597+
// Get settings, otherwise clear effect and throw error
1598+
guard
1599+
let insulinSensitivity = insulinSensitivityScheduleApplyingOverrideHistory
1600+
else {
1601+
suspendInsulinDeliveryEffect = []
1602+
throw LoopError.configurationError(.insulinSensitivitySchedule)
1603+
}
1604+
guard
1605+
let basalRateSchedule = basalRateScheduleApplyingOverrideHistory
1606+
else {
1607+
suspendInsulinDeliveryEffect = []
1608+
throw LoopError.configurationError(.basalRateSchedule)
1609+
}
1610+
1611+
let insulinModel = doseStore.insulinModelProvider.model(for: pumpInsulinType)
1612+
let insulinActionDuration = insulinModel.effectDuration
1613+
1614+
let startSuspend = now()
1615+
let endSuspend = startSuspend.addingTimeInterval(insulinActionDuration)
1616+
1617+
var suspendDoses: [DoseEntry] = []
1618+
let basalItems = basalRateSchedule.between(start: startSuspend, end: endSuspend)
1619+
1620+
// Iterate over basal entries during suspension of insulin delivery
1621+
for (index, basalItem) in basalItems.enumerated() {
1622+
var startSuspendDoseDate: Date
1623+
var endSuspendDoseDate: Date
1624+
1625+
if index == 0 {
1626+
startSuspendDoseDate = startSuspend
1627+
} else {
1628+
startSuspendDoseDate = basalItem.startDate
1629+
}
1630+
1631+
if index == basalItems.count - 1 {
1632+
endSuspendDoseDate = endSuspend
1633+
} else {
1634+
endSuspendDoseDate = basalItems[index + 1].startDate
1635+
}
1636+
1637+
let suspendDose = DoseEntry(type: .tempBasal, startDate: startSuspendDoseDate, endDate: endSuspendDoseDate, value: -basalItem.value, unit: DoseUnit.unitsPerHour)
1638+
1639+
suspendDoses.append(suspendDose)
1640+
}
1641+
1642+
// Calculate predicted glucose effect of suspending insulin delivery
1643+
suspendInsulinDeliveryEffect = suspendDoses.glucoseEffects(insulinModelProvider: doseStore.insulinModelProvider, longestEffectDuration: doseStore.longestEffectDuration, insulinSensitivity: insulinSensitivity).filterDateRange(startSuspend, endSuspend)
1644+
}
1645+
15781646
/// Runs the glucose prediction on the latest effect data.
15791647
///
15801648
/// - Throws:

Loop/Models/PredictionInputEffect.swift

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct PredictionInputEffect: OptionSet {
1717
static let insulin = PredictionInputEffect(rawValue: 1 << 1)
1818
static let momentum = PredictionInputEffect(rawValue: 1 << 2)
1919
static let retrospection = PredictionInputEffect(rawValue: 1 << 3)
20+
static let suspend = PredictionInputEffect(rawValue: 1 << 4)
2021

2122
static let all: PredictionInputEffect = [.carbs, .insulin, .momentum, .retrospection]
2223

@@ -30,6 +31,8 @@ struct PredictionInputEffect: OptionSet {
3031
return NSLocalizedString("Glucose Momentum", comment: "Title of the prediction input effect for glucose momentum")
3132
case [.retrospection]:
3233
return NSLocalizedString("Retrospective Correction", comment: "Title of the prediction input effect for retrospective correction")
34+
case [.suspend]:
35+
return NSLocalizedString("Suspension of Insulin Delivery", comment: "Title of the prediction input effect for suspension of insulin delivery")
3336
default:
3437
return nil
3538
}
@@ -45,6 +48,8 @@ struct PredictionInputEffect: OptionSet {
4548
return NSLocalizedString("15 min glucose regression coefficient (b₁), continued with decay over 30 min", comment: "Description of the prediction input effect for glucose momentum")
4649
case [.retrospection]:
4750
return NSLocalizedString("30 min comparison of glucose prediction vs actual, continued with decay over 60 min", comment: "Description of the prediction input effect for retrospective correction")
51+
case [.suspend]:
52+
return NSLocalizedString("Glucose effect of suspending insulin delivery", comment: "Description of the prediction input effect for suspension of insulin delivery")
4853
default:
4954
return nil
5055
}

Loop/View Controllers/PredictionTableViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class PredictionTableViewController: LoopChartsTableViewController, Identifiable
197197

198198
private var eventualGlucoseDescription: String?
199199

200-
private var availableInputs: [PredictionInputEffect] = [.carbs, .insulin, .momentum, .retrospection]
200+
private var availableInputs: [PredictionInputEffect] = [.carbs, .insulin, .momentum, .retrospection, .suspend]
201201

202202
private var selectedInputs = PredictionInputEffect.all
203203

0 commit comments

Comments
 (0)