This commit is contained in:
Daniel Arantes Loverde
2026-02-06 09:51:53 -03:00
parent 379bb35cfa
commit 92c2a67736
13 changed files with 574 additions and 138 deletions

View File

@@ -5,9 +5,24 @@ import CoreLocation
#endif
final class LocationService: NSObject {
typealias LocationResult = Result<(Double, Double), LocationError>
#if os(iOS)
enum LocationError: Error {
case servicesDisabled
case denied
case unavailable
}
#else
enum LocationError: Error {
case denied
case unavailable
}
#endif
#if os(iOS)
private let manager = CLLocationManager()
private var completion: ((Double, Double) -> Void)?
private var completion: ((LocationResult) -> Void)?
#endif
override init() {
@@ -18,28 +33,52 @@ final class LocationService: NSObject {
#endif
}
func requestLocation(_ completion: @escaping (Double, Double) -> Void) {
func requestLocation(_ completion: @escaping (LocationResult) -> Void) {
#if os(iOS)
guard CLLocationManager.locationServicesEnabled() else {
completion(.failure(.servicesDisabled))
return
}
self.completion = completion
manager.requestWhenInUseAuthorization()
manager.requestLocation()
#else
// Android: implement later with platform-specific bridge
_ = completion
let defaults = UserDefaults.standard
if defaults.bool(forKey: "location_permission_denied") {
completion(.failure(.denied))
return
}
guard let latRaw = defaults.string(forKey: "last_location_lat"),
let lngRaw = defaults.string(forKey: "last_location_lng"),
let lat = Double(latRaw),
let lng = Double(lngRaw) else {
completion(.failure(.unavailable))
return
}
completion(.success((lat, lng)))
#endif
}
}
#if os(iOS)
extension LocationService: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus
if status == .denied || status == .restricted {
completion?(.failure(.denied))
completion = nil
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
completion?(location.coordinate.latitude, location.coordinate.longitude)
completion?(.success((location.coordinate.latitude, location.coordinate.longitude)))
completion = nil
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
// Silence for now; UI can handle missing location.
completion?(.failure(.unavailable))
completion = nil
}
}