Explorar o código

fix conversion in mmol/L for y axis labels in live activity charts

improve readability for lockscreen widget in light mode
polscm32 %!s(int64=2) %!d(string=hai) anos
pai
achega
24b35a4c23

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

@@ -7,7 +7,7 @@ struct LiveActivityAttributes: ActivityAttributes {
         let trendSystemImage: String?
         let trendSystemImage: String?
         let change: String
         let change: String
         let date: Date
         let date: Date
-        let chart: [Int16]
+        let chart: [Double]
         let chartDate: [Date?]
         let chartDate: [Date?]
         let rotationDegrees: Double
         let rotationDegrees: Double
     }
     }

+ 9 - 7
FreeAPS/Sources/Services/LiveActivity/LiveActivityBridge.swift

@@ -21,7 +21,7 @@ extension LiveActivityAttributes.ContentState {
             .string(from: mmol ? value.asMmolL as NSNumber : NSNumber(value: value))!
             .string(from: mmol ? value.asMmolL as NSNumber : NSNumber(value: value))!
     }
     }
 
 
-    init?(new bg: BloodGlucose, prev: BloodGlucose?, mmol: Bool, chart: [Readings]) {
+    init?(new bg: BloodGlucose, prev: BloodGlucose?, mmol: Bool, chart: [Readings], settings: FreeAPSSettings) {
         guard let glucose = bg.glucose,
         guard let glucose = bg.glucose,
               bg.dateString.timeIntervalSinceNow > -TimeInterval(minutes: 6)
               bg.dateString.timeIntervalSinceNow > -TimeInterval(minutes: 6)
         else {
         else {
@@ -71,6 +71,9 @@ extension LiveActivityAttributes.ContentState {
 
 
         let chartBG = chart.map(\.glucose)
         let chartBG = chart.map(\.glucose)
 
 
+        let conversionFactor: Double = settings.units == .mmolL ? 18.0 : 1.0
+        let convertedChartBG = chartBG.map { Double($0) / conversionFactor }
+
         let chartDate = chart.map(\.date)
         let chartDate = chart.map(\.date)
 
 
         self.init(
         self.init(
@@ -78,7 +81,7 @@ extension LiveActivityAttributes.ContentState {
             trendSystemImage: trendString,
             trendSystemImage: trendString,
             change: change,
             change: change,
             date: bg.dateString,
             date: bg.dateString,
-            chart: chartBG,
+            chart: convertedChartBG,
             chartDate: chartDate, rotationDegrees: rotationDegrees
             chartDate: chartDate, rotationDegrees: rotationDegrees
         )
         )
     }
     }
@@ -211,11 +214,8 @@ extension LiveActivityBridge: GlucoseObserver {
         defer {
         defer {
             self.latestGlucose = glucose.last
             self.latestGlucose = glucose.last
         }
         }
-
-//        let last72Glucose = Array(glucose.dropLast().suffix(72))
+//        fetch glucose for chart from Core Data
         let coreDataStorage = CoreDataStorage()
         let coreDataStorage = CoreDataStorage()
-
-//        let fetchGlucose = coreDataStorage.fetchGlucose(interval: DateFilter().day)
         let sixHoursAgo = Calendar.current.date(byAdding: .hour, value: -6, to: Date()) ?? Date()
         let sixHoursAgo = Calendar.current.date(byAdding: .hour, value: -6, to: Date()) ?? Date()
         let fetchGlucose = coreDataStorage.fetchGlucose(interval: sixHoursAgo as NSDate)
         let fetchGlucose = coreDataStorage.fetchGlucose(interval: sixHoursAgo as NSDate)
 
 
@@ -223,7 +223,9 @@ extension LiveActivityBridge: GlucoseObserver {
             new: bg,
             new: bg,
             prev: latestGlucose,
             prev: latestGlucose,
             mmol: settings.units == .mmolL,
             mmol: settings.units == .mmolL,
-            chart: fetchGlucose
+
+            chart: fetchGlucose, settings: settings
+
         ) else {
         ) else {
             // no bg or value stale. Don't update the activity if there already is one, just let it turn stale so that it can still be used once current bg is available again
             // no bg or value stale. Don't update the activity if there already is one, just let it turn stale so that it can still be used once current bg is available again
             return
             return

+ 11 - 2
LiveActivity/LiveActivity.swift

@@ -84,7 +84,16 @@ struct LiveActivity: Widget {
                 plotContent.background(.cyan.opacity(0.1))
                 plotContent.background(.cyan.opacity(0.1))
             }
             }
             .chartYAxis {
             .chartYAxis {
-                AxisMarks(position: .leading)
+                AxisMarks(position: .leading) { _ in
+                    AxisValueLabel().foregroundStyle(Color.white)
+                    AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3])).foregroundStyle(Color.white)
+                }
+            }
+            .chartXAxis {
+                AxisMarks(position: .automatic) { _ in
+                    AxisValueLabel().foregroundStyle(Color.white)
+                    AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3])).foregroundStyle(Color.white)
+                }
             }
             }
         }
         }
     }
     }
@@ -118,7 +127,7 @@ struct LiveActivity: Widget {
             .background(Color.white.opacity(0.2))
             .background(Color.white.opacity(0.2))
             .foregroundColor(Color.white)
             .foregroundColor(Color.white)
             .activityBackgroundTint(Color.black.opacity(0.7))
             .activityBackgroundTint(Color.black.opacity(0.7))
-            .activitySystemActionForegroundColor(Color.black)
+            .activitySystemActionForegroundColor(Color.white)
 
 
         } dynamicIsland: { context in
         } dynamicIsland: { context in
             DynamicIsland {
             DynamicIsland {

+ 4 - 6
LiveActivity/WidgetBobble.swift

@@ -40,7 +40,7 @@ struct TriangleShapeWidget: View {
             .fill(color)
             .fill(color)
             .frame(width: 35, height: 35)
             .frame(width: 35, height: 35)
             .rotationEffect(.degrees(90))
             .rotationEffect(.degrees(90))
-            .offset(x: 78)
+            .offset(x: 88)
     }
     }
 }
 }
 
 
@@ -48,11 +48,9 @@ struct TriangleWidget: Shape {
     func path(in rect: CGRect) -> Path {
     func path(in rect: CGRect) -> Path {
         var path = Path()
         var path = Path()
 
 
-        let cornerRadius: CGFloat = 2
-
-        path.move(to: CGPoint(x: rect.midX, y: rect.minY))
-        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius))
-        path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY - cornerRadius), control: CGPoint(x: rect.midX, y: rect.maxY))
+        path.move(to: CGPoint(x: rect.midX, y: rect.minY + 15))
+        path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
+        path.addQuadCurve(to: CGPoint(x: rect.minX, y: rect.maxY), control: CGPoint(x: rect.midX, y: rect.midY + 10))
         path.closeSubpath()
         path.closeSubpath()
 
 
         return path
         return path