diff --git a/FuelBoard/ContentView.swift b/FuelBoard/ContentView.swift index f1ade4f..600b13b 100644 --- a/FuelBoard/ContentView.swift +++ b/FuelBoard/ContentView.swift @@ -36,8 +36,15 @@ struct ContentView: View { } } - private var cheapestPrice: Double? { - poolStations.compactMap { $0.prices[selectedFuel] }.min() + /// The price reference every row's delta + RAG compares against. + /// Cheapest mode → the cheapest station in the pool (best value). + /// Closest mode → the CLOSEST station: deltas answer "is it worth leaving + /// the station nearest to me?" (+X.Xp pricier / -X.Xp cheaper). + private var baselinePrice: Double? { + if sortMode == .closest { + return sortedStations.first?.prices[selectedFuel] + } + return poolStations.compactMap { $0.prices[selectedFuel] }.min() } private var displayedStations: [FuelStation] { @@ -86,7 +93,7 @@ struct ContentView: View { selectedFuel: $selectedFuel, sortMode: $sortMode, stationLimit: $stationLimit, - cheapestPrice: cheapestPrice, + baselinePrice: baselinePrice, location: location, favouriteIDs: favouriteIDs, onToggleFavourite: toggleFavourite, @@ -214,14 +221,14 @@ struct StationRow: View { let station: FuelStation let fuel: FuelType let location: Coordinate? - let cheapestPrice: Double? + let baselinePrice: Double? let isTopResult: Bool let isFavourite: Bool var onToggleFavourite: () -> Void = {} private var ragColor: Color { - guard let price = station.prices[fuel], let cheapestPrice else { return .gray } - switch RAGRating.rating(price: price, cheapest: cheapestPrice) { + guard let price = station.prices[fuel], let baselinePrice else { return .gray } + switch RAGRating.rating(price: price, cheapest: baselinePrice) { case .green: return .green case .amber: return .orange case .red: return .red @@ -229,10 +236,12 @@ struct StationRow: View { } private var deltaText: String? { - guard let price = station.prices[fuel], let cheapestPrice else { return nil } - let delta = price - cheapestPrice - if delta <= 0.05 { return "best" } - return String(format: "+%.1fp", delta) + guard let price = station.prices[fuel], let baselinePrice else { return nil } + let delta = price - baselinePrice + if abs(delta) <= 0.05 { return "best" } + // Signed: +X.Xp pricier than baseline, -X.Xp cheaper (Closest mode); + // in Cheapest mode the baseline is the cheapest so deltas are ≥ 0. + return String(format: "%+.1fp", delta) } var body: some View { diff --git a/FuelBoard/FavouritesView.swift b/FuelBoard/FavouritesView.swift index 503f564..6bdd130 100644 --- a/FuelBoard/FavouritesView.swift +++ b/FuelBoard/FavouritesView.swift @@ -62,7 +62,7 @@ struct FavouritesView: View { station: station, fuel: selectedFuel, location: location, - cheapestPrice: cheapestPrice, + baselinePrice: cheapestPrice, isTopResult: index == 0, isFavourite: favouriteIDs.contains(station.id), onToggleFavourite: { onToggleFavourite(station) } diff --git a/FuelBoard/StationsView.swift b/FuelBoard/StationsView.swift index 2558e0d..b7156f4 100644 --- a/FuelBoard/StationsView.swift +++ b/FuelBoard/StationsView.swift @@ -9,7 +9,7 @@ struct StationsView: View { @Binding var selectedFuel: FuelType @Binding var sortMode: SortMode @Binding var stationLimit: Int - let cheapestPrice: Double? + let baselinePrice: Double? let location: Coordinate? let favouriteIDs: Set var onToggleFavourite: (FuelStation) -> Void = { _ in } @@ -110,7 +110,7 @@ struct StationsView: View { station: station, fuel: selectedFuel, location: location, - cheapestPrice: cheapestPrice, + baselinePrice: baselinePrice, isTopResult: index == 0, isFavourite: favouriteIDs.contains(station.id), onToggleFavourite: { onToggleFavourite(station) }