Jelajahi Sumber

Port over (and slightly refactor) chart fixes (via #431)

Deniz Cengiz 1 tahun lalu
induk
melakukan
f3209c56f4

+ 15 - 16
Trio/Sources/Modules/Onboarding/View/OnboardingSteps/BasalProfileStepView.swift

@@ -13,10 +13,7 @@ struct BasalProfileStepView: View {
     @Bindable var state: Onboarding.StateModel
     @State private var refreshUI = UUID() // to update chart when slider value changes
     @State private var therapyItems: [TherapySettingItem] = []
-
-    // For chart scaling
-    private let chartScale = Calendar.current
-        .date(from: DateComponents(year: 2001, month: 01, day: 01, hour: 0, minute: 0, second: 0))
+    @State private var now = Date()
 
     private var rateFormatter: NumberFormatter {
         let formatter = NumberFormatter()
@@ -158,17 +155,19 @@ struct BasalProfileStepView: View {
             ForEach(Array(state.basalProfileItems.enumerated()), id: \.element.id) { index, item in
                 let displayValue = state.basalProfileRateValues[item.rateIndex]
 
-                let tzOffset = TimeZone.current.secondsFromGMT() * -1
-                let startDate = Date(timeIntervalSinceReferenceDate: state.basalProfileTimeValues[item.timeIndex])
-                    .addingTimeInterval(TimeInterval(tzOffset))
-                let endDate = state.basalProfileItems.count > index + 1 ?
-                    Date(
-                        timeIntervalSinceReferenceDate: state
-                            .basalProfileTimeValues[state.basalProfileItems[index + 1].timeIndex]
-                    )
-                    .addingTimeInterval(TimeInterval(tzOffset)) :
-                    Date(timeIntervalSinceReferenceDate: state.basalProfileTimeValues.last!).addingTimeInterval(30 * 60)
-                    .addingTimeInterval(TimeInterval(tzOffset))
+                let startDate = Calendar.current
+                    .startOfDay(for: now)
+                    .addingTimeInterval(state.basalProfileTimeValues[item.timeIndex])
+
+                var offset: TimeInterval {
+                    if state.basalProfileItems.count > index + 1 {
+                        return state.basalProfileTimeValues[state.basalProfileItems[index + 1].timeIndex]
+                    } else {
+                        return state.basalProfileTimeValues.last! + 30 * 60
+                    }
+                }
+
+                let endDate = Calendar.current.startOfDay(for: now).addingTimeInterval(offset)
 
                 RectangleMark(
                     xStart: .value("start", startDate),
@@ -201,7 +200,7 @@ struct BasalProfileStepView: View {
             }
         }
         .chartXScale(
-            domain: Calendar.current.startOfDay(for: chartScale!) ... Calendar.current.startOfDay(for: chartScale!)
+            domain: Calendar.current.startOfDay(for: now) ... Calendar.current.startOfDay(for: now)
                 .addingTimeInterval(60 * 60 * 24)
         )
         .chartYAxis {

+ 15 - 16
Trio/Sources/Modules/Onboarding/View/OnboardingSteps/CarbRatioStepView.swift

@@ -13,10 +13,7 @@ struct CarbRatioStepView: View {
     @Bindable var state: Onboarding.StateModel
     @State private var refreshUI = UUID() // to update chart when slider value changes
     @State private var therapyItems: [TherapySettingItem] = []
-
-    // For chart scaling
-    private let chartScale = Calendar.current
-        .date(from: DateComponents(year: 2001, month: 01, day: 01, hour: 0, minute: 0, second: 0))
+    @State private var now = Date()
 
     private var formatter: NumberFormatter {
         let formatter = NumberFormatter()
@@ -133,17 +130,19 @@ struct CarbRatioStepView: View {
             ForEach(Array(state.carbRatioItems.enumerated()), id: \.element.id) { index, item in
                 let displayValue = state.carbRatioRateValues[item.rateIndex]
 
-                let tzOffset = TimeZone.current.secondsFromGMT() * -1
-                let startDate = Date(timeIntervalSinceReferenceDate: state.carbRatioTimeValues[item.timeIndex])
-                    .addingTimeInterval(TimeInterval(tzOffset))
-                let endDate = state.carbRatioItems.count > index + 1 ?
-                    Date(
-                        timeIntervalSinceReferenceDate: state
-                            .carbRatioTimeValues[state.carbRatioItems[index + 1].timeIndex]
-                    )
-                    .addingTimeInterval(TimeInterval(tzOffset)) :
-                    Date(timeIntervalSinceReferenceDate: state.carbRatioTimeValues.last!).addingTimeInterval(30 * 60)
-                    .addingTimeInterval(TimeInterval(tzOffset))
+                let startDate = Calendar.current
+                    .startOfDay(for: now)
+                    .addingTimeInterval(state.carbRatioTimeValues[item.timeIndex])
+
+                var offset: TimeInterval {
+                    if state.carbRatioItems.count > index + 1 {
+                        return state.carbRatioTimeValues[state.carbRatioItems[index + 1].timeIndex]
+                    } else {
+                        return state.carbRatioTimeValues.last! + 30 * 60
+                    }
+                }
+
+                let endDate = Calendar.current.startOfDay(for: now).addingTimeInterval(offset)
 
                 RectangleMark(
                     xStart: .value("start", startDate),
@@ -176,7 +175,7 @@ struct CarbRatioStepView: View {
             }
         }
         .chartXScale(
-            domain: Calendar.current.startOfDay(for: chartScale!) ... Calendar.current.startOfDay(for: chartScale!)
+            domain: Calendar.current.startOfDay(for: now) ... Calendar.current.startOfDay(for: now)
                 .addingTimeInterval(60 * 60 * 24)
         )
         .chartYAxis {

+ 19 - 31
Trio/Sources/Modules/Onboarding/View/OnboardingSteps/GlucoseTargetStepView.swift

@@ -5,6 +5,7 @@
 //  Created by Marvin Polscheit on 19.03.25.
 //
 import Charts
+import Foundation
 import SwiftUI
 import UIKit
 
@@ -13,6 +14,7 @@ struct GlucoseTargetStepView: View {
     @Bindable var state: Onboarding.StateModel
     @State private var refreshUI = UUID() // to update chart when slider value changes
     @State private var therapyItems: [TherapySettingItem] = []
+    @State private var now = Date()
 
     // Formatter for glucose values
     private var numberFormatter: NumberFormatter {
@@ -29,10 +31,6 @@ struct GlucoseTargetStepView: View {
         return formatter
     }
 
-    // For chart scaling
-    private let chartScale = Calendar.current
-        .date(from: DateComponents(year: 2001, month: 01, day: 01, hour: 0, minute: 0, second: 0))
-
     var body: some View {
         LazyVStack {
             VStack(alignment: .leading, spacing: 0) {
@@ -83,33 +81,19 @@ struct GlucoseTargetStepView: View {
             ForEach(Array(state.targetItems.enumerated()), id: \.element.id) { index, item in
                 let displayValue = state.targetRateValues[item.lowIndex]
 
-                let tzOffset = TimeZone.current.secondsFromGMT() * -1
-                let startDate = Date(timeIntervalSinceReferenceDate: state.targetTimeValues[item.timeIndex])
-                    .addingTimeInterval(TimeInterval(tzOffset))
-                let endDate = state.targetItems.count > index + 1 ?
-                    Date(
-                        timeIntervalSinceReferenceDate: state
-                            .targetTimeValues[state.targetItems[index + 1].timeIndex]
-                    )
-                    .addingTimeInterval(TimeInterval(tzOffset)) :
-                    Date(timeIntervalSinceReferenceDate: state.targetTimeValues.last!).addingTimeInterval(30 * 60)
-                    .addingTimeInterval(TimeInterval(tzOffset))
+                let startDate = Calendar.current
+                    .startOfDay(for: now)
+                    .addingTimeInterval(state.targetTimeValues[item.timeIndex])
 
-                RectangleMark(
-                    xStart: .value("start", startDate),
-                    xEnd: .value("end", endDate),
-                    yStart: .value("rate-start", displayValue),
-                    yEnd: .value("rate-end", 0)
-                ).foregroundStyle(
-                    .linearGradient(
-                        colors: [
-                            Color.green.opacity(0.6),
-                            Color.green.opacity(0.1)
-                        ],
-                        startPoint: .bottom,
-                        endPoint: .top
-                    )
-                ).alignsMarkStylesWithPlotArea()
+                var offset: TimeInterval {
+                    if state.targetItems.count > index + 1 {
+                        return state.targetTimeValues[state.targetItems[index + 1].timeIndex]
+                    } else {
+                        return state.targetTimeValues.last! + 30 * 60
+                    }
+                }
+
+                let endDate = Calendar.current.startOfDay(for: now).addingTimeInterval(offset)
 
                 LineMark(x: .value("End Date", startDate), y: .value("Ratio", displayValue))
                     .lineStyle(.init(lineWidth: 1)).foregroundStyle(Color.green)
@@ -126,7 +110,7 @@ struct GlucoseTargetStepView: View {
             }
         }
         .chartXScale(
-            domain: Calendar.current.startOfDay(for: chartScale!) ... Calendar.current.startOfDay(for: chartScale!)
+            domain: Calendar.current.startOfDay(for: now) ... Calendar.current.startOfDay(for: now)
                 .addingTimeInterval(60 * 60 * 24)
         )
         .chartYAxis {
@@ -135,5 +119,9 @@ struct GlucoseTargetStepView: View {
                 AxisGridLine(centered: true, stroke: StrokeStyle(lineWidth: 1, dash: [2, 4]))
             }
         }
+        .chartYScale(
+            domain: (state.units == .mgdL ? Decimal(72) : Decimal(72).asMmolL) ...
+                (state.units == .mgdL ? Decimal(180) : Decimal(180).asMmolL)
+        )
     }
 }

+ 15 - 12
Trio/Sources/Modules/Onboarding/View/OnboardingSteps/InsulinSensitivityStepView.swift

@@ -13,6 +13,7 @@ struct InsulinSensitivityStepView: View {
     @Bindable var state: Onboarding.StateModel
     @State private var refreshUI = UUID() // to update chart when slider value changes
     @State private var therapyItems: [TherapySettingItem] = []
+    @State private var now = Date()
 
     // For chart scaling
     private let chartScale = Calendar.current
@@ -145,17 +146,19 @@ struct InsulinSensitivityStepView: View {
             ForEach(Array(state.isfItems.enumerated()), id: \.element.id) { index, item in
                 let displayValue = state.isfRateValues[item.rateIndex]
 
-                let tzOffset = TimeZone.current.secondsFromGMT() * -1
-                let startDate = Date(timeIntervalSinceReferenceDate: state.isfTimeValues[item.timeIndex])
-                    .addingTimeInterval(TimeInterval(tzOffset))
-                let endDate = state.isfItems.count > index + 1 ?
-                    Date(
-                        timeIntervalSinceReferenceDate: state
-                            .isfTimeValues[state.isfItems[index + 1].timeIndex]
-                    )
-                    .addingTimeInterval(TimeInterval(tzOffset)) :
-                    Date(timeIntervalSinceReferenceDate: state.isfTimeValues.last!).addingTimeInterval(30 * 60)
-                    .addingTimeInterval(TimeInterval(tzOffset))
+                let startDate = Calendar.current
+                    .startOfDay(for: now)
+                    .addingTimeInterval(state.isfTimeValues[item.timeIndex])
+
+                var offset: TimeInterval {
+                    if state.isfItems.count > index + 1 {
+                        return state.isfTimeValues[state.isfItems[index + 1].timeIndex]
+                    } else {
+                        return state.isfTimeValues.last! + 30 * 60
+                    }
+                }
+
+                let endDate = Calendar.current.startOfDay(for: now).addingTimeInterval(offset)
 
                 RectangleMark(
                     xStart: .value("start", startDate),
@@ -188,7 +191,7 @@ struct InsulinSensitivityStepView: View {
             }
         }
         .chartXScale(
-            domain: Calendar.current.startOfDay(for: chartScale!) ... Calendar.current.startOfDay(for: chartScale!)
+            domain: Calendar.current.startOfDay(for: now) ... Calendar.current.startOfDay(for: now)
                 .addingTimeInterval(60 * 60 * 24)
         )
         .chartYAxis {