@@ -372,6 +372,8 @@ final class LoopDataManager {
372
372
}
373
373
374
374
private var retrospectiveGlucoseDiscrepanciesSummed : [ GlucoseChange ] ?
375
+
376
+ private var suspendInsulinDeliveryEffect : [ GlucoseEffect ] = [ ]
375
377
376
378
fileprivate var predictedGlucose : [ PredictedGlucoseValue ] ? {
377
379
didSet {
@@ -1112,6 +1114,12 @@ extension LoopDataManager {
1112
1114
warnings. append ( . fetchDataWarning( . retrospectiveGlucoseEffect( error: error) ) )
1113
1115
}
1114
1116
}
1117
+
1118
+ do {
1119
+ try updateSuspendInsulinDeliveryEffect ( )
1120
+ } catch let error {
1121
+ logger. error ( " %{public}@ " , String ( describing: error) )
1122
+ }
1115
1123
1116
1124
dosingDecision. appendWarnings ( warnings. value)
1117
1125
@@ -1310,6 +1318,11 @@ extension LoopDataManager {
1310
1318
effects. append ( retrospectiveGlucoseEffect)
1311
1319
}
1312
1320
}
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
+ }
1313
1326
1314
1327
var prediction = LoopMath . predictGlucose ( startingAt: glucose, momentum: momentum, effects: effects)
1315
1328
@@ -1575,6 +1588,61 @@ extension LoopDataManager {
1575
1588
)
1576
1589
}
1577
1590
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
+
1578
1646
/// Runs the glucose prediction on the latest effect data.
1579
1647
///
1580
1648
/// - Throws:
0 commit comments