Просмотр исходного кода

Don't recommend insulin when lastLoopDate is > 15 mins

* Also adds line to popup about rounding for pump.
Mike Plante 1 год назад
Родитель
Сommit
388242c7d8

+ 6 - 0
Trio/Sources/Localizations/Main/Localizable.xcstrings

@@ -95788,6 +95788,9 @@
         }
       }
     },
+    "Last loop was > 15 mins ago." : {
+
+    },
     "Last loop was more than %d min ago" : {
       "comment" : "Last loop was more than %d min ago",
       "localizations" : {
@@ -130858,6 +130861,9 @@
         }
       }
     },
+    "Rounded for pump." : {
+
+    },
     "Run now" : {
       "extractionState" : "manual",
       "localizations" : {

+ 2 - 0
Trio/Sources/Modules/Treatments/TreatmentsStateModel.swift

@@ -42,6 +42,7 @@ extension Treatments {
         var minDelta: Decimal = 0
         var expectedDelta: Decimal = 0
         var minPredBG: Decimal = 0
+        var lastLoopDate: Date?
         var isAwaitingDeterminationResult: Bool = false
         var carbRatio: Decimal = 0
 
@@ -804,6 +805,7 @@ extension Treatments.StateModel {
             insulinRequired = (mostRecentDetermination.insulinReq ?? 0) as Decimal
             evBG = (mostRecentDetermination.eventualBG ?? 0) as Decimal
             minPredBG = (mostRecentDetermination.minPredBGFromReason ?? 0) as Decimal
+            lastLoopDate = apsManager.lastLoopDate as Date?
             insulin = (mostRecentDetermination.insulinForManualBolus ?? 0) as Decimal
             target = (mostRecentDetermination.currentTarget ?? currentBGTarget as NSDecimalNumber) as Decimal
             isf = (mostRecentDetermination.insulinSensitivity ?? currentISF as NSDecimalNumber) as Decimal

+ 10 - 1
Trio/Sources/Modules/Treatments/View/PopupView.swift

@@ -447,7 +447,12 @@ struct PopupView: View {
 
             VStack(alignment: .leading) {
                 let iobAvailable: Decimal = state.maxIOB - state.iob
-                if state.factoredInsulin < 0 {
+                let isLoopStale = state.lastLoopDate == nil ||
+                    Date().timeIntervalSince(state.lastLoopDate!) > 15 * 60
+
+                if isLoopStale {
+                    Text("Last loop was > 15 mins ago.")
+                } else if state.factoredInsulin < 0 {
                     Text("No insulin recommended.")
                 } else if state.currentBG < 54 {
                     Text("Glucose is very low.")
@@ -464,6 +469,10 @@ struct PopupView: View {
                         .font(.caption)
                         .foregroundColor(.secondary)
                 }
+                if state.insulinCalculated > 0 {
+                    Text("Rounded for pump.")
+                        .foregroundColor(.secondary)
+                }
             }
             .foregroundColor(Color.loopRed)
 

+ 8 - 3
Trio/Sources/Services/BolusCalculator/BolusCalculationManager.swift

@@ -31,6 +31,7 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
         var insulinRequired: Decimal
         var evBG: Decimal
         var minPredBG: Decimal
+        var lastLoopDate: Date?
         var insulin: Decimal
         var target: Decimal
         var isf: Decimal
@@ -247,6 +248,7 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
                 insulinRequired: 0,
                 evBG: 0,
                 minPredBG: 0,
+                lastLoopDate: nil,
                 insulin: 0,
                 target: currentBGTarget,
                 isf: currentISF,
@@ -262,6 +264,7 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
             insulinRequired: (mostRecentDetermination.insulinReq ?? 0) as Decimal,
             evBG: (mostRecentDetermination.eventualBG ?? 0) as Decimal,
             minPredBG: (mostRecentDetermination.minPredBGFromReason ?? 0) as Decimal,
+            lastLoopDate: apsManager.lastLoopDate as Date?,
             insulin: (mostRecentDetermination.insulinForManualBolus ?? 0) as Decimal,
             target: (mostRecentDetermination.currentTarget ?? currentBGTarget as NSDecimalNumber) as Decimal,
             isf: (mostRecentDetermination.insulinSensitivity ?? NSDecimalNumber(decimal: currentISF)) as Decimal,
@@ -402,15 +405,17 @@ final class BaseBolusCalculationManager: BolusCalculationManager, Injectable {
 
         // the final result for recommended insulin amount
         var insulinCalculated: Decimal
-        // don't recommend insulin when current glucose or minPredBG is < 54
-        if input.currentBG < 54 || input.minPredBG < 54 {
+        let isLoopStale = Date().timeIntervalSince(apsManager.lastLoopDate) > 15 * 60
+        
+        // don't recommend insulin when current glucose or minPredBG is < 54 or last sucessful loop was over 15 minutes ago
+        if input.currentBG < 54 || input.minPredBG < 54 || isLoopStale {
             insulinCalculated = 0
         } else {
             // no negative insulinCalculated
             insulinCalculated = max(factoredInsulin, 0)
             // don't exceed maxBolus
             insulinCalculated = min(insulinCalculated, input.maxBolus)
-            // dont exceed maxIOB
+            // don't exceed maxIOB
             insulinCalculated = min(insulinCalculated, input.maxIOB - input.iob)
             // round calculated recommendation to allowed bolus increment
             insulinCalculated = apsManager.roundBolus(amount: insulinCalculated)