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

Add Manual Glucose to Main Chart (#11)

* Add manual glucose point mark (symbol drop.fill) to chart

* Add comment
Deniz Cengiz 2 лет назад
Родитель
Сommit
b1e1ad2bac

+ 0 - 7
FreeAPS/Sources/Modules/Home/HomeProvider.swift

@@ -33,13 +33,6 @@ extension Home {
             }
         }
 
-        func manualGlucose(hours: Int) -> [BloodGlucose] {
-            glucoseStorage.recent().filter {
-                $0.type == GlucoseType.manual.rawValue &&
-                    $0.dateString.addingTimeInterval(hours.hours.timeInterval) > Date()
-            }
-        }
-
         func pumpHistory(hours: Int) -> [PumpHistoryEvent] {
             pumpHistoryStorage.recent().filter {
                 $0.timestamp.addingTimeInterval(hours.hours.timeInterval) > Date()

+ 11 - 3
FreeAPS/Sources/Modules/Home/HomeStateModel.swift

@@ -12,7 +12,7 @@ extension Home {
         private let timer = DispatchTimer(timeInterval: 5)
         private(set) var filteredHours = 24
         @Published var glucose: [BloodGlucose] = []
-        @Published var isManual: [BloodGlucose] = []
+        @Published var manualGlucose: [BloodGlucose] = []
         @Published var announcement: [Announcement] = []
         @Published var suggestion: Suggestion?
         @Published var uploadStats = false
@@ -234,8 +234,16 @@ extension Home {
         private func setupGlucose() {
             DispatchQueue.main.async { [weak self] in
                 guard let self = self else { return }
-                self.isManual = self.provider.manualGlucose(hours: self.filteredHours)
-                self.glucose = self.provider.filteredGlucose(hours: self.filteredHours)
+                let filteredGlucose = self.provider.filteredGlucose(hours: self.filteredHours)
+
+                for glucose in filteredGlucose {
+                    if glucose.type == GlucoseType.manual.rawValue {
+                        self.manualGlucose.append(glucose)
+                    } else {
+                        self.glucose.append(glucose)
+                    }
+                }
+
                 self.recentGlucose = self.glucose.last
                 if self.glucose.count >= 2 {
                     self.glucoseDelta = (self.recentGlucose?.glucose ?? 0) - (self.glucose[self.glucose.count - 2].glucose ?? 0)

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

@@ -60,6 +60,7 @@ struct MainChartView: View {
     }
 
     @Binding var glucose: [BloodGlucose]
+    @Binding var manualGlucose: [BloodGlucose]
     @Binding var units: GlucoseUnits
     @Binding var eventualBG: Int?
     @Binding var suggestion: Suggestion?
@@ -332,6 +333,19 @@ extension MainChartView {
                         }
                     }
                 }
+                /// manual glucose mark
+                ForEach(manualGlucose) { item in
+                    if let manualGlucose = item.glucose {
+                        PointMark(
+                            x: .value("Time", item.dateString, unit: .second),
+                            y: .value("Value", Decimal(manualGlucose) * conversionFactor)
+                        )
+                        .symbol {
+                            Image(systemName: "drop.fill").font(.system(size: 10)).symbolRenderingMode(.monochrome)
+                                .foregroundStyle(.red)
+                        }
+                    }
+                }
             }.id("MainChart")
                 .onChange(of: glucose) { _ in
                     calculatePredictions()
@@ -377,16 +391,6 @@ extension MainChartView {
                         AxisValueLabel(format: .dateTime.hour(.defaultDigits(amPM: .narrow)), anchor: .top)
                     }
                 }
-//                .chartYAxis {
-//                    AxisMarks { _ in
-//                        if displayYgridLines {
-//                            AxisGridLine(stroke: .init(lineWidth: 0.3, dash: [2, 3]))
-//                        } else {
-//                            AxisGridLine(stroke: .init(lineWidth: 0, dash: [2, 3]))
-//                        }
-//                        AxisValueLabel()
-//                    }
-//                }
                 .chartYAxis {
                     AxisMarks(position: .trailing) { value in
                         let upperLimit = units == .mgdL ? 400 : 22.2

+ 1 - 0
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -358,6 +358,7 @@ extension Home {
 
                 MainChartView(
                     glucose: $state.glucose,
+                    manualGlucose: $state.manualGlucose,
                     units: $state.units,
                     eventualBG: $state.eventualBG,
                     suggestion: $state.suggestion,