Explorar el Código

Dynamic BG color in graph views with this commit

- Set up in graph views
- Toggle is working from settings as wel
Auggie Fisher hace 1 año
padre
commit
a82dccd04e

+ 1 - 0
FreeAPS/Resources/json/defaults/freeaps/freeaps_settings.json

@@ -39,6 +39,7 @@
   "high" : 180,
   "low" : 70,
   "hours" : 6,
+  "dynamicBGColor" : false,
   "xGridLines" : true,
   "yGridLines" : true,
   "oneDimensionalGraph" : false,

+ 17 - 4
FreeAPS/Sources/Helpers/BGColor.swift

@@ -1,11 +1,24 @@
+import CoreData
+import Foundation
 import SwiftUI
 
 // Helper function to decide how to pick the BG color
-public func setBGColor(bgValue: Int, lowGlucose: Int, highGlucose: Int, targetGlucose: Int) -> Color {
+public func setBGColor(bgValue: Int, highBGColorValue: Decimal, lowBGColorValue: Decimal, dynamicBGColor: Bool) -> Color {
+    // Auggie - injected fails here
+    // Convert Decimal to Int for high and low glucose values
+    let lowGlucose = NSDecimalNumber(decimal: lowBGColorValue).intValue - 20
+    let highGlucose = NSDecimalNumber(decimal: highBGColorValue).intValue + 20
+    let targetGlucose = 90
+
     // TODO:
     // Only use setDynamicBGColor if the setting is enabled in preferences
-    if true {
-        return setDynamicBGColor(bgValue: bgValue, lowGlucose: lowGlucose, highGlucose: highGlucose, targetGlucose: targetGlucose)
+    if dynamicBGColor {
+        return setDynamicBGColor(
+            bgValue: bgValue,
+            highGlucose: Int(highGlucose),
+            lowGlucose: Int(lowGlucose),
+            targetGlucose: targetGlucose
+        )
     }
     // Otheriwse, use static (orange = high, red = low, green = range)
     else {
@@ -23,7 +36,7 @@ public func setBGColor(bgValue: Int, lowGlucose: Int, highGlucose: Int, targetGl
 // We'll shift color gradually one BG point at a time
 // We'll shift through the rainbow colors of ROY-G-BIV from low to high
 // Start at red for lowGlucose, green for targetGlucose, and violet for highGlucose
-public func setDynamicBGColor(bgValue: Int, lowGlucose: Int, highGlucose: Int, targetGlucose: Int) -> Color {
+public func setDynamicBGColor(bgValue: Int, highGlucose: Int, lowGlucose: Int, targetGlucose: Int) -> Color {
     let redHue: CGFloat = 0.0 / 360.0 // 0 degrees
     let greenHue: CGFloat = 120.0 / 360.0 // 120 degrees
     let purpleHue: CGFloat = 270.0 / 360.0 // 270 degrees

+ 5 - 0
FreeAPS/Sources/Models/FreeAPSSettings.swift

@@ -55,6 +55,7 @@ struct FreeAPSSettings: JSON, Equatable {
     var high: Decimal = 180
     var low: Decimal = 70
     var hours: Int = 6
+    var dynamicBGColor: Bool = false
     var xGridLines: Bool = true
     var yGridLines: Bool = true
     var oneDimensionalGraph: Bool = false
@@ -265,6 +266,10 @@ extension FreeAPSSettings: Decodable {
             settings.hours = hours
         }
 
+        if let dynamicBGColor = try? container.decode(Bool.self, forKey: .dynamicBGColor) {
+            settings.dynamicBGColor = dynamicBGColor
+        }
+
         if let xGridLines = try? container.decode(Bool.self, forKey: .xGridLines) {
             settings.xGridLines = xGridLines
         }

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

@@ -49,6 +49,7 @@ extension Home {
         @Published var lowGlucose: Decimal = 4 / 0.0555
         @Published var highGlucose: Decimal = 10 / 0.0555
         @Published var overrideUnit: Bool = false
+        @Published var dynamicBGColor: Bool = false
         @Published var displayXgridLines: Bool = false
         @Published var displayYgridLines: Bool = false
         @Published var thresholdLines: Bool = false
@@ -123,6 +124,7 @@ extension Home {
             lowGlucose = settingsManager.settings.low
             highGlucose = settingsManager.settings.high
             overrideUnit = settingsManager.settings.overrideHbA1cUnit
+            dynamicBGColor = settingsManager.settings.dynamicBGColor
             displayXgridLines = settingsManager.settings.xGridLines
             displayYgridLines = settingsManager.settings.yGridLines
             thresholdLines = settingsManager.settings.rulerMarks
@@ -456,6 +458,7 @@ extension Home.StateModel:
         lowGlucose = settingsManager.settings.low
         highGlucose = settingsManager.settings.high
         overrideUnit = settingsManager.settings.overrideHbA1cUnit
+        dynamicBGColor = settingsManager.settings.dynamicBGColor
         displayXgridLines = settingsManager.settings.xGridLines
         displayYgridLines = settingsManager.settings.yGridLines
         thresholdLines = settingsManager.settings.rulerMarks

+ 4 - 5
FreeAPS/Sources/Modules/Home/View/Chart/MainChartView.swift

@@ -47,6 +47,7 @@ struct MainChartView: View {
     @Binding var highGlucose: Decimal
     @Binding var lowGlucose: Decimal
     @Binding var screenHours: Int16
+    @Binding var dynamicBGColor: Bool
     @Binding var displayXgridLines: Bool
     @Binding var displayYgridLines: Bool
     @Binding var thresholdLines: Bool
@@ -469,13 +470,11 @@ extension MainChartView {
     private func drawGlucose(dummy _: Bool) -> some ChartContent {
         ForEach(state.glucoseFromPersistence) { item in
             let glucoseLevel = Int(item.glucose)
-            let lowColorThreshold = Int(lowGlucose)
-            let highColorThreshold = Int(highGlucose)
             let color = setBGColor(
                 bgValue: glucoseLevel,
-                lowGlucose: lowColorThreshold,
-                highGlucose: highColorThreshold,
-                targetGlucose: 90 // Auggie TODO: get the target color from preferences
+                highBGColorValue: highGlucose,
+                lowBGColorValue: lowGlucose,
+                dynamicBGColor: dynamicBGColor
             )
 
             if smooth {

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

@@ -373,6 +373,7 @@ extension Home {
                     highGlucose: $state.highGlucose,
                     lowGlucose: $state.lowGlucose,
                     screenHours: $state.hours,
+                    dynamicBGColor: $state.dynamicBGColor,
                     displayXgridLines: $state.displayXgridLines,
                     displayYgridLines: $state.displayYgridLines,
                     thresholdLines: $state.thresholdLines,

+ 2 - 0
FreeAPS/Sources/Modules/StatConfig/StatConfigStateModel.swift

@@ -12,6 +12,7 @@ extension StatConfig {
         @Published var low: Decimal = 70
         @Published var high: Decimal = 180
         @Published var hours: Decimal = 6
+        @Published var dynamicBGColor = false
         @Published var xGridLines = false
         @Published var yGridLines: Bool = false
         @Published var oneDimensionalGraph = false
@@ -24,6 +25,7 @@ extension StatConfig {
             self.units = units
 
             subscribeSetting(\.overrideHbA1cUnit, on: $overrideHbA1cUnit) { overrideHbA1cUnit = $0 }
+            subscribeSetting(\.dynamicBGColor, on: $dynamicBGColor) { dynamicBGColor = $0 }
             subscribeSetting(\.xGridLines, on: $xGridLines) { xGridLines = $0 }
             subscribeSetting(\.yGridLines, on: $yGridLines) { yGridLines = $0 }
             subscribeSetting(\.rulerMarks, on: $rulerMarks) { rulerMarks = $0 }

+ 1 - 0
FreeAPS/Sources/Modules/StatConfig/View/StatConfigRootView.swift

@@ -45,6 +45,7 @@ extension StatConfig {
         var body: some View {
             Form {
                 Section {
+                    Toggle("Use Dynamic BG Color", isOn: $state.dynamicBGColor)
                     Toggle("Display Chart X - Grid lines", isOn: $state.xGridLines)
                     Toggle("Display Chart Y - Grid lines", isOn: $state.yGridLines)
                     Toggle("Display Chart Threshold lines for Low and High", isOn: $state.rulerMarks)