Siri: scope cheapest-near-me to the saved search radius (5/10/15 mi)

This commit is contained in:
FuelBoard Contributor
2026-08-14 12:49:30 +01:00
parent be781d80f5
commit 037c2418af
3 changed files with 64 additions and 28 deletions
+14 -6
View File
@@ -5,17 +5,25 @@ import Foundation
/// targets, and FuelBoardTests exercises this directly.
enum SiriCheapestLookup {
/// The cheapest station selling `fuel`, tie-broken by distance from the
/// given point. Stations without a price for `fuel` are skipped entirely.
/// Returns nil when no station sells the fuel.
/// The cheapest station selling `fuel` WITHIN `withinMiles` of the given
/// point, tie-broken by distance. Stations without a price for `fuel`, or
/// farther than the radius, are skipped entirely. Returns nil when no
/// station sells the fuel inside the radius.
///
/// Radius scoping mirrors the app's Stations tab (search radius
/// 5/10/15 mi): without it, "cheapest near me" silently answers the
/// UK-wide minimum a genuinely cheap station hundreds of miles away
/// (found with real data: GULF HISTON 100.9p was 132 mi from the phone).
static func cheapest(
in stations: [FuelStation],
fuel: FuelType,
fromLat lat: Double,
lng: Double
lng: Double,
withinMiles: Double
) -> FuelStation? {
stations
.filter { $0.prices[fuel] != nil }
let radiusKM = withinMiles * 1.60934
return stations
.filter { $0.prices[fuel] != nil && $0.distanceKM(to: lat, lng2: lng) <= radiusKM }
.min { lhs, rhs in
guard let lp = lhs.prices[fuel], let rp = rhs.prices[fuel] else {
return false