Explorar el Código

fix for predictions color

polscm32 hace 2 años
padre
commit
9cb5edde3c
Se han modificado 1 ficheros con 23 adiciones y 60 borrados
  1. 23 60
      FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

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

@@ -171,7 +171,6 @@ struct MainChartView: View {
                     LazyVStack(spacing: 0) {
                     LazyVStack(spacing: 0) {
                         mainChart
                         mainChart
                         basalChart
                         basalChart
-
                     }.onChange(of: screenHours) { _ in
                     }.onChange(of: screenHours) { _ in
                         updateStartEndMarkers()
                         updateStartEndMarkers()
                         yAxisChartData()
                         yAxisChartData()
@@ -234,7 +233,7 @@ extension MainChartView {
                 drawFpus()
                 drawFpus()
                 drawBoluses()
                 drawBoluses()
                 drawTempTargets()
                 drawTempTargets()
-                drawPredictions()
+                drawForecasts()
                 drawGlucose()
                 drawGlucose()
                 drawManualGlucose()
                 drawManualGlucose()
 
 
@@ -258,9 +257,6 @@ extension MainChartView {
                 }
                 }
             }
             }
             .id("MainChart")
             .id("MainChart")
-            .onChange(of: glucoseFromPersistence.map(\.id)) { _ in
-//                calculatePredictions()
-            }
             .onChange(of: boluses) { _ in
             .onChange(of: boluses) { _ in
                 state.roundedTotalBolus = state.calculateTINS()
                 state.roundedTotalBolus = state.calculateTINS()
             }
             }
@@ -268,26 +264,22 @@ extension MainChartView {
                 calculateTTs()
                 calculateTTs()
             }
             }
             .onChange(of: didAppearTrigger) { _ in
             .onChange(of: didAppearTrigger) { _ in
-//                calculatePredictions()
                 calculateTTs()
                 calculateTTs()
             }
             }
-            .onChange(of: determinations.map(\.id)) { _ in
-//                calculatePredictions()
-            }
-            .onReceive(
-                Foundation.NotificationCenter.default
-                    .publisher(for: UIApplication.willEnterForegroundNotification)
-            ) { _ in
-//                calculatePredictions()
-            }
             .frame(minHeight: UIScreen.main.bounds.height * 0.3)
             .frame(minHeight: UIScreen.main.bounds.height * 0.3)
             .frame(width: fullWidth(viewWidth: screenSize.width))
             .frame(width: fullWidth(viewWidth: screenSize.width))
             .chartXScale(domain: startMarker ... endMarker)
             .chartXScale(domain: startMarker ... endMarker)
             .chartXAxis { mainChartXAxis }
             .chartXAxis { mainChartXAxis }
-            // .chartXAxis(.hidden)
+            .backport.chartXSelection(value: $selection)
             .chartYAxis { mainChartYAxis }
             .chartYAxis { mainChartYAxis }
             .chartYScale(domain: minValue ... maxValue)
             .chartYScale(domain: minValue ... maxValue)
-            .backport.chartXSelection(value: $selection)
+            .chartForegroundStyleScale([
+                "zt": Color.zt,
+                "uam": Color.uam,
+                "cob": .orange,
+                "iob": .blue
+            ])
+            .chartLegend(.hidden)
         }
         }
     }
     }
 
 
@@ -495,36 +487,22 @@ extension MainChartView {
         return forecastValues.sorted(by: { $0.index < $1.index })
         return forecastValues.sorted(by: { $0.index < $1.index })
     }
     }
 
 
-    private func drawPredictions() -> some ChartContent {
-        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 ?? ""))
+    private func drawForecasts() -> some ChartContent {
+        /// for every determination in determinations get the forecasts
+        ForEach(determinations.flatMap { determination -> [(id: UUID, forecast: Forecast, forecastValue: ForecastValue)] in
+            let forecasts = getForecasts(determination) /// returns array of Forecast objects
+            /// now get the values for every forecast and add it to a tuple, identify it with an ID
+            return forecasts.flatMap { forecast in
+                getForecastValues(forecast).map { forecastValue in
+                    (id: UUID(), forecast: forecast, forecastValue: forecastValue)
                 }
                 }
             }
             }
-        }
-    }
-
-    private func colorForType(_ type: PredictionType) -> Color {
-        switch type {
-        case .uam:
-            return .uam
-        case .cob:
-            return .orange
-        case .iob:
-            return .insulin
-        case .zt:
-            return .zt
-        default:
-            return .gray // Default color for unknown types
+        }, id: \.id) { tuple in
+            LineMark(
+                x: .value("Time", timeForIndex(tuple.forecastValue.index)),
+                y: .value("Value", Int(tuple.forecastValue.value))
+            )
+            .foregroundStyle(by: .value("Predictions", tuple.forecast.type ?? ""))
         }
         }
     }
     }
 
 
@@ -792,21 +770,6 @@ extension MainChartView {
         TempBasals = returnTempBasalRates
         TempBasals = returnTempBasalRates
     }
     }
 
 
-//    private func addPredictions(_ predictions: [Int], type: PredictionType, deliveredAt: Date, endMarker: Date) -> [Prediction] {
-//        var calculatedPredictions: [Prediction] = []
-//        predictions.indices.forEach { index in
-//            let predTime = Date(
-//                timeIntervalSince1970: deliveredAt.timeIntervalSince1970 + TimeInterval(index) * 5.minutes.timeInterval
-//            )
-//            if predTime.timeIntervalSince1970 < endMarker.timeIntervalSince1970 {
-//                calculatedPredictions.append(
-//                    Prediction(amount: predictions[index], timestamp: predTime, type: type)
-//                )
-//            }
-//        }
-//        return calculatedPredictions
-//    }
-
     private func findRegularBasalPoints(
     private func findRegularBasalPoints(
         timeBegin: TimeInterval,
         timeBegin: TimeInterval,
         timeEnd: TimeInterval,
         timeEnd: TimeInterval,