浏览代码

Live Activity Graph
* conditionally show glucose graph in different colors dependent of high and low glucose settings
* To Do: add setting for showing graph in lockscreen and/or just a minimalistic view

polscm32 2 年之前
父节点
当前提交
71aaa92799

+ 2 - 0
FreeAPS/Sources/Services/LiveActivity/LiveActitiyShared.swift

@@ -10,6 +10,8 @@ struct LiveActivityAttributes: ActivityAttributes {
         let chart: [Double]
         let chartDate: [Date?]
         let rotationDegrees: Double
+        let highGlucose: Double
+        let lowGlucose: Double
     }
 
     let startDate: Date

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

@@ -76,13 +76,20 @@ extension LiveActivityAttributes.ContentState {
 
         let chartDate = chart.map(\.date)
 
+        /// glucose limits from settings
+        let highGlucose = settings.highGlucose
+        let lowGlucose = settings.lowGlucose
+
         self.init(
             bg: formattedBG,
             trendSystemImage: trendString,
             change: change,
             date: bg.dateString,
             chart: convertedChartBG,
-            chartDate: chartDate, rotationDegrees: rotationDegrees
+            chartDate: chartDate,
+            rotationDegrees: rotationDegrees,
+            highGlucose: Double(highGlucose),
+            lowGlucose: Double(lowGlucose)
         )
     }
 }

+ 17 - 4
LiveActivity/LiveActivity.swift

@@ -75,10 +75,23 @@ struct LiveActivity: Widget {
         } else {
             Chart {
                 ForEach(context.state.chart.indices, id: \.self) { index in
-                    LineMark(
-                        x: .value("Time", context.state.chartDate[index] ?? Date()),
-                        y: .value("Value", context.state.chart[index])
-                    ).foregroundStyle(Color.green.gradient).symbolSize(12)
+                    let currentValue = context.state.chart[index]
+                    if currentValue > context.state.highGlucose {
+                        PointMark(
+                            x: .value("Time", context.state.chartDate[index] ?? Date()),
+                            y: .value("Value", currentValue)
+                        ).foregroundStyle(Color.orange.gradient).symbolSize(12)
+                    } else if currentValue < context.state.lowGlucose {
+                        PointMark(
+                            x: .value("Time", context.state.chartDate[index] ?? Date()),
+                            y: .value("Value", currentValue)
+                        ).foregroundStyle(Color.red.gradient).symbolSize(12)
+                    } else {
+                        PointMark(
+                            x: .value("Time", context.state.chartDate[index] ?? Date()),
+                            y: .value("Value", currentValue)
+                        ).foregroundStyle(Color.green.gradient).symbolSize(12)
+                    }
                 }
             }.chartPlotStyle { plotContent in
                 plotContent.background(.cyan.opacity(0.1))