Przeglądaj źródła

Merge pull request #155 from MikePlante1/simple-la-dynamic-color

Move units to state instead of detailedState for LA
Deniz Cengiz 1 rok temu
rodzic
commit
1651ea6f8d

+ 1 - 1
FreeAPS/Sources/Services/LiveActivity/LiveActitiyAttributes.swift

@@ -13,6 +13,7 @@ struct LiveActivityAttributes: ActivityAttributes {
     }
 
     struct ContentState: Codable, Hashable {
+        let unit: String
         let bg: String
         let direction: String?
         let change: String
@@ -33,7 +34,6 @@ struct LiveActivityAttributes: ActivityAttributes {
         let rotationDegrees: Double
         let cob: Decimal
         let iob: Decimal
-        let unit: String
         let isOverrideActive: Bool
         let overrideName: String
         let overrideDate: Date

+ 1 - 1
FreeAPS/Sources/Services/LiveActivity/LiveActivityAttributes+Helper.swift

@@ -109,7 +109,6 @@ extension LiveActivityAttributes.ContentState {
                 rotationDegrees: rotationDegrees,
                 cob: Decimal(determination?.cob ?? 0),
                 iob: determination?.iob ?? 0 as Decimal,
-                unit: settings.units.rawValue,
                 isOverrideActive: override?.isActive ?? false,
                 overrideName: override?.overrideName ?? "Override",
                 overrideDate: override?.date ?? Date(),
@@ -123,6 +122,7 @@ extension LiveActivityAttributes.ContentState {
         }
 
         self.init(
+            unit: settings.units.rawValue,
             bg: formattedBG,
             direction: trendString,
             change: change,

+ 1 - 0
FreeAPS/Sources/Services/LiveActivity/LiveActivityBridge.swift

@@ -241,6 +241,7 @@ final class LiveActivityBridge: Injectable, ObservableObject, SettingsObserver {
                 let expired = ActivityContent(
                     state: LiveActivityAttributes
                         .ContentState(
+                            unit: settings.units.rawValue,
                             bg: "--",
                             direction: nil,
                             change: "--",

+ 13 - 3
LiveActivity/LiveActivity.swift

@@ -11,8 +11,7 @@ struct LiveActivity: Widget {
 
             var glucoseColor: Color {
                 let state = context.state
-                let detailedState = state.detailedViewState
-                let isMgdL = detailedState?.unit == "mg/dL"
+                let isMgdL = state.unit == "mg/dL"
 
                 // TODO: workaround for now: set low value to 55, to have dynamic color shades between 55 and user-set low (approx. 70); same for high glucose
                 let hardCodedLow = isMgdL ? Decimal(55) : 55.asMmolL
@@ -85,7 +84,6 @@ private extension LiveActivityAttributes.ContentState {
         rotationDegrees: 0,
         cob: 20,
         iob: 1.5,
-        unit: GlucoseUnits.mgdL.rawValue,
         isOverrideActive: false,
         overrideName: "Exercise",
         overrideDate: Date().addingTimeInterval(-3600),
@@ -99,6 +97,7 @@ private extension LiveActivityAttributes.ContentState {
     // Use mmol/l notation with decimal point as well for the same reason, it uses up to 4 characters, while mg/dl uses up to 3
     static var testWide: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00.0",
             direction: "→",
             change: "+0.0",
@@ -114,6 +113,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testVeryWide: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00.0",
             direction: "↑↑",
             change: "+0.0",
@@ -129,6 +129,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testSuperWide: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00.0",
             direction: "↑↑↑",
             change: "+0.0",
@@ -145,6 +146,7 @@ private extension LiveActivityAttributes.ContentState {
     // 2 characters for BG, 1 character for change is the minimum that will be shown
     static var testNarrow: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00",
             direction: "↑",
             change: "+0",
@@ -160,6 +162,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testMedium: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "000",
             direction: "↗︎",
             change: "+00",
@@ -175,6 +178,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testExpired: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "--",
             direction: nil,
             change: "--",
@@ -190,6 +194,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testWideDetailed: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00.0",
             direction: "→",
             change: "+0.0",
@@ -205,6 +210,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testVeryWideDetailed: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00.0",
             direction: "↑↑",
             change: "+0.0",
@@ -220,6 +226,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testSuperWideDetailed: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00.0",
             direction: "↑↑↑",
             change: "+0.0",
@@ -236,6 +243,7 @@ private extension LiveActivityAttributes.ContentState {
     // 2 characters for BG, 1 character for change is the minimum that will be shown
     static var testNarrowDetailed: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "00",
             direction: "↑",
             change: "+0",
@@ -251,6 +259,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testMediumDetailed: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "000",
             direction: "↗︎",
             change: "+00",
@@ -266,6 +275,7 @@ private extension LiveActivityAttributes.ContentState {
 
     static var testExpiredDetailed: LiveActivityAttributes.ContentState {
         LiveActivityAttributes.ContentState(
+            unit: "mg/dL",
             bg: "--",
             direction: nil,
             change: "--",

+ 3 - 3
LiveActivity/Views/LiveActivityChartView.swift

@@ -17,7 +17,7 @@ struct LiveActivityChartView: View {
 
     var body: some View {
         let state = context.state
-        let isMgdL: Bool = additionalState.unit == "mg/dL"
+        let isMgdL: Bool = state.unit == "mg/dL"
 
         // Determine scale
         let minValue = min(additionalState.chart.min() ?? 39, 39) as Decimal
@@ -84,7 +84,7 @@ struct LiveActivityChartView: View {
                 AxisValueLabel().foregroundStyle(.primary).font(.footnote)
             }
         }
-        .chartYScale(domain: additionalState.unit == "mg/dL" ? minValue ... maxValue : minValue.asMmolL ... maxValue.asMmolL)
+        .chartYScale(domain: state.unit == "mg/dL" ? minValue ... maxValue : minValue.asMmolL ... maxValue.asMmolL)
         .chartYAxis(.hidden)
         .chartPlotStyle { plotContent in
             plotContent
@@ -123,7 +123,7 @@ struct LiveActivityChartView: View {
 
     private func drawChart(yAxisRuleMarkMin _: Decimal, yAxisRuleMarkMax _: Decimal) -> some ChartContent {
         ForEach(additionalState.chart.indices, id: \.self) { index in
-            let isMgdL = additionalState.unit == "mg/dL"
+            let isMgdL = context.state.unit == "mg/dL"
             let currentValue = additionalState.chart[index]
             let displayValue = isMgdL ? currentValue : currentValue.asMmolL
             let chartDate = additionalState.chartDate[index] ?? Date()

+ 1 - 2
LiveActivity/Views/LiveActivityView.swift

@@ -19,8 +19,7 @@ struct LiveActivityView: View {
 
     private var glucoseColor: Color {
         let state = context.state
-        let detailedState = state.detailedViewState
-        let isMgdL = detailedState?.unit == "mg/dL"
+        let isMgdL = state.unit == "mg/dL"
 
         // TODO: workaround for now: set low value to 55, to have dynamic color shades between 55 and user-set low (approx. 70); same for high glucose
         let hardCodedLow = isMgdL ? Decimal(55) : 55.asMmolL