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

update forecasts of chart when there are new carb entries

polscm32 aka Marvout 1 год назад
Родитель
Сommit
c70d0acda7

+ 4 - 2
FreeAPS/Sources/Modules/Bolus/BolusStateModel.swift

@@ -97,6 +97,8 @@ extension Bolus {
         @Published var glucoseFromPersistence: [GlucoseStored] = []
         @Published var determination: [OrefDetermination] = []
         @Published var preprocessedData: [(id: UUID, forecast: Forecast, forecastValue: ForecastValue)] = []
+        @Published var simulatedDetermination: Determination?
+        @Published var predictionsForChart: Predictions?
 
         let now = Date.now
 
@@ -694,7 +696,7 @@ extension Bolus.StateModel {
     }
 
     func updateForecasts() async {
-        let dummyDetermination = await apsManager.simulateDetermineBasal(carbs: carbs, iob: amount)
-        print("determination: \(dummyDetermination)")
+        simulatedDetermination = await apsManager.simulateDetermineBasal(carbs: carbs, iob: amount)
+        predictionsForChart = simulatedDetermination?.predictions
     }
 }

+ 38 - 15
FreeAPS/Sources/Modules/Bolus/View/ForeCastChart.swift

@@ -57,27 +57,50 @@ struct ForeCastChart: View {
         }
     }
 
-    private func drawForecasts() -> some ChartContent {
-        ForEach(state.preprocessedData, id: \.id) { tuple in
-            let forecastValue = tuple.forecastValue
-            let forecast = tuple.forecast
-            let valueAsDecimal = Decimal(forecastValue.value)
-            let displayValue = units == .mmolL ? valueAsDecimal.asMmolL : valueAsDecimal
-
-            LineMark(
-                x: .value("Time", timeForIndex(forecastValue.index)),
-                y: .value("Value", displayValue)
-            )
-            .foregroundStyle(by: .value("Predictions", forecast.type ?? ""))
-        }
-    }
-
     private func timeForIndex(_ index: Int32) -> Date {
         let currentTime = Date()
         let timeInterval = TimeInterval(index * 300)
         return currentTime.addingTimeInterval(timeInterval)
     }
 
+    private func drawForecasts() -> some ChartContent {
+        let predictions = state.predictionsForChart
+
+        let predictionData = [
+            ("IOB", predictions?.iob),
+            ("ZT", predictions?.zt),
+            ("COB", predictions?.cob),
+            ("UAM", predictions?.uam)
+        ]
+
+        return ForEach(predictionData, id: \.0) { name, values in
+            if let values = values {
+                ForEach(values.indices, id: \.self) { index in
+                    LineMark(
+                        x: .value("Time", timeForIndex(Int32(index))),
+                        y: .value("Value", Decimal(values[index]) * conversionFactor)
+                    )
+                    .foregroundStyle(by: .value("Prediction Type", name))
+                }
+            }
+        }
+    }
+
+//    private func drawForecasts() -> some ChartContent {
+//        ForEach(state.preprocessedData, id: \.id) { tuple in
+//            let forecastValue = tuple.forecastValue
+//            let forecast = tuple.forecast
+//            let valueAsDecimal = Decimal(forecastValue.value)
+//            let displayValue = units == .mmolL ? valueAsDecimal.asMmolL : valueAsDecimal
+//
+//            LineMark(
+//                x: .value("Time", timeForIndex(forecastValue.index)),
+//                y: .value("Value", displayValue)
+//            )
+//            .foregroundStyle(by: .value("Predictions", forecast.type ?? ""))
+//        }
+//    }
+
     private func drawCurrentTimeMarker() -> some ChartContent {
         RuleMark(
             x: .value(