Files
fuelboard/FuelBoard/FavouritesView.swift
T
FuelBoard Contributor 8153a36168 Make Closest a station label and retire Top
Top and Cheapest were two words for one state: both meant "the lowest price
among the stations on screen", differing only in the candidate set (radius vs
Favourites). Which word a surface used had drifted by device — the phone's
rows said Cheapest while the watch said Top for the identical state. Top is
gone from the enum, the Key legend, the badge and the watch's hand-rolled
label; Cheapest survives because it names the measure and is already the word
the favourites copy uses.

Closest is now a real station label rather than a verdict. StationRecommendationBadge
gains .closest with a total precedence — Best value, then Cheapest, then Closest
— because a row has room for one label and proximity is the weakest claim: a
short drive says nothing about the price, and unlike the other two it reports no
saving at all. The badge sits above the price, where the other two sit. The
closest row's verdict capsule is suppressed: its only word for that state was
"Closest", so printing it below as well would state the same fact twice, three
lines apart. Other rows keep their Saves / Break even / Costs capsules unchanged.

"Closest station" becomes "Closest", read from the single
FuelSavingsResult.baselineHeadline constant so the badge, the verdict and the Key
cannot word it differently. The Key legend renders all three station labels with
the shared StationLabelBadge; the old KeyStationLabelIndicator, which
special-cased the closest station to a wider verdict pill so it read as a
different class of thing from the two above it, is deleted.

One closest icon everywhere. The listing and detail verdict symbols used
location.circle and location.circle.fill for the state the Key taught as
location.north.fill; both now read StationLabelKind.closest.symbol, so the three
surfaces resolve one glyph from one place.

The detail sheet gains the Closest capsule below the map, ordered to match the
Key. Favourites gains the nearest favourite as its Closest, measured on the same
one-way distance its rows print and restricted to favourites with a price for the
active fuel — matching the radius tab, where the closest station is by definition
the savings baseline.

closestStationID flows from the one scaffold baseline the savings lookup already
compares against, so a row's badge and the reference station cannot disagree.
No savings calculation, threshold, ranking, eligibility or station-selection
logic changed.

The Key sheet's detent is now bound rather than fixed, so dragging it up to read
the Station labels section sticks instead of snapping back; a QA launch argument
opens it at full height, since the 0.6 detent puts that section below the fold on
every device. That argument is read unconditionally, unlike the other harness
hooks which are #if DEBUG-gated.

Tests: 278 pass, 9 new — the label precedence ladder across every combination,
capsule order in the sheet, Closest alone, and the retired label pinned absent.
2026-10-19 14:59:47 +01:00

314 lines
16 KiB
Swift

import SwiftUI
/// Favourites tab — starred stations, split by fuel type. Each fuel type
/// with at least one favourite appears as its own tab. The list shows the
/// USER'S manual order (drag to reorder — Edit button in the toolbar): the
/// FIRST station in each fuel's list is the one used as "the favourite" in
/// single widgets. Favourites are fuel-scoped entries — starring a station
/// only pins it for the fuel you were viewing, so an Unleaded favourite
/// never shows as Diesel.
struct FavouritesView: View {
let favourites: [FavouriteEntry]
/// The app's currently selected fuel — used only as the initial tab.
let selectedFuel: FuelType
let location: Coordinate?
let distanceUnit: DistanceUnit
let priceDisplayStyle: PriceDisplayStyle
let stationTapAction: StationTapAction
/// Fuel Savings config. When it is configured this tab shows the SAME
/// fill-up control and per-row estimate as the Stations list: one session
/// amount is shared, so adjusting it in either list updates both, and it
/// still never touches the Settings default.
let fuelSavingsSettings: FuelSavingsSettings
@Binding var fuelSavingsAmount: FuelSavingsFillAmount
/// Estimated saving for a favourite, against the closest station by
/// journey distance — the same baseline and definition the Stations list uses.
var savingsForStation: (FuelStation) -> FuelSavingsAvailability? = { _ in nil }
var journeyDiagnosticsForStation: (FuelStation) -> FuelSavingsJourneyDiagnostics? = { _ in nil }
var onToggleFavourite: (FuelStation, FuelType) -> Void = { _, _ in }
/// Persists a reordered favourites array (after drag-and-drop).
var onReorder: ([FavouriteEntry]) -> Void = { _ in }
/// Propagated from ContentView — Trends' history failure with no data
/// raises the global connection banner; recovery clears it.
var onHistoryUnavailable: (() -> Void)? = nil
var onHistoryRecovered: (() -> Void)? = nil
/// Fuel types that currently have at least one favourite — these are the
/// only tabs shown (a fuel with no favourites gets no tab).
private var availableFuels: [FuelType] {
FuelType.allCases.filter { fuel in
favourites.contains { $0.fuel == fuel }
}
}
/// The active tab. Falls back if the chosen fuel ever loses all its
/// favourites (e.g. the last one is un-starred while viewing it).
@State private var fuel: FuelType = .e10
/// Trends sheet (price history chart) presentation state.
@State private var showTrends = false
/// The Key (legend) sheet — the same sheet the Stations tab presents, opened
/// from the info button beside Edit.
@State private var showKey = false
/// The Key sheet's two nested sheets, owned here because its rows set them
/// (same arrangement as StationsView, so the QA hooks behave identically).
@State private var showBestValueInfo = false
@State private var showImportantInformation = false
/// Station picked for the More-info sheet (tap action = showDetails).
@State private var detailStation: FuelStation?
private var activeFuel: FuelType {
availableFuels.contains(fuel) ? fuel : (availableFuels.first ?? .e10)
}
/// Stations favourited for the active fuel, in the USER'S stored order —
/// NOT price-sorted. The first row is the favourite used by single
/// widgets (see footer note).
private var ordered: [FuelStation] {
favourites.filter { $0.fuel == activeFuel }.map(\.station)
}
/// Cheapest favourited station for the active fuel — the RAG baseline and
/// the "cheapest favourite" callout, independent of the manual order.
private var cheapest: FuelStation? {
ordered.min { ($0.prices[activeFuel] ?? .infinity) < ($1.prices[activeFuel] ?? .infinity) }
}
private var cheapestPrice: Double? { cheapest?.prices[activeFuel] }
/// Nearest favourited station for the active fuel — this tab's Closest.
///
/// Measured on the same one-way distance the rows print, so the badge cannot
/// point at a station the list shows as further away than its neighbour. Only
/// favourites with a price for the active fuel are eligible, matching the
/// radius tab, where the closest station is by definition the savings
/// baseline; an unpriced favourite appears last and wears no label. Ties keep
/// the user's own order, which is what `ordered` already encodes.
private var nearest: FuelStation? {
guard let location else { return nil }
var best: (station: FuelStation, km: Double)?
for station in ordered where station.prices[activeFuel] != nil {
let km = FuelStore.stationDistanceDisplay(
station: station, userLat: location.lat, userLng: location.lng
).km
if best == nil || km < best!.km { best = (station, km) }
}
return best?.station
}
/// The baseline note is useful only when at least one visible favourite has
/// an actual savings comparison. An unavailable result (for example, while
/// location is missing) must not leave an orphaned explanation below the list.
private var showsSavingsComparisonExplanation: Bool {
fuelSavingsSettings.isConfigured && ordered.contains { station in
savingsForStation(station)?.result != nil
}
}
/// IDs favourited for the active fuel — the star state on each row.
private var activeFuelFavouriteIDs: Set<String> {
Set(favourites.filter { $0.fuel == activeFuel }.map(\.station.id))
}
init(favourites: [FavouriteEntry],
selectedFuel: FuelType,
location: Coordinate?,
distanceUnit: DistanceUnit,
priceDisplayStyle: PriceDisplayStyle,
stationTapAction: StationTapAction,
fuelSavingsSettings: FuelSavingsSettings = .defaults,
fuelSavingsAmount: Binding<FuelSavingsFillAmount>,
savingsForStation: @escaping (FuelStation) -> FuelSavingsAvailability? = { _ in nil },
journeyDiagnosticsForStation: @escaping (FuelStation) -> FuelSavingsJourneyDiagnostics? = { _ in nil },
onToggleFavourite: @escaping (FuelStation, FuelType) -> Void = { _, _ in },
onReorder: @escaping ([FavouriteEntry]) -> Void = { _ in },
onHistoryUnavailable: (() -> Void)? = nil,
onHistoryRecovered: (() -> Void)? = nil) {
self.favourites = favourites
self.selectedFuel = selectedFuel
self.location = location
self.distanceUnit = distanceUnit
self.priceDisplayStyle = priceDisplayStyle
self.stationTapAction = stationTapAction
self.fuelSavingsSettings = fuelSavingsSettings
_fuelSavingsAmount = fuelSavingsAmount
self.savingsForStation = savingsForStation
self.journeyDiagnosticsForStation = journeyDiagnosticsForStation
self.onToggleFavourite = onToggleFavourite
self.onReorder = onReorder
self.onHistoryUnavailable = onHistoryUnavailable
self.onHistoryRecovered = onHistoryRecovered
_fuel = State(initialValue: selectedFuel)
}
/// Drag-and-drop reorder (List edit mode): the active fuel's entries are
/// reordered within the global array; the fuel's block keeps its original
/// position and other fuels keep their relative order (FuelStore helper).
private func moveFavourite(fromOffsets source: IndexSet, toOffset destination: Int) {
onReorder(FuelStore.reorderedFavourites(
favourites, fuel: activeFuel,
fromOffsets: source, toOffset: destination
))
}
var body: some View {
NavigationStack {
List {
if favourites.isEmpty {
Section {
VStack(spacing: 10) {
Image(systemName: "star")
.font(.system(size: 40))
.foregroundStyle(.secondary)
Text(String(localized: "No favourites yet", comment: "Favourites empty-state title"))
.font(.headline)
Text(String(localized: "Tap the star on any station in the Stations tab to pin it here. The first station in each list becomes the favourite used in single widgets — drag to put your pick first.", comment: "Favourites empty-state explanation"))
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.frame(maxWidth: .infinity)
.padding(.vertical, 24)
}
} else {
Section(String(localized: "Fuel type", comment: "Fuel type section title")) {
FuelTypeSegmentedPicker(
selection: $fuel,
fuels: availableFuels
)
}
if fuelSavingsSettings.isConfigured {
Section {
FuelSavingsAdjustmentView(amount: $fuelSavingsAmount)
}
}
if showsSavingsComparisonExplanation {
Section {
Text(String(localized: "Price colours show pump price competitiveness. Savings compare with your closest station, including journey costs.", comment: "Station listing price and savings explanation"))
.font(.footnote)
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
}
Section {
ForEach(Array(ordered.enumerated()), id: \.element.id) { index, station in
StationRow(
station: station,
fuel: activeFuel,
location: location,
distanceUnit: distanceUnit,
priceDisplayStyle: priceDisplayStyle,
baselinePrice: cheapestPrice,
isCheapestResult: station.id == cheapest?.id,
isClosestResult: station.id == nearest?.id,
isFavourite: activeFuelFavouriteIDs.contains(station.id),
tapAction: stationTapAction,
fuelSavings: savingsForStation(station),
onToggleFavourite: { onToggleFavourite(station, activeFuel) },
onShowDetails: { detailStation = $0 }
)
}
.onMove(perform: moveFavourite)
} header: {
Text(String(format: String(localized: "%@ favourites", comment: "Favourites section title for fuel type"), activeFuel.displayName))
} footer: {
VStack(alignment: .leading, spacing: 12) {
Text(String(localized: "The first station is the favourite used in single widgets. Tap Edit, then drag to reorder.", comment: "Favourites reorder explanation"))
}
.font(.footnote)
.foregroundStyle(.secondary)
.frame(maxWidth: .infinity, alignment: .leading)
}
if let cheapest = cheapest {
Section {
Text(String(format: String(localized: "%@ is your cheapest %@ favourite. Favourites are monitored automatically — switch to the Alerts tab and enable alerts to get pinged when you approach one that's the cheapest within the radius.", comment: "Cheapest favourite explanation"), cheapest.name, activeFuel.displayName))
.font(.caption)
.foregroundStyle(.secondary)
}
}
}
}
.navigationTitle(String(localized: "Favourites", comment: "Favourites tab title"))
.toolbar {
ToolbarItemGroup(placement: .topBarTrailing) {
if !favourites.isEmpty {
Button {
showTrends = true
} label: {
Image(systemName: "chart.xyaxis.line")
.accessibilityLabel(String(localized: "Trends", comment: "Trends button accessibility label"))
}
EditButton()
}
// Same button and same slot as the Stations tab: last in the
// group, so it sits to the right of Edit. Shown even with no
// favourites — the legend explains the prices and labels, which
// matter before anything is starred.
Button {
showKey = true
} label: {
Image(systemName: "info.circle")
}
.accessibilityLabel(String(localized: "Key", comment: "Key button accessibility label"))
}
}
.sheet(isPresented: $showKey) {
KeySheetView(
showBestValueInfo: $showBestValueInfo,
showImportantInformation: $showImportantInformation
)
}
.sheet(isPresented: $showTrends) {
TrendsView(
favourites: favourites,
selectedFuel: activeFuel,
priceDisplayStyle: priceDisplayStyle,
onHistoryUnavailable: onHistoryUnavailable,
onHistoryRecovered: onHistoryRecovered
)
}
.sheet(item: $detailStation) { station in
StationDetailView(
station: station,
location: location,
distanceUnit: distanceUnit,
priceStyle: priceDisplayStyle,
selectedFuel: activeFuel,
fuelSavings: savingsForStation(station),
fuelSavingsAmount: fuelSavingsAmount,
journeyDiagnostics: journeyDiagnosticsForStation(station),
// This tab's cheapest favourite — the same flag its rows get,
// and the same label they wear.
isCheapestInRange: station.id == cheapest?.id,
// This tab's nearest favourite — the same flag its rows get.
isClosestInRange: station.id == nearest?.id,
// Every station listed here IS a favourite for the tab's
// fuel, so the star starts filled; tapping it unstars.
isFavourite: true,
onToggleFavourite: { onToggleFavourite(station, activeFuel) }
)
}
.onReceive(NotificationCenter.default.publisher(for: .fuelBoardOpenBestValueSettings)) { _ in
// "Setup Best Value" from the explanation sheet, opened inside
// this view's More info sheet. That sheet closes itself; this one
// is presented from HERE and would otherwise stay over Settings.
detailStation = nil
}
.onAppear {
#if DEBUG
// QA hook: launch with `-showTrends` to open the sheet
// without a tap (same pattern as -showKeySheet).
if ProcessInfo.processInfo.arguments.contains("-showTrends") {
showTrends = true
}
#endif
}
}
}
}