Siri: scope cheapest-near-me to the saved search radius (5/10/15 mi)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user