Skip to content

Commit 101cde2

Browse files
committed
update glucose color for live activity; author: bastiaanv
1 parent f66d332 commit 101cde2

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

Loop Widget Extension/Live Activity/ChartView.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct ChartView: View {
1717
private let glucoseRanges: [GlucoseRangeValue]
1818
private let preset: Preset?
1919
private let yAxisMarks: [Double]
20+
private let colorGradient: LinearGradient
2021

2122
init(glucoseSamples: [GlucoseSampleAttributes], predicatedGlucose: [Double], predicatedStartDate: Date?, predicatedInterval: TimeInterval?, useLimits: Bool, lowerLimit: Double, upperLimit: Double, glucoseRanges: [GlucoseRangeValue], preset: Preset?, yAxisMarks: [Double]) {
2223
self.glucoseSampleData = ChartValues.convert(data: glucoseSamples, useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit)
@@ -28,6 +29,7 @@ struct ChartView: View {
2829
lowerLimit: lowerLimit,
2930
upperLimit: upperLimit
3031
)
32+
self.colorGradient = ChartView.getGradient(useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit, highestValue: yAxisMarks.max() ?? 1)
3133
self.preset = preset
3234
self.glucoseRanges = glucoseRanges
3335
self.yAxisMarks = yAxisMarks
@@ -39,6 +41,28 @@ struct ChartView: View {
3941
self.preset = preset
4042
self.glucoseRanges = glucoseRanges
4143
self.yAxisMarks = yAxisMarks
44+
self.colorGradient = ChartView.getGradient(useLimits: useLimits, lowerLimit: lowerLimit, upperLimit: upperLimit, highestValue: yAxisMarks.max() ?? 1)
45+
}
46+
47+
private static func getGradient(useLimits: Bool, lowerLimit: Double, upperLimit: Double, highestValue: Double) -> LinearGradient {
48+
var stops: [Gradient.Stop] = [Gradient.Stop(color: Color("glucose"), location: 0)]
49+
if useLimits {
50+
let lowerStop = lowerLimit / highestValue
51+
let upperStop = upperLimit / highestValue
52+
stops = [
53+
Gradient.Stop(color: .red, location: 0),
54+
Gradient.Stop(color: .red, location: lowerStop - 0.01),
55+
Gradient.Stop(color: .green, location: lowerStop),
56+
Gradient.Stop(color: .green, location: upperStop),
57+
Gradient.Stop(color: .orange, location: upperStop + 0.01),
58+
Gradient.Stop(color: .orange, location: 600), // Just use the mg/dl limit for the most upper value
59+
]
60+
}
61+
return LinearGradient(
62+
gradient: Gradient(stops: stops),
63+
startPoint: .bottom,
64+
endPoint: .top
65+
)
4266
}
4367

4468
var body: some View {
@@ -79,7 +103,7 @@ struct ChartView: View {
79103
y: .value("Glucose level", item.y)
80104
)
81105
.lineStyle(StrokeStyle(lineWidth: 2, dash: [6, 5]))
82-
.foregroundStyle(by: .value("Color", item.color))
106+
.foregroundStyle(colorGradient)
83107
}
84108
}
85109
.chartForegroundStyleScale([
@@ -144,7 +168,7 @@ struct ChartValues: Identifiable {
144168
return ChartValues(
145169
x: startDate.addingTimeInterval(interval * Double(index)),
146170
y: item,
147-
color: !useLimits ? "Default" : item < lowerLimit ? "Low" : item > upperLimit ? "High" : "Good"
171+
color: "Default" // Color is handled by the gradient
148172
)
149173
}
150174
}

0 commit comments

Comments
 (0)