Closest mode: deltas vs the CLOSEST station (signed +X.Xp/-X.Xp), not the nationwide cheapest; baseline = sortedStations.first

This commit is contained in:
FuelBoard Contributor
2026-08-11 20:04:29 +01:00
parent 3bc795317a
commit 2ca1274b8f
3 changed files with 22 additions and 13 deletions
+19 -10
View File
@@ -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 {