Scope cheapest/RAG/TOP to alert trigger radius in app + widget; header shows 'within X km'; falls back to full fetch when radius pool empty

This commit is contained in:
FuelBoard Contributor
2026-08-11 17:21:36 +01:00
parent cde51622a3
commit 2d95c4e1d6
3 changed files with 34 additions and 6 deletions
+15 -2
View File
@@ -21,8 +21,20 @@ struct ContentView: View {
@State private var locationManager = LocationManager()
@StateObject private var monitor = ProximityMonitor()
/// Stations within the alert trigger radius of the current location, used
/// as the pool for "cheapest" (RAG reference, TOP, sorting). Falls back to
/// the full fetch when location is unknown or the radius pool is empty
/// (rural areas) so the list is never blank.
private var radiusScopedStations: [FuelStation] {
guard let location else { return stations }
let within = stations.filter {
$0.distanceKM(to: location.lat, lng2: location.lng) <= alertsRadius
}
return within.isEmpty ? stations : within
}
private var cheapestPrice: Double? {
stations.compactMap { $0.prices[selectedFuel] }.min()
radiusScopedStations.compactMap { $0.prices[selectedFuel] }.min()
}
private var displayedStations: [FuelStation] {
@@ -30,7 +42,7 @@ struct ContentView: View {
}
private var sortedStations: [FuelStation] {
let available = stations.filter { $0.prices[selectedFuel] != nil }
let available = radiusScopedStations.filter { $0.prices[selectedFuel] != nil }
switch sortMode {
case .closest:
guard let location else { return available.sorted { $0.prices[selectedFuel]! < $1.prices[selectedFuel]! } }
@@ -73,6 +85,7 @@ struct ContentView: View {
stationLimit: $stationLimit,
cheapestPrice: cheapestPrice,
location: location,
radiusKM: alertsRadius,
favouriteIDs: favouriteIDs,
onToggleFavourite: toggleFavourite
)