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

Remove offset for dynamic glucose color & commented out code

Deniz Cengiz 1 год назад
Родитель
Сommit
2be8d8e5c9

+ 3 - 4
FreeAPS/Sources/Helpers/DynamicGlucoseColor.swift

@@ -7,15 +7,14 @@ public func getDynamicGlucoseColor(
     highGlucoseColorValue: Decimal,
     lowGlucoseColorValue: Decimal,
     targetGlucose: Decimal,
-    glucoseColorScheme: GlucoseColorScheme,
-    offset: Decimal
+    glucoseColorScheme: GlucoseColorScheme
 ) -> Color {
     // Only use calculateHueBasedGlucoseColor if the setting is enabled in preferences
     if glucoseColorScheme == .dynamicColor {
         return calculateHueBasedGlucoseColor(
             glucoseValue: glucoseValue,
-            highGlucose: highGlucoseColorValue + (offset * 1.75),
-            lowGlucose: lowGlucoseColorValue - offset,
+            highGlucose: highGlucoseColorValue,
+            lowGlucose: lowGlucoseColorValue,
             targetGlucose: targetGlucose
         )
     }

+ 7 - 4
FreeAPS/Sources/Modules/Bolus/View/ForecastChart.swift

@@ -122,13 +122,16 @@ struct ForecastChart: View {
             let highGlucose = units == .mgdL ? state.highGlucose : state.highGlucose.asMgdL
             let targetGlucose = (state.determination.first?.currentTarget ?? state.currentBGTarget as NSDecimalNumber) as Decimal
 
+            // 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 = Decimal(55)
+            let hardCodedHigh = Decimal(220)
+
             let pointMarkColor: Color = FreeAPS.getDynamicGlucoseColor(
                 glucoseValue: Decimal(item.glucose),
-                highGlucoseColorValue: highGlucose,
-                lowGlucoseColorValue: lowGlucose,
+                highGlucoseColorValue: hardCodedHigh,
+                lowGlucoseColorValue: hardCodedLow,
                 targetGlucose: targetGlucose,
-                glucoseColorScheme: state.glucoseColorScheme,
-                offset: 20
+                glucoseColorScheme: state.glucoseColorScheme
             )
 
             if !state.isSmoothingEnabled {

+ 5 - 7
FreeAPS/Sources/Modules/Home/View/Chart/DummyCharts.swift

@@ -11,18 +11,16 @@ extension MainChartView {
                 let highColor = FreeAPS.getDynamicGlucoseColor(
                     glucoseValue: highGlucose,
                     highGlucoseColorValue: highGlucose,
-                    lowGlucoseColorValue: highGlucose,
-                    targetGlucose: units == .mgdL ? currentGlucoseTarget : currentGlucoseTarget.asMmolL,
-                    glucoseColorScheme: glucoseColorScheme,
-                    offset: units == .mgdL ? Decimal(20) : Decimal(20).asMmolL
+                    lowGlucoseColorValue: lowGlucose,
+                    targetGlucose: currentGlucoseTarget,
+                    glucoseColorScheme: glucoseColorScheme
                 )
                 let lowColor = FreeAPS.getDynamicGlucoseColor(
                     glucoseValue: lowGlucose,
                     highGlucoseColorValue: highGlucose,
                     lowGlucoseColorValue: lowGlucose,
-                    targetGlucose: units == .mgdL ? currentGlucoseTarget : currentGlucoseTarget.asMmolL,
-                    glucoseColorScheme: glucoseColorScheme,
-                    offset: units == .mgdL ? Decimal(20) : Decimal(20).asMmolL
+                    targetGlucose: currentGlucoseTarget,
+                    glucoseColorScheme: glucoseColorScheme
                 )
 
                 RuleMark(y: .value("High", highGlucose))

+ 7 - 4
FreeAPS/Sources/Modules/Home/View/Chart/GlucoseChartView.swift

@@ -23,13 +23,16 @@ struct GlucoseChartView: ChartContent {
             let lowGlucose = units == .mgdL ? lowGlucose : lowGlucose.asMgdL
             let highGlucose = units == .mgdL ? highGlucose : highGlucose.asMgdL
 
+            // 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 = Decimal(55)
+            let hardCodedHigh = Decimal(220)
+
             let pointMarkColor: Color = FreeAPS.getDynamicGlucoseColor(
                 glucoseValue: Decimal(item.glucose),
-                highGlucoseColorValue: highGlucose,
-                lowGlucoseColorValue: lowGlucose,
+                highGlucoseColorValue: hardCodedHigh,
+                lowGlucoseColorValue: hardCodedLow,
                 targetGlucose: currentGlucoseTarget,
-                glucoseColorScheme: glucoseColorScheme,
-                offset: 20
+                glucoseColorScheme: glucoseColorScheme
             )
 
             if !isSmoothingEnabled {

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

@@ -244,7 +244,6 @@ extension MainChartView {
 
     @ViewBuilder var selectionPopover: some View {
         if let sgv = selectedGlucose?.glucose {
-            let glucoseToShow = units == .mgdL ? Decimal(sgv) : Decimal(sgv).asMmolL
             VStack(alignment: .leading) {
                 HStack {
                     Image(systemName: "clock")
@@ -252,16 +251,19 @@ extension MainChartView {
                         .font(.body).bold()
                 }.font(.body).padding(.bottom, 5)
 
+                // 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 = Decimal(55)
+                let hardCodedHigh = Decimal(220)
+
                 let glucoseColor = FreeAPS.getDynamicGlucoseColor(
-                    glucoseValue: glucoseToShow,
-                    highGlucoseColorValue: highGlucose,
-                    lowGlucoseColorValue: lowGlucose,
-                    targetGlucose: currentGlucoseTarget,
-                    glucoseColorScheme: glucoseColorScheme,
-                    offset: units == .mgdL ? 20 : 20.asMmolL
+                    glucoseValue: Decimal(sgv),
+                    highGlucoseColorValue: hardCodedHigh,
+                    lowGlucoseColorValue: hardCodedLow,
+                    targetGlucose: units == .mgdL ? currentGlucoseTarget : currentGlucoseTarget.asMgdL,
+                    glucoseColorScheme: glucoseColorScheme
                 )
                 HStack {
-                    Text(units == .mgdL ? glucoseToShow.description : Decimal(sgv).formattedAsMmolL)
+                    Text(units == .mgdL ? Decimal(sgv).description : Decimal(sgv).formattedAsMmolL)
                         .bold()
                         + Text(" \(units.rawValue)")
                 }.foregroundStyle(

+ 7 - 26
FreeAPS/Sources/Modules/Home/View/Header/CurrentGlucoseView.swift

@@ -89,14 +89,17 @@ struct CurrentGlucoseView: View {
 
                             var glucoseDisplayColor = Color.primary
 
+                            // 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 = Decimal(55)
+                            let hardCodedHigh = Decimal(220)
+
                             if Decimal(glucoseValue) <= lowGlucose || Decimal(glucoseValue) >= highGlucose {
                                 glucoseDisplayColor = FreeAPS.getDynamicGlucoseColor(
                                     glucoseValue: Decimal(glucoseValue),
-                                    highGlucoseColorValue: highGlucose,
-                                    lowGlucoseColorValue: lowGlucose,
+                                    highGlucoseColorValue: hardCodedHigh,
+                                    lowGlucoseColorValue: hardCodedLow,
                                     targetGlucose: targetGlucose,
-                                    glucoseColorScheme: glucoseColorScheme,
-                                    offset: 20
+                                    glucoseColorScheme: glucoseColorScheme
                                 )
                             }
 
@@ -180,28 +183,6 @@ struct CurrentGlucoseView: View {
         let deltaAsDecimal = units == .mmolL ? Decimal(delta).asMmolL : Decimal(delta)
         return deltaFormatter.string(from: deltaAsDecimal as NSNumber) ?? "--"
     }
-
-//    var glucoseDisplayColor: Color {
-//        guard let lastGlucose = glucose.last?.glucose else { return .primary }
-//
-//        // low and high glucose is parsed in state to mmol/L; parse it back to mg/dl here for comparison
-//        let lowGlucose = units == .mgdL ? lowGlucose : lowGlucose.asMgdL
-//        let highGlucose = units == .mgdL ? highGlucose : highGlucose.asMgdL
-//
-//        // Ensure the thresholds are logical
-//        guard lowGlucose < highGlucose else { return .primary }
-//
-//        guard Decimal(lastGlucose) <= lowGlucose && Decimal(lastGlucose) >= highGlucose else { return .primary }
-//
-//        return FreeAPS.getDynamicGlucoseColor(
-//            glucoseValue: Decimal(lastGlucose),
-//            highGlucoseColorValue: highGlucose,
-//            lowGlucoseColorValue: lowGlucose,
-//            targetGlucose: currentGlucoseTarget,
-//            glucoseColorScheme: glucoseColorScheme,
-//            offset: 20
-//        )
-//    }
 }
 
 struct Triangle: Shape {

+ 6 - 7
FreeAPS/Sources/Modules/LiveActivitySettings/LiveActivitySettingsStateModel.swift

@@ -3,7 +3,6 @@ import SwiftUI
 
 extension LiveActivitySettings {
     final class StateModel: BaseStateModel<Provider> {
-        @Injected() var settings: SettingsManager!
         @Injected() var storage: FileStorage!
 
         @Published var units: GlucoseUnits = .mgdL
@@ -35,11 +34,11 @@ extension LiveActivitySettings {
 extension LiveActivitySettings.StateModel: SettingsObserver {
     func settingsDidChange(_: FreeAPSSettings) {
         units = settingsManager.settings.units
-//        showChart = settingsManager.settings.showChart
-//        showCurrentGlucose = settingsManager.settings.showCurrentGlucose
-//        showChangeLabel = settingsManager.settings.showChangeLabel
-//        showIOB = settingsManager.settings.showIOB
-//        showCOB = settingsManager.settings.showCOB
-//        showUpdatedLabel = settingsManager.settings.showUpdatedLabel
+        showChart = settingsManager.settings.showChart
+        showCurrentGlucose = settingsManager.settings.showCurrentGlucose
+        showChangeLabel = settingsManager.settings.showChangeLabel
+        showIOB = settingsManager.settings.showIOB
+        showCOB = settingsManager.settings.showCOB
+        showUpdatedLabel = settingsManager.settings.showUpdatedLabel
     }
 }

+ 9 - 179
FreeAPS/Sources/Modules/LiveActivitySettings/View/LiveActivityWidgetConfiguration.swift

@@ -52,184 +52,6 @@ struct LiveActivityWidgetConfiguration: BaseView {
         return data
     }
 
-//    var body: some View {
-//            VStack {
-//                Group {
-//                    VStack(alignment: .leading, spacing: 0) {
-//                        Text("Live Activity Personalization".uppercased())
-//                            .frame(maxWidth: .infinity, alignment: .leading)
-//                            .foregroundColor(.secondary)
-//                            .font(.footnote)
-//                            .padding(.leading)
-//                    }
-//                    VStack {
-//                        Text(
-//                            "Trio offers you to customize your Live Activity lock screen widget. The default configuration will display current glucose, IOB, COB and the time of last algorithm run."
-//                        )
-//                        .padding()
-//                        .font(.footnote)
-//                        .foregroundColor(.secondary)
-//                    }
-//                    .background(
-//                        RoundedRectangle(cornerRadius: 10, style: .continuous)
-//                            .fill(Color.chart)
-//                    )
-//                }
-//
-//                GroupBox {
-//                    VStack {
-//                        dummyChart
-//
-//                        HStack {
-//                            if selectedItems.isEmpty {
-//                                Spacer()
-//                                Button(action: {
-//                                    showAddItemDialog.toggle()
-//                                }) {
-//                                    VStack {
-//                                        Image(systemName: "plus")
-//                                            .font(.title2)
-//                                            .foregroundColor(.gray)
-//                                    }
-//                                    .frame(width: 60, height: 40)
-//                                    .overlay(
-//                                        RoundedRectangle(cornerRadius: 12)
-//                                            .stroke(style: StrokeStyle(lineWidth: 2, dash: [5]))
-//                                            .foregroundColor(.gray)
-//                                    )
-//                                }
-//                                Spacer()
-//                            } else {
-//                                ForEach(Array(selectedItems.enumerated()), id: \.element) { index, item in
-//                                    if index > 0 {
-//                                        Divider()
-//                                            .frame(height: 50)
-//                                    }
-//
-//                                    ZStack(alignment: .topTrailing) {
-//                                        getItemPreview(for: item)
-//                                            .frame(width: 50, height: 50)
-//                                            .padding(5)
-//                                            .background(
-//                                                draggingItem == item ? Color.blue.opacity(0.2) : Color.clear
-//                                            )
-//                                            .cornerRadius(12)
-//                                            .overlay(
-//                                                RoundedRectangle(cornerRadius: 12)
-//                                                    .stroke(
-//                                                        draggingItem == item ? Color.blue : Color.primary,
-//                                                        lineWidth: draggingItem == item ? 2 : 1
-//                                                    )
-//                                            )
-//                                            .opacity(draggingItem == item ? 0.85 : 1.0)
-//                                            .onDrag {
-//                                                self.draggingItem = item
-//                                                return NSItemProvider(object: item.rawValue as NSString)
-//                                            }
-//                                            .onDrop(
-//                                                of: [UTType.text],
-//                                                delegate: DropViewDelegate(
-//                                                    item: item,
-//                                                    items: $selectedItems,
-//                                                    draggingItem: $draggingItem
-//                                                )
-//                                            )
-//                                            .disabled(!isEditMode)
-//                                            .rotationEffect(.degrees(isEditMode ? 2.5 : 0))
-//                                            .rotation3DEffect(.degrees(isEditMode ? 2.5 : 0), axis: (x: 0, y: -5, z: 0))
-//                                            .animation(
-//                                                isEditMode ? Animation.easeInOut(duration: 0.15)
-//                                                    .repeatForever(autoreverses: true) : .default,
-//                                                value: isEditMode
-//                                            )
-//                                        if isEditMode {
-//                                            Button(action: {
-//                                                showDeleteAlert = true
-//                                            }) {
-//                                                Image(systemName: "minus.circle.fill")
-//                                                    .foregroundColor(Color(UIColor.systemGray2)) // Opaque foreground color
-//                                                    .background(Color.white) // Adding a background for contrast
-//                                                    .clipShape(Circle()) // Make sure the background stays circular
-//                                                    .font(.system(size: 20))
-//                                            }
-//                                            .offset(x: -45, y: -10)
-//                                            .alert(isPresented: $showDeleteAlert) {
-//                                                Alert(
-//                                                    title: Text("Delete Widget"),
-//                                                    message: Text("Are you sure you want to delete this widget?"),
-//                                                    primaryButton: .destructive(Text("Delete")) {
-//                                                        removeItem(item)
-//                                                    },
-//                                                    secondaryButton: .cancel()
-//                                                )
-//                                            }
-//                                        }
-//                                    }
-//                                    .animation(.easeInOut, value: draggingItem)
-//                                    .frame(maxWidth: .infinity)
-//                                }
-//                            }
-//                        }
-//                        .padding()
-//                        .overlay(
-//                            RoundedRectangle(cornerRadius: 12)
-//                                .stroke(style: StrokeStyle(lineWidth: 2, dash: [5]))
-//                                .foregroundColor(.gray)
-//                        )
-//                        .cornerRadius(12)
-//                    }
-//                }.padding(.vertical).groupBoxStyle(.dummyChart)
-//
-//                if isEditMode {
-//                    HStack {
-//                        Image(systemName: "hand.draw.fill")
-//                        Text("Tap 'Add +' to add a widget. Press, drag and drop a widget to re-order a widget.")
-//                    }.frame(maxWidth: .infinity, alignment: .leading)
-//                        .foregroundColor(.secondary)
-//                        .font(.footnote)
-//                        .padding(.horizontal)
-//                }
-//
-//                Spacer()
-//            }
-//            .padding()
-//            .scrollContentBackground(.hidden).background(color)
-//            .navigationTitle("Widget Configuration")
-//            .navigationBarTitleDisplayMode(.automatic)
-//            .toolbar {
-//                ToolbarItem(placement: .topBarTrailing) {
-//                    Button {
-//                        isEditMode.toggle()
-//                    } label: {
-//                        HStack {
-//                            Text("Edit")
-//                        }
-//                    }
-//                }
-//                ToolbarItem(placement: .topBarTrailing) {
-//                    Button {
-//                        showAddItemDialog.toggle()
-//                    } label: {
-//                        HStack {
-//                            Text("Add")
-//                            Image(systemName: "plus")
-//                        }
-//                    }
-//                }
-//            }
-//            .confirmationDialog("Add Widget", isPresented: $showAddItemDialog, titleVisibility: .visible) {
-//                ForEach(LiveActivityItem.allCases, id: \.self) { item in
-//                    Button(item.displayName) {
-//                        addItem(item)
-//                    }.disabled(selectedItems.contains(item))
-//                }
-//            }
-//            .onAppear {
-//                configureView()
-//                loadOrder()
-//            }
-//        }
-
     var body: some View {
         VStack {
             Group {
@@ -408,10 +230,18 @@ struct LiveActivityWidgetConfiguration: BaseView {
     private var dummyChart: some View {
         Chart {
             ForEach(glucoseData) { data in
+                let pointMarkColor = FreeAPS.getDynamicGlucoseColor(
+                    glucoseValue: Decimal(data.glucoseLevel),
+                    highGlucoseColorValue: state.settingsManager.settings.highGlucose,
+                    lowGlucoseColorValue: state.settingsManager.settings.lowGlucose,
+                    targetGlucose: state.units == .mgdL ? Decimal(100) : 100.asMmolL,
+                    glucoseColorScheme: state.settingsManager.settings.glucoseColorScheme
+                )
+
                 PointMark(
                     x: .value("Time", data.time),
                     y: .value("Glucose Level", data.glucoseLevel)
-                ).foregroundStyle(.green.gradient).symbolSize(15)
+                ).foregroundStyle(pointMarkColor).symbolSize(15)
             }
         }
         .chartPlotStyle { plotContent in

+ 29 - 27
LiveActivity/LiveActivity.swift

@@ -70,28 +70,22 @@ extension Color {
         highGlucoseColorValue: Decimal,
         lowGlucoseColorValue: Decimal,
         targetGlucose: Decimal,
-        glucoseColorScheme: String,
-        offset: Decimal
+        glucoseColorScheme: String
     ) -> Color {
-        // Convert Decimal to Int for high and low glucose values
-        let lowGlucose = glucoseColorScheme == "dynamicColor" ? (lowGlucoseColorValue - offset) : lowGlucoseColorValue
-        let highGlucose = glucoseColorScheme == "dynamicColor" ? (highGlucoseColorValue + (offset * 1.75)) : highGlucoseColorValue
-        let targetGlucose = targetGlucose
-
         // Only use calculateHueBasedGlucoseColor if the setting is enabled in preferences
         if glucoseColorScheme == "dynamicColor" {
             return calculateHueBasedGlucoseColor(
                 glucoseValue: glucoseValue,
-                highGlucose: highGlucose,
-                lowGlucose: lowGlucose,
+                highGlucose: highGlucoseColorValue,
+                lowGlucose: lowGlucoseColorValue,
                 targetGlucose: targetGlucose
             )
         }
         // Otheriwse, use static (orange = high, red = low, green = range)
         else {
-            if glucoseValue > highGlucose {
+            if glucoseValue > highGlucoseColorValue {
                 return Color.orange
-            } else if glucoseValue < lowGlucose {
+            } else if glucoseValue < lowGlucoseColorValue {
                 return Color.red
             } else {
                 return Color.green
@@ -145,13 +139,16 @@ struct LiveActivity: Widget {
                 let detailedState = state.detailedViewState
                 let isMgdL = detailedState?.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
+                let hardCodedHigh = isMgdL ? Decimal(220) : 220.asMmolL
+
                 return Color.getDynamicGlucoseColor(
                     glucoseValue: Decimal(string: state.bg) ?? 100,
-                    highGlucoseColorValue: isMgdL ? state.highGlucose : context.state.highGlucose.asMmolL,
-                    lowGlucoseColorValue: isMgdL ? state.lowGlucose : state.lowGlucose.asMmolL,
+                    highGlucoseColorValue: hardCodedHigh,
+                    lowGlucoseColorValue: hardCodedLow,
                     targetGlucose: isMgdL ? state.target : state.target.asMmolL,
-                    glucoseColorScheme: state.glucoseColorScheme,
-                    offset: isMgdL ? 20 : 20.asMmolL
+                    glucoseColorScheme: state.glucoseColorScheme
                 )
             }
 
@@ -198,13 +195,16 @@ struct LiveActivityView: View {
         let detailedState = state.detailedViewState
         let isMgdL = detailedState?.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
+        let hardCodedHigh = isMgdL ? Decimal(220) : 220.asMmolL
+
         return Color.getDynamicGlucoseColor(
             glucoseValue: Decimal(string: state.bg) ?? 100,
-            highGlucoseColorValue: isMgdL ? state.highGlucose : context.state.highGlucose.asMmolL,
-            lowGlucoseColorValue: isMgdL ? state.lowGlucose : state.lowGlucose.asMmolL,
+            highGlucoseColorValue: hardCodedHigh,
+            lowGlucoseColorValue: hardCodedLow,
             targetGlucose: isMgdL ? state.target : state.target.asMmolL,
-            glucoseColorScheme: state.glucoseColorScheme,
-            offset: isMgdL ? 20 : 20.asMmolL
+            glucoseColorScheme: state.glucoseColorScheme
         )
     }
 
@@ -464,8 +464,7 @@ struct LiveActivityChartView: View {
             highGlucoseColorValue: yAxisRuleMarkMax,
             lowGlucoseColorValue: yAxisRuleMarkMin,
             targetGlucose: isMgdl ? state.target : state.target.asMmolL,
-            glucoseColorScheme: context.state.glucoseColorScheme,
-            offset: isMgdl ? Decimal(20) : Decimal(20).asMmolL
+            glucoseColorScheme: context.state.glucoseColorScheme
         )
 
         let lowColor = Color.getDynamicGlucoseColor(
@@ -473,8 +472,7 @@ struct LiveActivityChartView: View {
             highGlucoseColorValue: yAxisRuleMarkMax,
             lowGlucoseColorValue: yAxisRuleMarkMin,
             targetGlucose: isMgdl ? state.target : state.target.asMmolL,
-            glucoseColorScheme: context.state.glucoseColorScheme,
-            offset: isMgdl ? Decimal(20) : Decimal(20).asMmolL
+            glucoseColorScheme: context.state.glucoseColorScheme
         )
 
         Chart {
@@ -544,13 +542,17 @@ struct LiveActivityChartView: View {
             let currentValue = additionalState.chart[index]
             let displayValue = isMgdL ? currentValue : currentValue.asMmolL
             let chartDate = additionalState.chartDate[index] ?? Date()
+
+            // 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 = Decimal(55)
+            let hardCodedHigh = Decimal(220)
+
             let pointMarkColor = Color.getDynamicGlucoseColor(
                 glucoseValue: currentValue,
-                highGlucoseColorValue: context.state.highGlucose,
-                lowGlucoseColorValue: context.state.lowGlucose,
+                highGlucoseColorValue: hardCodedHigh,
+                lowGlucoseColorValue: hardCodedLow,
                 targetGlucose: context.state.target,
-                glucoseColorScheme: context.state.glucoseColorScheme,
-                offset: 20
+                glucoseColorScheme: context.state.glucoseColorScheme
             )
 
             let pointMark = PointMark(