Modify live activity by marionbarker · Pull Request #2397 · LoopKit/Loop

I believe the code introduced in 101cde2 does not work fully as intended. This can lead to issues such as the lowest portion of the predictive line always being coloured red, even though the user is within the ranges defined in the Live Activity "Display control options".

See examples here: #2392 (comment)

I tried looking at the changes in the code and observed the following. I am a complete amateur at Swift however so I may be interpreting the code wrong. So everything below is just my assumption of how the code works by inspecting it. Please correct me if this is wrong.

In getGradient, lowerStop and upperStop are normalised based on the highest value in the predicted line.
From Loop Widget Extension/Live Activity/ChartView.swift:

private static func getGradient(useLimits: Bool, lowerLimit: Double, upperLimit: Double, highestValue: Double) -> LinearGradient {
    var stops: [Gradient.Stop] = [Gradient.Stop(color: Color("glucose"), location: 0)]
    if useLimits {
        let lowerStop = lowerLimit / highestValue // Normalisation of values
        let upperStop = upperLimit / highestValue // Normalisation of values
        stops = [
            Gradient.Stop(color: .red, location: 0),
            Gradient.Stop(color: .red, location: lowerStop - 0.01),
            Gradient.Stop(color: .green, location: lowerStop),
            Gradient.Stop(color: .green, location: upperStop),
            Gradient.Stop(color: .orange, location: upperStop + 0.01),
            Gradient.Stop(color: .orange, location: 600), // Just use the mg/dl limit for the most upper value
        ]
   }
   return LinearGradient(
        gradient: Gradient(stops: stops),
        startPoint: .bottom,
        endPoint: .top
   )
}

So the Gradient.Stop points are assigned based on normalised glucose levels.

However, when applying the gradient in the following piece of code, the values passed are not normalised. This results in the gradient being applied in the wrong way.

From Loop Widget Extension/Live Activity/ChartView.swift:

ForEach(predicatedData) { item in
    LineMark (x: .value("Date", item.x),
                     y: .value("Glucose level", item.y) // NOT normalised
    )
    .lineStyle(StrokeStyle(lineWidth: 2, dash: [6, 5]))
    .foregroundStyle(colorGradient)
}

By mixing normalised and not normalised values, the line is coloured in the wrong way.

Again, this is just my hypothesis based on reading the code so I may be understanding it wrong.