Skip to content

Fix range and mmol/L colors in chart and Live Activity - #777

Open
MtlPhil wants to merge 7 commits into
loopandlearn:devfrom
achkars-org:fix/la-custom-range-updates
Open

MtlPhil wants to merge 7 commits into
loopandlearn:devfrom
achkars-org:fix/la-custom-range-updates

Conversation

@MtlPhil

@MtlPhil MtlPhil commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

What this fixes

The app decided "low / in range / high" in several places, each with its own comparison. With custom range lines and mmol/L they could disagree with each other and with the number on screen: two readings that both display "3.9" could get different colors, and the Live Activity and stats pie could be stale after a range change.

Changes

  • One shared rule. GlucoseBand.of(_:low:high:mmol:) in Shared/ classifies a reading at the precision shown to the user. A reading on a line is in range. The chart, BG text, chart scrubbing, contact image, stats pie, Live Activity widget, and the watch app and complications all use it (through UnitSettingsStore.glucoseBand(for:thresholds:) in the app), replacing the repeated compare-and-pick-a-color blocks.
  • Boundary rule: a reading on a line is in range. Whether a reading exactly on the low or high line is in range or out of range is discussed in Treatment of "on the line" readings (in range vs out of range) #779, which would benefit from community resolution. This PR implements the inclusive rule, which matches the international consensus on time in range (70–180 mg/dL, 3.9–10.0 mmol/L) and the TIR screen. It differs from the rule the colors use today, where a reading on a line counts as low or high. In mmol/L, readings that display 3.9 or 10.0 are in range.
  • Compared at the displayed value. Readings and lines are rounded to the precision shown before comparing (GlucoseConversion.toDisplayPrecision): one decimal in mmol/L, whole numbers in mg/dL. Readings that display the same value always get the same band, and a line picked in mmol/L (3.9 is stored as 70.26) behaves in mg/dL like the 70 it displays.
  • Custom line changes refresh the Live Activity through refreshFromCurrentState, the same path as a range-mode change, so the dismissed/enabled checks apply and a burst of picker changes collapses into one update (it lands after the existing 20 s refresh delay). Thresholds are published through writeThresholds() so the effective range is always used.
  • Stats pie refreshes when the range lines or range mode change, and uses the same rule as the colors.
  • TIR screen and GRI keep their inclusive bands and published cutoffs, and now compare at display precision too, so in mmol/L 180 and 181 mg/dL (both shown 10.0) land in the same band.
  • Contact image uses effectiveThresholds(), so TIR and TITR modes match the chart, BG text and Live Activity.

Alarms

The alarm-threshold change and the BGPicker conversion factor were in this PR as a separate commit. As suggested in review, they are now their own PR, #839, which builds on this one. This PR is limited to colors and stats.

@MtlPhil
MtlPhil marked this pull request as ready for review October 12, 2026 16:37
@MtlPhil
MtlPhil marked this pull request as draft October 12, 2026 17:46
@MtlPhil
MtlPhil marked this pull request as ready for review October 12, 2026 18:49
- Push an immediate Live Activity update when the custom low/high lines
  change. TIR/TITR use fixed ranges, so this only applies in custom mode.
  Thresholds are published through writeThresholds() so the effective
  range is always used.
- Refresh the main stats pie when the range lines or range mode change.
- Compare reading colors at mmol/L display precision, so two readings that
  both display as "3.9" get the same color. Applies to the chart, BG text,
  contact image and the Live Activity widget. mg/dL is unchanged.
- Share the rounding via GlucoseConversion.toDisplayedMmol.
…ndary

- Route custom low/high line changes through refreshFromCurrentState
  instead of a separate hand-built Live Activity push, so the dismissed,
  enabled and in-flight checks apply and picker bursts are debounced.
- Use effectiveThresholds() for the contact image so TIR and TITR modes
  match the chart, BG text and Live Activity.
- Add GlucoseBand.of(_:low:high:mmol:) in Shared and use it for the chart,
  BG text, contact image, stats pie and the Live Activity widget. A
  reading on a line is low or high, compared at display precision.
- Make the stats pie follow the same boundary rule.
- Rename normalizeForColorComparison to normalizeToDisplayPrecision and
  shorten its comment.
@MtlPhil
MtlPhil force-pushed the fix/la-custom-range-updates branch from c671619 to 1ba4700 Compare November 3, 2026 16:33
@marionbarker

Copy link
Copy Markdown
Collaborator

I built the branch locally without problems.
A few items are provided from a Claude review:

Should fix in this PR

  1. Watch app and complications still use the old rule. WatchFormat.bgColor (WatchShared/WatchFormat.swift:98) still treats a reading on a line as low or high (>= high / <= low) and compares raw mg/dL. It colors the glucose page, the watch chart, the chart page and both complication views. With this PR, a reading of 70 or 180 mg/dL is green on the phone and Live Activity but red or yellow on the watch. GlucoseConversion is already compiled into the watch targets, so GlucoseBand.of(..., mmol: unit == .mmol) should work there directly.
  2. RetroSnapshot wasn't converted. LoopFollow/InfoTable/RetroSnapshot.swift:104-112 still has its own copy of the old comparison. When you scrub back on the chart, the BG text color at a boundary disagrees with the chart dots.

Suggestions

  1. In mg/dL there's no rounding, so a fractional line breaks "on the line is in range". Lines set in mmol/L are stored as fractions (3.9 becomes 70.26 with the new factor, 70.2 with ×18). After the user switches to mg/dL, the line shows "70" but a reading of 70 is classified low. Returning mgdl.rounded() in the mg/dL branch of normalizeToDisplayPrecision and GlucoseBand.of would make the rule hold in both units, and alarm thresholds would get the same fix.
  2. Duplicate refresh at launch. @Published sends its current value on subscribe, so the new lowLine/highLine subscriber in LiveActivityManager.init runs refreshFromCurrentState twice at startup, and each run also asks the watch to update. Adding .dropFirst() to each publisher would keep only real changes.

For maintainers to decide

  1. The inclusive boundary changes behavior for mg/dL users as well (70 and 180 turn green). With the Low alarm still firing at or below its threshold, someone whose alarm equals their low line will see a green 70 while the alarm sounds. This seems fine if Treatment of "on the line" readings (in range vs out of range) #779 settles on the inclusive rule.
  2. TIR and GRI now depend on the display unit. For example, a reading of 181 mg/dL is in range in mmol/L but high in mg/dL. That fits the 3.9–10.0 consensus for TIR. GRI has published mg/dL cutoffs, though, and may be worth keeping independent of the unit.
  3. The alarm commit is correct, and MissedBolusCondition is covered through passesBGLimits. Still, I'd support splitting it into its own PR, as you offered, so this one stays limited to colors and stats.

Replace the function-reference/closure ternary with plain per-value
conversions so the compiler can type-check it.
A reading exactly on the low or high line is now in range, matching the
international consensus on time in range (70-180 mg/dL, 3.9-10.0 mmol/L).
GlucoseBand, and so the chart, BG text, contact image, stats pie and
Live Activity widget, use the inclusive rule at display precision.

The TIR screen and GRI keep their inclusive bands and published cutoffs
but now compare at display precision, so readings that display the same
mmol/L value share a band. Alarm thresholds are unchanged.
Address review on the range color change:

- Watch app and complications color readings with GlucoseBand at the
  displayed unit's precision, so a reading on a line is green there too.
- RetroSnapshot uses the shared band, so the scrubbed BG text matches
  the chart dots.
- mg/dL values are rounded to whole numbers before comparing, so a line
  picked in mmol/L (stored as 70.26) behaves like the 70 it displays.
  TIR and GRI get the same rounding.
- The Live Activity threshold subscriber skips the initial value, so
  launch no longer refreshes twice.
@MtlPhil

MtlPhil commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. All four fixes are pushed:

  1. The watch app and complications now use GlucoseBand.of with the snapshot's unit, so 70 and 180 mg/dL are green there as well.
  2. RetroSnapshot uses the shared band, so the scrubbed BG text matches the chart dots.
  3. Values in mg/dL are now rounded to whole numbers before comparing (GlucoseConversion.toDisplayPrecision), so a line picked as 3.9 mmol/L behaves like the 70 it shows. I've added tests for this.
  4. I added .dropFirst() to both threshold publishers, so launch refreshes only once.

On 5, I'm keeping the inclusive rule, as you suggested, pending #779. The case where the Low alarm equals the low line is on purpose: an alarm threshold is a trigger (it fires at or below its value), while a range line is a boundary, so a green 70 can sound the alarm. If maintainers would rather the two agree, I can change it in #839.

On 6, I kept GRI in the user's units, since the GRI paper defines its zones in both (3.9–10.0 mmol/L, 70–180 mg/dL for in range). The exception is 181 mg/dL: at 10.047 mmol/L it shows as 10.0, so it counts as in range in mmol/L. Comparing at the precision the user sees keeps the stats consistent with the number on screen. If maintainers prefer the mg/dL cutoffs for everyone, it's a small change.

On 7, I've moved the alarm commit and the picker factor into #839, which builds on this PR. This one now covers only colors and stats.

@MtlPhil

MtlPhil commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Updated with the latest dev (088f0ba); no conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants