Cheapest reference uses chosen miles radius (same pool as list) in app + widget; header shows 'within N miles'
This commit is contained in:
@@ -21,15 +21,15 @@ struct ContentView: View {
|
|||||||
@State private var locationManager = LocationManager()
|
@State private var locationManager = LocationManager()
|
||||||
@StateObject private var monitor = ProximityMonitor()
|
@StateObject private var monitor = ProximityMonitor()
|
||||||
|
|
||||||
/// Stations within the alert trigger radius of the current location — used
|
/// Stations within the CHOSEN search radius (miles) of the current location
|
||||||
/// ONLY as the "cheapest" reference (RAG, TOP, deltas), so the cheapest
|
/// — the same pool the list shows, so "cheapest" (RAG, TOP, deltas) matches
|
||||||
/// label matches what alerts would flag. The list itself shows the full
|
/// exactly what's on screen. Falls back to the full fetch when location is
|
||||||
/// fetch so the count picker works. Falls back to the full fetch when
|
/// unknown or the radius pool is empty.
|
||||||
/// location is unknown or the radius pool is empty.
|
|
||||||
private var radiusScopedStations: [FuelStation] {
|
private var radiusScopedStations: [FuelStation] {
|
||||||
guard let location else { return stations }
|
guard let location else { return stations }
|
||||||
|
let radiusKM = Double(stationLimit) * 1.60934 // chosen miles → km
|
||||||
let within = stations.filter {
|
let within = stations.filter {
|
||||||
$0.distanceKM(to: location.lat, lng2: location.lng) <= alertsRadius
|
$0.distanceKM(to: location.lat, lng2: location.lng) <= radiusKM
|
||||||
}
|
}
|
||||||
return within.isEmpty ? stations : within
|
return within.isEmpty ? stations : within
|
||||||
}
|
}
|
||||||
@@ -102,7 +102,6 @@ struct ContentView: View {
|
|||||||
stationLimit: $stationLimit,
|
stationLimit: $stationLimit,
|
||||||
cheapestPrice: cheapestPrice,
|
cheapestPrice: cheapestPrice,
|
||||||
location: location,
|
location: location,
|
||||||
radiusKM: alertsRadius,
|
|
||||||
favouriteIDs: favouriteIDs,
|
favouriteIDs: favouriteIDs,
|
||||||
onToggleFavourite: toggleFavourite,
|
onToggleFavourite: toggleFavourite,
|
||||||
onRefresh: { await refresh(force: true) }
|
onRefresh: { await refresh(force: true) }
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ struct StationsView: View {
|
|||||||
@Binding var stationLimit: Int
|
@Binding var stationLimit: Int
|
||||||
let cheapestPrice: Double?
|
let cheapestPrice: Double?
|
||||||
let location: Coordinate?
|
let location: Coordinate?
|
||||||
let radiusKM: Double
|
|
||||||
let favouriteIDs: Set<String>
|
let favouriteIDs: Set<String>
|
||||||
var onToggleFavourite: (FuelStation) -> Void = { _ in }
|
var onToggleFavourite: (FuelStation) -> Void = { _ in }
|
||||||
var onRefresh: () async -> Void = {}
|
var onRefresh: () async -> Void = {}
|
||||||
@@ -21,7 +20,7 @@ struct StationsView: View {
|
|||||||
List {
|
List {
|
||||||
Section {
|
Section {
|
||||||
if let location {
|
if let location {
|
||||||
Text("\(sortMode == .closest ? "Closest" : "Cheapest") \(selectedFuel.displayName) within \(Int(radiusKM)) km — tap a station for directions.")
|
Text("\(sortMode == .closest ? "Closest" : "Cheapest") \(selectedFuel.displayName) within \(stationLimit) miles — tap a station for directions.")
|
||||||
.font(.footnote)
|
.font(.footnote)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -85,10 +85,11 @@ struct FuelPriceTimelineProvider: TimelineProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3) Load stations, then sort by price (distance tiebreak), scoped to
|
// 3) Load stations, then sort by price (distance tiebreak), scoped to
|
||||||
// the alert trigger radius so the widget's "cheapest" matches the app.
|
// the chosen search radius (miles) so the widget's "cheapest" matches
|
||||||
|
// the app's list.
|
||||||
var stations = FuelStore.loadStations()
|
var stations = FuelStore.loadStations()
|
||||||
if stations.isEmpty { stations = SampleFuelProvider.sampleStations }
|
if stations.isEmpty { stations = SampleFuelProvider.sampleStations }
|
||||||
let radiusKM = FuelStore.loadAlertsRadius()
|
let radiusKM = Double(FuelStore.loadStationLimit()) * 1.60934 // miles → km
|
||||||
if let location {
|
if let location {
|
||||||
let within = stations.filter {
|
let within = stations.filter {
|
||||||
$0.distanceKM(to: location.lat, lng2: location.lng) <= radiusKM
|
$0.distanceKM(to: location.lat, lng2: location.lng) <= radiusKM
|
||||||
|
|||||||
Reference in New Issue
Block a user