Sort every fuel tab cheapest-first (distance only as tiebreaker) in app + widget
This commit is contained in:
@@ -19,15 +19,13 @@ struct ContentView: View {
|
|||||||
stations
|
stations
|
||||||
.filter { $0.prices[selectedFuel] != nil }
|
.filter { $0.prices[selectedFuel] != nil }
|
||||||
.sorted { lhs, rhs in
|
.sorted { lhs, rhs in
|
||||||
// Prefer distance when we have a location, otherwise price.
|
// Cheapest first; distance only breaks ties.
|
||||||
guard let location else {
|
let lPrice = lhs.prices[selectedFuel]!
|
||||||
return lhs.prices[selectedFuel]! < rhs.prices[selectedFuel]!
|
let rPrice = rhs.prices[selectedFuel]!
|
||||||
}
|
if lPrice != rPrice { return lPrice < rPrice }
|
||||||
let lScore = lhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 +
|
guard let location else { return false }
|
||||||
lhs.prices[selectedFuel]! * 0.001
|
return lhs.distanceKM(to: location.lat, lng2: location.lng) <
|
||||||
let rScore = rhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 +
|
rhs.distanceKM(to: location.lat, lng2: location.lng)
|
||||||
rhs.prices[selectedFuel]! * 0.001
|
|
||||||
return lScore < rScore
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -91,9 +91,12 @@ struct FuelPriceTimelineProvider: TimelineProvider {
|
|||||||
let sorted: [FuelStation]
|
let sorted: [FuelStation]
|
||||||
if let location {
|
if let location {
|
||||||
sorted = filtered.sorted { lhs, rhs in
|
sorted = filtered.sorted { lhs, rhs in
|
||||||
let lScore = lhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + lhs.prices[fuel]! * 0.001
|
// Cheapest first; distance only breaks ties.
|
||||||
let rScore = rhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + rhs.prices[fuel]! * 0.001
|
let lPrice = lhs.prices[fuel]!
|
||||||
return lScore < rScore
|
let rPrice = rhs.prices[fuel]!
|
||||||
|
if lPrice != rPrice { return lPrice < rPrice }
|
||||||
|
return lhs.distanceKM(to: location.lat, lng2: location.lng) <
|
||||||
|
rhs.distanceKM(to: location.lat, lng2: location.lng)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sorted = filtered.sorted { $0.prices[fuel]! < $1.prices[fuel]! }
|
sorted = filtered.sorted { $0.prices[fuel]! < $1.prices[fuel]! }
|
||||||
|
|||||||
Reference in New Issue
Block a user