Skip to content

Commit

Permalink
Merge pull request #154 from theleftbit/better-error-handling
Browse files Browse the repository at this point in the history
Better Location Error handling
  • Loading branch information
daiku60 authored Sep 18, 2024
2 parents deda028 + af9dee5 commit a71c042
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions Sources/BSWFoundation/Location/LocationFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ import CoreLocation
@MainActor
public final class LocationFetcher: NSObject, CLLocationManagerDelegate {

public enum LocationErrors: Swift.Error {
public enum Error: LocalizedError {
case authorizationDenied
case coreLocationError(Swift.Error)
case unknown

public var errorDescription: String? {
switch self {
case .authorizationDenied:
return "LocationFetcher.Error.authorizationDenied"
case .coreLocationError(let error):
return error.localizedDescription
case .unknown:
return "LocationFetcher.Error.unknown"
}
}
}

public static let fetcher = LocationFetcher()

internal var locationManager = CLLocationManager()
private var continuations: [CheckedContinuation<CLLocation, Error>] = []
private var continuations: [CheckedContinuation<CLLocation, Swift.Error>] = []
public let desiredAccuracy = kCLLocationAccuracyHundredMeters
public var lastKnownLocation: CLLocation?

Expand Down Expand Up @@ -56,23 +68,23 @@ public final class LocationFetcher: NSObject, CLLocationManagerDelegate {
case .restricted:
fallthrough
case .denied:
throw LocationErrors.authorizationDenied
throw LocationFetcher.Error.authorizationDenied
case .authorizedAlways:
fallthrough
case .authorizedWhenInUse:
locationManager.requestLocation()
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
@unknown default:
throw LocationErrors.unknown
throw LocationFetcher.Error.unknown
}

return try await withCheckedThrowingContinuation({ continuation in
continuations.append(continuation)
})
}

private func completeCurrentRequest(_ result: Swift.Result<CLLocation, LocationErrors> = .failure(.unknown)) {
private func completeCurrentRequest(_ result: Swift.Result<CLLocation, LocationFetcher.Error>) {
continuations.forEach({
switch result {
case .failure(let error):
Expand All @@ -95,10 +107,10 @@ public final class LocationFetcher: NSObject, CLLocationManagerDelegate {
}
}

public nonisolated func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
public nonisolated func locationManager(_ manager: CLLocationManager, didFailWithError error: Swift.Error) {
print("Error finding location: \(error.localizedDescription)")
MainActor.assumeIsolated {
completeCurrentRequest()
completeCurrentRequest(.failure(.coreLocationError(error)))
}
}

Expand All @@ -113,11 +125,12 @@ public final class LocationFetcher: NSObject, CLLocationManagerDelegate {
return (status == .authorizedAlways || status == .authorizedWhenInUse)
#endif
}()

if isStatusAuthorized {
manager.requestLocation()
} else {
MainActor.assumeIsolated {
completeCurrentRequest()
completeCurrentRequest(.failure(.authorizationDenied))
}
}
}
Expand Down

0 comments on commit a71c042

Please sign in to comment.