Browse Source

Get minPredBG from Reason string

polscm32 1 year ago
parent
commit
fbd77843a8

+ 19 - 0
Model/Helper/Determination+helper.swift

@@ -11,6 +11,25 @@ extension OrefDetermination {
     }
 }
 
+extension Determination {
+    var minPredBGFromReason: Decimal? {
+        // Split reason into parts by semicolon and get first part
+        let reasonParts = reason.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []
+
+        // Find the part that contains "minPredBG"
+        if let minPredBGPart = reasonParts.first(where: { $0.contains("minPredBG") }) {
+            // Extract the number after "minPredBG"
+            let components = minPredBGPart.components(separatedBy: "minPredBG ")
+            if let valueComponent = components.dropFirst().first {
+                // Get everything after "minPredBG " and convert to Decimal
+                let valueString = valueComponent.trimmingCharacters(in: CharacterSet(charactersIn: "0123456789.-").inverted)
+                return Decimal(string: valueString)
+            }
+        }
+        return nil
+    }
+}
+
 extension OrefDetermination {
     var reasonParts: [String] {
         reason?.components(separatedBy: "; ").first?.components(separatedBy: ", ") ?? []

+ 8 - 1
Trio/Sources/Modules/Treatments/TreatmentsStateModel.swift

@@ -743,7 +743,6 @@ extension Treatments.StateModel {
 
             let determination = await determinationFetchContext.perform {
                 let determinationObject = determinationObjects.first
-                let eventualBG = determinationObject?.eventualBG?.intValue
 
                 let forecastsSet = determinationObject?.forecasts ?? []
                 let predictions = Predictions(
@@ -820,11 +819,19 @@ extension Treatments.StateModel {
         debug(.bolusState, "updateForecasts fired")
         if let forecastData = forecastData {
             simulatedDetermination = forecastData
+            debugPrint("\(DebuggingIdentifiers.failed) minPredBG: \(minPredBG)")
         } else {
             simulatedDetermination = await Task { [self] in
                 debug(.bolusState, "calling simulateDetermineBasal to get forecast data")
                 return await apsManager.simulateDetermineBasal(simulatedCarbsAmount: carbs, simulatedBolusAmount: amount)
             }.value
+
+            // Update evBG and minPredBG from simulated determination
+            if let simDetermination = simulatedDetermination {
+                evBG = Decimal(simDetermination.eventualBG ?? 0)
+                minPredBG = simDetermination.minPredBGFromReason ?? 0
+                debugPrint("\(DebuggingIdentifiers.inProgress) minPredBG: \(minPredBG)")
+            }
         }
 
         predictionsForChart = simulatedDetermination?.predictions