Location-driven results: widget fetches its own location (async, cache fallback), app auto-requests on first launch, Halifax sample stations
This commit is contained in:
@@ -23,8 +23,9 @@ enum FuelPriceProvider {
|
||||
|
||||
// MARK: - Sample provider (default for the scaffold)
|
||||
|
||||
/// Ships realistic stations around Cambridge (52.2053, 0.1218) so the app and
|
||||
/// widget render meaningful data with zero setup. Prices in pence/litre.
|
||||
/// Ships realistic stations around Halifax, West Yorkshire (53.7270, -1.8575)
|
||||
/// so the app and widget render meaningful distances for the user with zero
|
||||
/// setup. Prices in pence/litre.
|
||||
struct SampleFuelProvider: FuelPriceProviding {
|
||||
func fetchStations(near lat: Double?, lng: Double?, fuel: FuelType) async throws -> [FuelStation] {
|
||||
try await Task.sleep(nanoseconds: 300_000_000) // simulate fetch
|
||||
@@ -32,45 +33,45 @@ struct SampleFuelProvider: FuelPriceProviding {
|
||||
}
|
||||
|
||||
static let sampleStations: [FuelStation] = [
|
||||
FuelStation(id: "s1", name: "Shell Cambridge Retail Park", brand: "Shell",
|
||||
address: "12 Retail Park Way", postcode: "CB1 3EW",
|
||||
lat: 52.1955, lng: 0.1380,
|
||||
prices: [.e10: 142.9, .e5: 149.9, .diesel: 148.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s2", name: "Tesco Express Hills Road", brand: "Tesco",
|
||||
address: "245 Hills Road", postcode: "CB2 8RP",
|
||||
lat: 52.1809, lng: 0.1398,
|
||||
FuelStation(id: "h1", name: "Tesco Express Halifax", brand: "Tesco",
|
||||
address: "12 Clare Road", postcode: "HX1 2HX",
|
||||
lat: 53.7200, lng: -1.8630,
|
||||
prices: [.e10: 138.9, .e5: 146.9, .diesel: 145.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s3", name: "BP Milton Road", brand: "BP",
|
||||
address: "161 Milton Road", postcode: "CB4 1XE",
|
||||
lat: 52.2160, lng: 0.1410,
|
||||
prices: [.e10: 140.9, .diesel: 147.9],
|
||||
FuelStation(id: "h2", name: "Shell Skircoat Road", brand: "Shell",
|
||||
address: "245 Skircoat Road", postcode: "HX3 0HP",
|
||||
lat: 53.7120, lng: -1.8710,
|
||||
prices: [.e10: 142.9, .e5: 149.9, .diesel: 148.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s4", name: "Morrisons Newmarket Road", brand: "Morrisons",
|
||||
address: "Newmarket Road", postcode: "CB5 8AA",
|
||||
lat: 52.2164, lng: 0.1599,
|
||||
FuelStation(id: "h3", name: "Morrisons Halifax", brand: "Morrisons",
|
||||
address: "Haugh Shaw Road", postcode: "HX1 3TU",
|
||||
lat: 53.7265, lng: -1.8580,
|
||||
prices: [.e10: 137.9, .e5: 144.9, .diesel: 144.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s5", name: "Sainsbury's Coldhams Lane", brand: "Sainsbury's",
|
||||
address: "Coldhams Lane", postcode: "CB1 3HY",
|
||||
lat: 52.2025, lng: 0.1589,
|
||||
prices: [.e10: 139.9, .e5: 147.9, .diesel: 146.9],
|
||||
FuelStation(id: "h4", name: "BP Queensbury", brand: "BP",
|
||||
address: "316 High Street, Queensbury", postcode: "BD13 2NB",
|
||||
lat: 53.7550, lng: -1.8450,
|
||||
prices: [.e10: 140.9, .diesel: 147.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s6", name: "Esso Cherry Hinton", brand: "Esso",
|
||||
address: "Cherry Hinton Road", postcode: "CB1 9AP",
|
||||
lat: 52.1854, lng: 0.1657,
|
||||
FuelStation(id: "h5", name: "Asda Halifax", brand: "Asda",
|
||||
address: "Ovenden Way", postcode: "HX2 8DD",
|
||||
lat: 53.7340, lng: -1.8890,
|
||||
prices: [.e10: 136.9, .e5: 143.9, .diesel: 143.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "h6", name: "Esso Sowerby Bridge", brand: "Esso",
|
||||
address: "Wharf Street", postcode: "HX6 2AH",
|
||||
lat: 53.7080, lng: -1.9080,
|
||||
prices: [.e10: 141.9, .e5: 148.9, .diesel: 147.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s7", name: "Gulf Fen Road", brand: "Gulf",
|
||||
address: "Fen Road", postcode: "CB4 1UN",
|
||||
lat: 52.2201, lng: 0.1470,
|
||||
prices: [.e10: 143.9, .e5: 151.9, .diesel: 149.9],
|
||||
FuelStation(id: "h7", name: "Sainsbury's Elland", brand: "Sainsbury's",
|
||||
address: "Southgate", postcode: "HX5 0PA",
|
||||
lat: 53.6860, lng: -1.8380,
|
||||
prices: [.e10: 139.9, .e5: 147.9, .diesel: 146.9],
|
||||
priceUpdated: nil),
|
||||
FuelStation(id: "s8", name: "Asda Beehive Centre", brand: "Asda",
|
||||
address: "Coldhams Lane", postcode: "CB1 3ER",
|
||||
lat: 52.1995, lng: 0.1641,
|
||||
prices: [.e10: 136.9, .e5: 143.9, .diesel: 143.9],
|
||||
FuelStation(id: "h8", name: "Gulf Brighouse", brand: "Gulf",
|
||||
address: "Bradford Road", postcode: "HD6 1RW",
|
||||
lat: 53.7000, lng: -1.7850,
|
||||
prices: [.e10: 143.9, .e5: 151.9, .diesel: 149.9],
|
||||
priceUpdated: nil),
|
||||
]
|
||||
}
|
||||
|
||||
@@ -12,6 +12,11 @@ import Security
|
||||
|
||||
// MARK: - Fuel types
|
||||
|
||||
struct Coordinate: Equatable, Codable {
|
||||
let lat: Double
|
||||
let lng: Double
|
||||
}
|
||||
|
||||
enum FuelType: String, Codable, CaseIterable, Identifiable {
|
||||
case e10 // Unleaded 95 (E10)
|
||||
case e5 // Premium 97/98 (E5)
|
||||
@@ -94,11 +99,11 @@ struct FuelStore {
|
||||
|
||||
// MARK: Last known location ("lat,lng,unixTime")
|
||||
|
||||
static func loadLocation() -> (lat: Double, lng: Double, date: Date)? {
|
||||
static func loadLocation() -> Coordinate? {
|
||||
let raw = loadString(service: locationKey)
|
||||
let parts = raw?.split(separator: ",").compactMap { Double($0) }
|
||||
guard let parts, parts.count == 3 else { return nil }
|
||||
return (parts[0], parts[1], Date(timeIntervalSince1970: parts[2]))
|
||||
return Coordinate(lat: parts[0], lng: parts[1])
|
||||
}
|
||||
|
||||
static func saveLocation(lat: Double, lng: Double, date: Date = Date()) {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// WidgetLocationFetcher.swift — one-shot location for the WIDGET EXTENSION.
|
||||
//
|
||||
// WidgetKit providers run in a short budget, so this fetcher is deliberately
|
||||
// timeout-bounded: it requests a single location and gives up after a few
|
||||
// seconds, falling back to whatever the caller already has cached.
|
||||
//
|
||||
// Location permission is shared with the containing app (same TCC identity),
|
||||
// so if the user granted "While Using" in FuelBoard, the widget can use
|
||||
// location directly — no extra prompt in the car.
|
||||
|
||||
import Foundation
|
||||
import CoreLocation
|
||||
|
||||
@MainActor
|
||||
final class WidgetLocationFetcher: NSObject, CLLocationManagerDelegate {
|
||||
static let shared = WidgetLocationFetcher()
|
||||
|
||||
private let manager = CLLocationManager()
|
||||
private var continuation: CheckedContinuation<CLLocation?, Never>?
|
||||
private var timeoutTask: Task<Void, Never>?
|
||||
|
||||
private override init() {
|
||||
super.init()
|
||||
manager.delegate = self
|
||||
manager.desiredAccuracy = kCLLocationAccuracyHundredMeters
|
||||
}
|
||||
|
||||
/// Requests the current location. Returns nil on denial, timeout, or
|
||||
/// no-permission — callers should fall back to cached data.
|
||||
func currentLocation(timeout: TimeInterval = 4) async -> CLLocation? {
|
||||
guard CLLocationManager.locationServicesEnabled() else { return nil }
|
||||
|
||||
switch manager.authorizationStatus {
|
||||
case .authorizedWhenInUse, .authorizedAlways:
|
||||
break
|
||||
case .notDetermined:
|
||||
// Widget extensions share the app's TCC identity; if the app was
|
||||
// never opened, the system can't prompt from here — fall back.
|
||||
return nil
|
||||
case .denied, .restricted:
|
||||
return nil
|
||||
@unknown default:
|
||||
return nil
|
||||
}
|
||||
|
||||
return await withCheckedContinuation { cont in
|
||||
continuation = cont
|
||||
manager.requestLocation()
|
||||
timeoutTask = Task { [weak self] in
|
||||
try? await Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
|
||||
self?.continuation?.resume(returning: nil)
|
||||
self?.continuation = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||
MainActor.assumeIsolated {
|
||||
timeoutTask?.cancel()
|
||||
continuation?.resume(returning: locations.last)
|
||||
continuation = nil
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
||||
MainActor.assumeIsolated {
|
||||
timeoutTask?.cancel()
|
||||
continuation?.resume(returning: nil)
|
||||
continuation = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user