Skip to content

Compare alarm thresholds at display precision - #839

Open
MtlPhil wants to merge 8 commits into
loopandlearn:devfrom
achkars-org:pr/alarm-display-precision
Open

MtlPhil wants to merge 8 commits into
loopandlearn:devfrom
achkars-org:pr/alarm-display-precision

Conversation

@MtlPhil

@MtlPhil MtlPhil commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

This builds on #777, so please merge that one first. Until then, this PR also shows #777's commits. Once #777 merges, only the alarm change is left: the last two commits (94608b0 and b6d660f), touching the three alarm conditions, BGPicker and BGDisplayPrecisionTests.

This change was split out of #777, as suggested in its review, so that #777 stays limited to colors and stats. It makes the BG alarms use the same rule as the colors: the reading and the threshold are both compared at the precision shown in the user's unit, so an alarm agrees with the number on screen.

Alarms

  • High, Low, activation limits and the Missed Bolus alarm (through passesBGLimits) round both the reading and the threshold to display precision before comparing: one decimal in mmol/L, whole numbers in mg/dL.
  • In mmol/L, a threshold of 10.0 fires for every reading that displays 10.0, whichever conversion factor stored it.
  • In mg/dL, a threshold picked in mmol/L (3.9 is stored as 70.2 or 70.26) fires at the whole number it displays, 70.
  • The alarm comparisons themselves are unchanged: Low still fires at or below its threshold, High at or above.

Picker

  • BGPicker converts mmol/L with GlucoseConversion.mmolToMgDl instead of a hardcoded 18. Saved values need no migration, because both sides are rounded before comparing.

Tests

  • BGDisplayPrecisionTests covers boundary readings in both units and with both conversion factors, and checks that every mmol/L picker value round-trips to the value picked.

- 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.
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.
- High, Low, activation limits and missed bolus alarms now round the
  reading and the threshold to the display precision of the current unit,
  so an alarm agrees with the number shown in mmol/L.
- BGPicker converts mmol/L to mg/dL with GlucoseConversion.mmolToMgDl
  instead of a hardcoded 18. Existing saved values need no migration
  because both sides are rounded before comparing.
- Add tests for boundary readings with both conversion factors.
A threshold picked in mmol/L is stored as fractional mg/dL (3.9 becomes
70.2 or 70.26). In mg/dL it now fires at the whole number it displays.
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.

1 participant