Sfoglia il codice sorgente

add temp target..WiP

polscm32 2 anni fa
parent
commit
7af271c961
1 ha cambiato i file con 34 aggiunte e 61 eliminazioni
  1. 34 61
      FreeAPS/Sources/Modules/Home/View/Chart/MainChartView2.swift

+ 34 - 61
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView2.swift

@@ -35,6 +35,12 @@ private struct ChartBolus: Hashable {
     let nearestGlucose: BloodGlucose
 }
 
+private struct ChartTempTarget: Hashable {
+    let amount: Decimal?
+    let start: Date
+    let end: Date?
+}
+
 private enum PredictionType: Hashable {
     case iob
     case cob
@@ -75,7 +81,7 @@ struct MainChartView2: View {
     @State var didAppearTrigger = false
     @State private var BasalProfiles: [BasalProfile] = []
     @State private var TempBasals: [PumpHistoryEvent] = []
-    @State private var ChartTTs: [TempTarget] = []
+    @State private var ChartTempTargets: [ChartTempTarget] = []
     @State private var Predictions: [Prediction] = []
     @State private var ChartCarbs: [Carb] = []
     @State private var ChartFpus: [Carb] = []
@@ -113,10 +119,7 @@ struct MainChartView2: View {
             ScrollViewReader { scroller in
                 ScrollView(.horizontal, showsIndicators: false) {
                     VStack {
-                        ZStack {
-                            TTs()
-                            MainChart()
-                        }
+                        MainChart()
 
                         BasalChart()
                             .padding(.bottom, 8)
@@ -210,26 +213,21 @@ extension MainChartView2 {
                         Text(bolusFormatter.string(from: bolusAmount as NSNumber)!).font(.caption2)
                     }
                 }
-                /*  ForEach(ChartTTs, id: \.self) { tt in
-                     //  let bolusAmount = tt.amount
-                     LineMark(
-                         x: .value("Time", tt.duration),
-                         y: .value("Value", tt.targetBottom)
-                     )
-                     .foregroundStyle(Color.insulin)
-                 }*/
-                /*     ForEach(ChartTTs, id: \.self) { profile in
-                     LineMark(
-                         x: .value("Start Date", profile.createdAt),
-                         y: .value("Amount", profile.targetBottom),
-                         series: .value("profile", "profile")
-                     ).lineStyle(.init(lineWidth: 2, dash: [2, 3]))
-                     LineMark(
-                         x: .value("End Date", profile.createdAt + 3 ?? endMarker),
-                         y: .value("Amount", profile.amount),
-                         series: .value("profile", "profile")
-                     ).lineStyle(.init(lineWidth: 2, dash: [2, 3]))
-                 }*/
+
+                // MARK: this code causes 'compiler is unable to type-check this expression'-error
+
+                ForEach(ChartTempTargets, id: \.self) { tt in
+                    PointMark(
+                        x: .value("Time", tt.start),
+                        y: .value("Value", tt.amount)
+                    )
+                    .foregroundStyle(Color.insulin).lineStyle(.init(lineWidth: 2, dash: [2, 3]))
+                    PointMark(
+                        x: .value("Time", tt.end),
+                        y: .value("Value", tt.amount)
+                    )
+                    .foregroundStyle(Color.insulin).lineStyle(.init(lineWidth: 2, dash: [2, 3]))
+                }
                 ForEach(Predictions, id: \.self) { info in
                     if info.type == .uam {
                         LineMark(
@@ -286,6 +284,9 @@ extension MainChartView2 {
                 .onChange(of: boluses) { _ in
                     calculateBoluses()
                 }
+                .onChange(of: tempTargets) { _ in
+                    calculateTTs()
+                }
                 .onChange(of: didAppearTrigger) { _ in
                     calculatePredictions()
                 }.onChange(of: suggestion) { _ in
@@ -419,12 +420,6 @@ extension MainChartView2 {
         }
     }
 
-    private func TTs() -> some View {
-        Chart(tempTargets) {
-            PointMark(x: .value("Time", $0.dateString), y: .value("value", $0.value))
-        }
-    }
-
     private func Legend() -> some View {
         HStack {
             Image(systemName: "line.diagonal")
@@ -525,39 +520,17 @@ extension MainChartView2 {
     }
 
     private func calculateTTs() {
-        var calculatedTTs: [TempTarget] = []
-        tempTargets.indices.forEach { index in
-            calculatedTTs.append(TempTarget(
-                name: tempTargets[index].name,
-                createdAt: tempTargets[index].createdAt,
-                targetTop: tempTargets[index].targetTop,
-                targetBottom: tempTargets[index].targetBottom,
-                duration: tempTargets[index].duration,
-                enteredBy: tempTargets[index].enteredBy,
-                reason: tempTargets[index].enteredBy
-            ))
+        var calculatedTTs: [ChartTempTarget] = []
+        tempTargets.forEach { tt in
+
+            let end = tt.createdAt.addingTimeInterval(TimeInterval(tt.duration))
+
+            calculatedTTs
+                .append(ChartTempTarget(amount: tt.targetTop, start: tt.createdAt, end: end))
         }
-        ChartTTs = calculatedTTs
+        ChartTempTargets = calculatedTTs
     }
 
-    /* private func calculateTTs() {
-         var calculatedTTs: [TempTarget] = []
-         tempTargets.forEach { tt in
-             calculatedTTs
-                 .append(TempTarget(
-                     id: tt.id,
-                     name: tt.name,
-                     createdAt: tt.createdAt,
-                     targetTop: tt.targetTop,
-                     targetBottom: tt.targetBottom,
-                     duration: tt.duration,
-                     enteredBy: tt.enteredBy,
-                     reason: tt.reason
-                 ))
-         }
-         ChartTTs = calculatedTTs
-     }*/
-
     private func calculatePredictions() {
         var calculatedPredictions: [Prediction] = []
         let uam = suggestion?.predictions?.uam ?? []