Преглед изворни кода

Remove conversion factor; fix holes in plot

Deniz Cengiz пре 1 година
родитељ
комит
35f728cc75

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

@@ -368,11 +368,7 @@ extension Bolus {
 
         /// Calculate insulin recommendation
         func calculateInsulin() -> Decimal {
-            // ensure that isf is in mg/dL
-            var conversion: Decimal {
-                units == .mmolL ? 0.0555 : 1
-            }
-            let isfForCalculation = isf / conversion
+            let isfForCalculation = units == .mmolL ? isf.asMgdL : isf
 
             // insulin needed for the current blood glucose
             targetDifference = currentBG - target
@@ -780,31 +776,6 @@ extension Bolus.StateModel {
 }
 
 extension Bolus.StateModel {
-    func calculateForecasts(predictions: Predictions?) -> ([Int], [Int]) {
-        let iob: [Int] = predictions?.iob ?? []
-        let zt: [Int] = predictions?.zt ?? []
-        let cob: [Int] = predictions?.cob ?? []
-        let uam: [Int] = predictions?.uam ?? []
-
-        // Filter out the empty arrays and find the maximum length of the remaining arrays
-        let nonEmptyArrays: [[Int]] = [iob, zt, cob, uam].filter { !$0.isEmpty }
-        guard !nonEmptyArrays.isEmpty, let maxCount = nonEmptyArrays.map(\.count).max(), maxCount > 0 else {
-            return ([], [])
-        }
-
-        let minForecast = (0 ..< maxCount).map { index -> Int in
-            let valuesAtCurrentIndex = nonEmptyArrays.compactMap { $0.indices.contains(index) ? $0[index] : nil }
-            return valuesAtCurrentIndex.min() ?? 0
-        }
-
-        let maxForecast = (0 ..< maxCount).map { index -> Int in
-            let valuesAtCurrentIndex = nonEmptyArrays.compactMap { $0.indices.contains(index) ? $0[index] : nil }
-            return valuesAtCurrentIndex.max() ?? 0
-        }
-
-        return (minForecast, maxForecast)
-    }
-
     @MainActor func updateForecasts(with forecastData: Determination? = nil) async {
         if let forecastData = forecastData {
             simulatedDetermination = forecastData
@@ -831,14 +802,14 @@ extension Bolus.StateModel {
             return
         }
 
-        let maxCount = min(36, nonEmptyArrays.map(\.count).max() ?? 0)
-        guard maxCount > 0 else { return }
+        let minCount = min(36, nonEmptyArrays.map(\.count).min() ?? 0)
+        guard minCount > 0 else { return }
 
-        minForecast = (0 ..< maxCount).map { index in
+        minForecast = (0 ..< minCount).map { index in
             nonEmptyArrays.compactMap { $0.indices.contains(index) ? $0[index] : nil }.min() ?? 0
         }
 
-        maxForecast = (0 ..< maxCount).map { index in
+        maxForecast = (0 ..< minCount).map { index in
             nonEmptyArrays.compactMap { $0.indices.contains(index) ? $0[index] : nil }.max() ?? 0
         }
     }

+ 12 - 10
FreeAPS/Sources/Modules/Bolus/View/ForeCastChart.swift

@@ -11,10 +11,6 @@ struct ForeCastChart: View {
     @State private var startMarker = Date(timeIntervalSinceNow: -4 * 60 * 60)
     @State private var endMarker = Date(timeIntervalSinceNow: 3 * 60 * 60)
 
-    private var conversionFactor: Decimal {
-        units == .mmolL ? 0.0555 : 1
-    }
-
     var body: some View {
         VStack {
             forecastChart
@@ -54,7 +50,7 @@ struct ForeCastChart: View {
         .chartXAxis { forecastChartXAxis }
         .chartXScale(domain: startMarker ... endMarker)
         .chartYAxis { forecastChartYAxis }
-        .chartYScale(domain: 0 ... 300 * conversionFactor)
+        .chartYScale(domain: units == .mgdL ? 0 ... 300 : 0.asMmolL ... 300.asMmolL)
     }
 
     private func drawGlucose() -> some ChartContent {
@@ -62,21 +58,21 @@ struct ForeCastChart: View {
             if item.glucose > Int(state.highGlucose) {
                 PointMark(
                     x: .value("Time", item.date ?? Date(), unit: .second),
-                    y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    y: .value("Value", units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL)
                 )
                 .foregroundStyle(Color.orange.gradient)
                 .symbolSize(20)
             } else if item.glucose < Int(state.lowGlucose) {
                 PointMark(
                     x: .value("Time", item.date ?? Date(), unit: .second),
-                    y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    y: .value("Value", units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL)
                 )
                 .foregroundStyle(Color.red.gradient)
                 .symbolSize(20)
             } else {
                 PointMark(
                     x: .value("Time", item.date ?? Date(), unit: .second),
-                    y: .value("Value", Decimal(item.glucose) * conversionFactor)
+                    y: .value("Value", units == .mgdL ? Decimal(item.glucose) : Decimal(item.glucose).asMmolL)
                 )
                 .foregroundStyle(Color.green.gradient)
                 .symbolSize(20)
@@ -94,8 +90,14 @@ struct ForeCastChart: View {
         ForEach(state.minForecast.indices, id: \.self) { index in
             AreaMark(
                 x: .value("Time", timeForIndex(Int32(index))),
-                yStart: .value("Min Value", Decimal(state.minForecast[index]) * conversionFactor),
-                yEnd: .value("Max Value", Decimal(state.maxForecast[index]) * conversionFactor)
+                yStart: .value(
+                    "Min Value",
+                    units == .mgdL ? Decimal(state.minForecast[index]) : Decimal(state.minForecast[index]).asMmolL
+                ),
+                yEnd: .value(
+                    "Max Value",
+                    units == .mgdL ? Decimal(state.maxForecast[index]) : Decimal(state.maxForecast[index]).asMmolL
+                )
             )
             .foregroundStyle(Color.blue.opacity(0.5))
             .interpolationMethod(.catmullRom)

+ 2 - 2
Trio.xcworkspace/xcshareddata/swiftpm/Package.resolved

@@ -1,5 +1,5 @@
 {
-  "originHash" : "59ac7eba66375d6eb406e758cb0b9964f4b3b0ae45c5665596f00384c32262b9",
+  "originHash" : "f5c836c216c4ca7d356e3777e58d6d4f9502b03f3974891349eb775f4c4cf750",
   "pins" : [
     {
       "identity" : "cryptoswift",
@@ -49,7 +49,7 @@
     {
       "identity" : "swiftcharts",
       "kind" : "remoteSourceControl",
-      "location" : "https://github.com/ivanschuetz/SwiftCharts.git",
+      "location" : "https://github.com/ivanschuetz/SwiftCharts",
       "state" : {
         "branch" : "master",
         "revision" : "c354c1945bb35a1f01b665b22474f6db28cba4a2"