From d73a64023f4260a39648b0934f8bede8f3556eb6 Mon Sep 17 00:00:00 2001 From: FuelBoard Contributor Date: Thu, 20 Aug 2026 12:56:16 +0100 Subject: [PATCH] road distance: trigger at launch + foreground, fall back to saved location Road-distance routing previously only fired once a fresh GPS fix arrived via the location hook/onChange. A returning user on a cold launch waited for that fix, and if onboarding/flag issues kept tracking from starting, distances stayed straight-line. Now: - refreshRoadDistancesIfNeeded() uses the last saved location when the live fix isn't set yet, so it can run immediately. - It's called explicitly on launch (already-onboarded branch), on foreground activation, and on the existing location paths. --- FuelBoard/ContentView.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/FuelBoard/ContentView.swift b/FuelBoard/ContentView.swift index cedabd0..1fca183 100644 --- a/FuelBoard/ContentView.swift +++ b/FuelBoard/ContentView.swift @@ -56,14 +56,16 @@ struct ContentView: View { /// Kicks off a (throttled) Apple-Maps road-distance recompute for the /// stations around the current fix. The app owns routing — the widget and - /// Live Activity only read the cached result. + /// Live Activity only read the cached result. Falls back to the last saved + /// location so it can run before the first fresh GPS fix arrives. private func refreshRoadDistancesIfNeeded() { - guard let location else { return } + let origin = location ?? FuelStore.loadLocation() + guard let origin else { return } Task { await RoadDistanceService.refreshIfNeeded( stations: stations, - lat: location.lat, - lng: location.lng + lat: origin.lat, + lng: origin.lng ) } } @@ -332,6 +334,10 @@ struct ContentView: View { fuel: alertsFuel, radiusKM: effectiveAlertsRadiusKM) monitor.setEnabled(alertsEnabled) updateLiveActivity() + // Compute road distances early (throttled; falls back to the + // last saved location) so distance surfaces are road-matched + // as soon as stations are available. + refreshRoadDistancesIfNeeded() // Refresh only when the cache is stale (twice-a-day policy). // Skipped under the force-* hooks so the banner stays up. #if DEBUG @@ -377,6 +383,7 @@ struct ContentView: View { monitor.update(stations: stations, favourites: refreshedFavourites, fuel: alertsFuel, radiusKM: effectiveAlertsRadiusKM) updateLiveActivity() + refreshRoadDistancesIfNeeded() // No network fetch on foreground — pull-to-refresh is the override. } else { locationManager.stopForegroundTracking()