Browse Source

ensure that predictions don't increase the y axis scale too much, weigh negative iob with factor 2

polscm32 aka Marvout 1 year ago
parent
commit
3767382034

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

@@ -390,8 +390,11 @@ extension Bolus {
 
         @MainActor func saveMeal() async {
             guard carbs > 0 || fat > 0 || protein > 0 else { return }
-            carbs = min(carbs, maxCarbs)
-            id_ = UUID().uuidString
+
+            await MainActor.run {
+                self.carbs = min(self.carbs, self.maxCarbs)
+                self.id_ = UUID().uuidString
+            }
 
             let carbsToStore = [CarbsEntry(
                 id: id_,

+ 6 - 6
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -107,10 +107,6 @@ struct MainChartView: View {
         units == .mgdL ? 30 : 1.66
     }
 
-    private var interpolationFactor: Double {
-        Double(state.enactedAndNonEnactedDeterminations.first?.cob ?? 1) * 10
-    }
-
     private var selectedGlucose: GlucoseStored? {
         if let selection = selection {
             let lowerBound = selection.addingTimeInterval(-120)
@@ -659,7 +655,8 @@ extension MainChartView {
 
     private func drawIOB() -> some ChartContent {
         ForEach(state.enactedAndNonEnactedDeterminations) { iob in
-            let amount: Double = (iob.iob?.doubleValue ?? 0 / interpolationFactor)
+            let rawAmount = iob.iob?.doubleValue ?? 0
+            let amount: Double = rawAmount > 0 ? rawAmount : rawAmount * 2 // weigh negative iob with factor 2
             let date: Date = iob.deliverAt ?? Date()
 
             LineMark(x: .value("Time", date), y: .value("Amount", amount))
@@ -996,8 +993,11 @@ extension MainChartView {
             return
         }
 
+        // Ensure maxForecast is not more than 100 over maxGlucose
+        let adjustedMaxForecast = min(maxForecast, maxGlucose + 100)
+
         let minOverall = min(minGlucose, minForecast)
-        let maxOverall = max(maxGlucose, maxForecast)
+        let maxOverall = max(maxGlucose, adjustedMaxForecast)
 
         minValue = minOverall * conversionFactor - 50 * conversionFactor
         maxValue = maxOverall * conversionFactor + 80 * conversionFactor