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
|
||||
.filter { $0.prices[selectedFuel] != nil }
|
||||
.sorted { lhs, rhs in
|
||||
// Prefer distance when we have a location, otherwise price.
|
||||
guard let location else {
|
||||
return lhs.prices[selectedFuel]! < rhs.prices[selectedFuel]!
|
||||
}
|
||||
let lScore = lhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 +
|
||||
lhs.prices[selectedFuel]! * 0.001
|
||||
let rScore = rhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 +
|
||||
rhs.prices[selectedFuel]! * 0.001
|
||||
return lScore < rScore
|
||||
// Cheapest first; distance only breaks ties.
|
||||
let lPrice = lhs.prices[selectedFuel]!
|
||||
let rPrice = rhs.prices[selectedFuel]!
|
||||
if lPrice != rPrice { return lPrice < rPrice }
|
||||
guard let location else { return false }
|
||||
return lhs.distanceKM(to: location.lat, lng2: location.lng) <
|
||||
rhs.distanceKM(to: location.lat, lng2: location.lng)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,9 +91,12 @@ struct FuelPriceTimelineProvider: TimelineProvider {
|
||||
let sorted: [FuelStation]
|
||||
if let location {
|
||||
sorted = filtered.sorted { lhs, rhs in
|
||||
let lScore = lhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + lhs.prices[fuel]! * 0.001
|
||||
let rScore = rhs.distanceKM(to: location.lat, lng2: location.lng) * 0.4 + rhs.prices[fuel]! * 0.001
|
||||
return lScore < rScore
|
||||
// Cheapest first; distance only breaks ties.
|
||||
let lPrice = lhs.prices[fuel]!
|
||||
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 {
|
||||
sorted = filtered.sorted { $0.prices[fuel]! < $1.prices[fuel]! }
|
||||
|
||||
Reference in New Issue
Block a user