Accurate distances toggle, Approx. distance markers, and Closest-mode speedup

Distance confidence
- Every distance surface takes the figure AND its "Approx." marker from one
  FuelStore.stationDistanceDisplay call (station rows, detail sheet, widget,
  Live Activity, watch, Siri), so a surface can never mark one figure approximate
  while showing a routed one. Radius filters and Closest ordering test that same
  displayed distance, so no row can read a distance outside the radius that
  filtered it (the reported "14.6 mi inside a 10-mile list").

Accurate distances (Settings -> Distances, default off)
- Off: the browse layer serves the straight line, marked Approx., for every row,
  so two installs agree to the metre and the app makes no route requests at all.
- On: routed distances for the whole search area, unmarked, served only from a
  COMPLETE set anchored within 600 m — a partial fill reverts every row at once
  rather than mixing road and straight-line bases.
- Best Value maths sits deliberately outside the toggle: it keeps using measured
  road routes and its own confidence marking, so money figures never move with
  the switch.
- Fills run only while the toggle is on and the app is frontmost; capacity raised
  to 600 stations per pass; completeness tolerates pins Apple Maps has no route
  for, which otherwise left an area permanently incomplete and the whole list
  stuck on estimates.

Performance (the reported slowdown switching to Closest / launching with it)
- FuelStore.accurateDistancesEnabled was a UserDefaults(suiteName:) read per
  call: a synchronous cfprefsd round-trip measured at ~1.9 us, ~200k calls per
  Closest sort. Memoised, with the setter keeping it in step.
- Both keyed sorts decorate their keys — one distance per station, carried as an
  index instead of copying whole stations into the decorated array — and the
  comparison baseline plus per-station journeys now come from ONE pass instead of
  two full scans per tab. The Stations tab also reuses its single sorted list for
  the count and the TOP badge instead of sorting the country three times.
- Measured on iPhone SE (7,914-station Closest pool): sort 36.6 ms -> 17.1 ms per
  list, Cheapest 6 ms, Best Value 11 ms.
- DEBUG probes: FB_PERF list timing, FB_PERF_BENCH toggle-read benchmark, and
  FB_DIST_FOCUS now reports the served value, marker and mode (and re-prints when
  a fill lands). FB_ACCURATE_DISTANCES / FB_SORT_MODE env forms for devices, where
  devicectl eats a leading-dash argument.

Tests: 198, 0 failures. Device and simulator Debug builds green.
This commit is contained in:
FuelBoard Contributor
2026-10-13 18:01:45 +01:00
parent e0a997ecb0
commit 2a2823c180
21 changed files with 1009 additions and 131 deletions
@@ -135,12 +135,21 @@ private struct FuelBoardLiveActivityView: View {
VStack(alignment: .leading, spacing: 2) {
Text("Cheapest \(context.state.fuel.displayName)")
.font(.headline)
Text("\(context.state.stationName) · \(context.state.distanceText)")
.font(.subheadline)
.foregroundStyle(.secondary)
.lineLimit(1)
.minimumScaleFactor(0.75)
.truncationMode(.tail)
// Name and distance are SEPARATE texts: the "Approx." marker
// lengthened this line, and as one tail-truncated string the
// FIGURE was what got cut ("… · Approx…"). The distance keeps its
// intrinsic width and the NAME truncates instead — the number is
// the informative part.
HStack(spacing: 4) {
Text(context.state.stationName)
.lineLimit(1)
.truncationMode(.tail)
Text("· \(context.state.distanceText)")
.lineLimit(1)
.layoutPriority(1)
}
.font(.subheadline)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity, alignment: .leading)
@@ -223,9 +232,17 @@ private struct FuelBoardLiveActivityStationView: View {
var body: some View {
Link(destination: FuelBoardLiveActivityTap.tapDestination(for: context.state)) {
Text("\(context.state.stationName) · \(context.state.distanceText)")
.font(.caption)
.lineLimit(1)
// Same split as the Lock Screen card: keep the distance and its
// "Approx." marker whole, truncate the name.
HStack(spacing: 4) {
Text(context.state.stationName)
.lineLimit(1)
.truncationMode(.tail)
Text("· \(context.state.distanceText)")
.lineLimit(1)
.layoutPriority(1)
}
.font(.caption)
}
}
}
@@ -248,9 +265,13 @@ private extension FuelBoardLiveActivityView {
// MARK: - ContentState display helpers
extension FuelBoardLiveActivityAttributes.ContentState {
/// Display distance using the user's chosen unit (miles default).
/// Display distance using the user's chosen unit (miles default), marked
/// "Approx." when the figure is the straight-line fallback rather than a
/// routed road distance — the same rule and formatter as the app's station
/// rows, so no surface can claim more confidence than it has.
var distanceText: String {
FuelStore.loadDistanceUnit().format(distanceKM)
StationDistanceDisplay(km: distanceKM, isApproximate: distanceIsApproximate ?? false)
.label(unit: FuelStore.loadDistanceUnit())
}
/// Price string from pence, in the user's chosen display style