@@ -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:
0 commit comments