Przeglądaj źródła

Fix hours scaling typo

Yakov Karpov 5 lat temu
rodzic
commit
0076a0c536

+ 3 - 3
FreeAPS/Sources/Charts/Views/Charts/MainChartView.swift

@@ -2,7 +2,7 @@ import SwiftUI
 
 struct MainChartView: View {
     let maxWidth: CGFloat
-    @Binding var showHours: Int
+    let showHours: Int
     @Binding var glucoseData: [BloodGlucose]
     @Binding var predictionsData: [PredictionLineData]
     var body: some View {
@@ -15,7 +15,7 @@ struct MainChartView: View {
                 minValue: minValue,
                 maxValue: maxValue,
                 maxWidth: maxWidth,
-                showHours: $showHours,
+                showHours: showHours,
                 glucoseData: $glucoseData
             ) { value in
                 GlucosePointView(value: value)
@@ -25,7 +25,7 @@ struct MainChartView: View {
                 maxValue: maxValue,
                 maxWidth: maxWidth,
                 data: $predictionsData,
-                showHours: $showHours
+                showHours: showHours
             )
         }
     }

+ 8 - 7
FreeAPS/Sources/Charts/Views/Charts/PointChartView.swift

@@ -5,18 +5,18 @@ struct PointChartView<PointEntry: View>: View {
     let maxValue: Int
     let maxWidth: CGFloat
 
-    @Binding var showHours: Int
+    let showHours: Int
     @Binding var glucoseData: [BloodGlucose]
 
     let pointEntry: (_: Int?) -> PointEntry
 
-    let hoursMultiplier: Double = 14
+    let hoursMultiplier: Double = 12
     let pointSize: CGFloat = ChartsConfig.glucosePointSize / 2
 
     public var body: some View {
         let firstEntryTime = glucoseData
-            .map(\.date)
-            .first ?? UInt64(Date().timeIntervalSince1970)
+            .map(\.dateString)
+            .first ?? Date()
 
         var width: CGFloat = 0
         if let lastGlucose = glucoseData.last {
@@ -39,14 +39,15 @@ struct PointChartView<PointEntry: View>: View {
 }
 
 extension PointChartView {
-    func calculateXPosition(glucose: BloodGlucose, firstEntryTime: UInt64) -> CGFloat {
-        let xPositionIndex = CGFloat(glucose.date - firstEntryTime) / CGFloat(300 * showHours)
+    func calculateXPosition(glucose: BloodGlucose, firstEntryTime: Date) -> CGFloat {
+        let xPositionIndex = CGFloat(DateInterval(start: firstEntryTime, end: glucose.dateString).duration) /
+            CGFloat(300)
         return (xPositionIndex * maxWidth / CGFloat(Double(showHours) * hoursMultiplier)) + pointSize
     }
 
     func getGlucosePoints(
         height: CGFloat,
-        firstEntryTime: UInt64
+        firstEntryTime: Date
     ) -> [GlucosePointData] {
         /// y = mx + b where m = scalingFactor, b = addendum, x = value, y = mapped value
         let scalingFactor = Double(height - pointSize * 2) / Double(maxValue - minValue)

+ 2 - 2
FreeAPS/Sources/Charts/Views/Charts/PredictionsChartView.swift

@@ -5,7 +5,7 @@ public struct PredictionsChartView: View {
     let maxValue: Int
     let maxWidth: CGFloat
     @Binding var data: [PredictionLineData]
-    @Binding var showHours: Int
+    let showHours: Int
 
     var chartsData: some View {
         ForEach(0 ..< data.count, id: \.self) { index -> AnyView in
@@ -14,7 +14,7 @@ public struct PredictionsChartView: View {
                     minValue: minValue,
                     maxValue: maxValue,
                     maxWidth: maxWidth,
-                    showHours: $showHours,
+                    showHours: showHours,
                     glucoseData: $data[index].values
                 ) { value in
                     PredictionPointView(

+ 13 - 10
FreeAPS/Sources/Modules/Home/View/HomeRootView.swift

@@ -8,15 +8,12 @@ extension Home {
         var chart: some View {
             GeometryReader { geo in
                 ScrollView(.horizontal, showsIndicators: false) {
-                    PointChartView(
-                        minValue: 20,
-                        maxValue: 300,
+                    MainChartView(
                         maxWidth: geo.size.width,
-                        showHours: $showHours,
-                        glucoseData: $viewModel.glucose
-                    ) { value in
-                        GlucosePointView(value: value)
-                    }
+                        showHours: showHours,
+                        glucoseData: $viewModel.glucose,
+                        predictionsData: .constant([])
+                    )
                 }
             }
         }
@@ -29,8 +26,14 @@ extension Home {
                         Text("Header")
                     }
                     ScrollView(.vertical, showsIndicators: false) {
-                        // chart.frame(height: 300)
-                        GlucoseChartView(glucose: $viewModel.glucose, suggestion: $viewModel.suggestion).frame(height: 150)
+                        HoursPickerView(selectedHour: $showHours)
+                        chart
+                            .frame(height: 300)
+                            .padding(.vertical)
+                            .background(Color(.systemGray6))
+                            .cornerRadius(20)
+                            .padding()
+                        // GlucoseChartView(glucose: $viewModel.glucose, suggestion: $viewModel.suggestion).frame(height: 150)
                         if let reason = viewModel.suggestion?.reason {
                             Text(reason).font(.caption).padding()
                         }