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

Fixed fetching stored predBGs:
* Extend Determination helper
* Extend Forecast helper
* Add additional fetch helper functions that use Core Data entity relations
* Fix plotting of forecasts in charts
* Still highly WIP!

dnzxy 2 лет назад
Родитель
Сommit
f47b04eb66

+ 1 - 0
FreeAPS/Sources/APS/OpenAPS/OpenAPS.swift

@@ -111,6 +111,7 @@ final class OpenAPS {
                                 .forEach { type, values in
                                     if let values = values {
                                         let forecast = Forecast(context: self.viewContext)
+                                        forecast.id = UUID()
                                         forecast.type = type
                                         forecast.date = Date() // or use a specific timestamp if available
                                         forecast

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

@@ -475,13 +475,38 @@ extension MainChartView {
         return currentTime.addingTimeInterval(timeInterval)
     }
 
+    private func getForecasts(_ determination: OrefDetermination) -> [Forecast] {
+        guard let forecastSet = determination.forecasts, let forecasts = Array(forecastSet) as? [Forecast] else {
+            return []
+        }
+
+        return forecasts
+    }
+
+    private func getForecastValues(_ forecast: Forecast) -> [ForecastValue] {
+        guard let forecastValueSet = forecast.forecastValues,
+              let forecastValues = Array(forecastValueSet) as? [ForecastValue]
+        else {
+            return []
+        }
+
+        return forecastValues.sorted(by: { $0.index < $1.index })
+    }
+
     private func drawPredictions() -> some ChartContent {
-        ForEach(forecasts, id: \.id) { forecast in
-            ForEach(forecast.forecastValuesArray, id: \.self) { item in
-                LineMark(
-                    x: .value("Time", timeForIndex(item.index)),
-                    y: .value("Value", Int(item.value))
-                ).foregroundStyle(by: .value("Predictions", forecast.type ?? ""))
+        ForEach(determinations) { determination in
+            let forecasts = getForecasts(determination)
+
+            ForEach(forecasts) { forecast in
+                let forecastValues = getForecastValues(forecast)
+
+                ForEach(forecastValues) { forecastValue in
+                    LineMark(
+                        x: .value("Time", timeForIndex(forecastValue.index)),
+                        y: .value("Value", Int(forecastValue.value))
+                    )
+                    .foregroundStyle(by: .value("Predictions", forecast.type ?? ""))
+                }
             }
         }
     }

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

@@ -7,6 +7,7 @@ extension OrefDetermination {
         request.sortDescriptors = [NSSortDescriptor(keyPath: \OrefDetermination.deliverAt, ascending: false)]
         request.predicate = predicate
         request.fetchLimit = 1
+        request.returnsObjectsAsFaults = true
         return request
     }
 }

+ 3 - 1
Model/Helper/Forecast+helper.swift

@@ -7,11 +7,13 @@ public extension Forecast {
         request.sortDescriptors = [NSSortDescriptor(keyPath: \Forecast.date, ascending: ascending)]
         request.fetchLimit = 1
         request.predicate = predicate
+        request.returnsObjectsAsFaults = true
+
         return request
     }
 
     var forecastValuesArray: [ForecastValue] {
-        let set = forecastValues as? Set<ForecastValue> ?? []
+        let set = forecastValues ?? []
         return set.sorted { $0.index < $1.index }
     }
 }