Skip to content

Commit

Permalink
Forward Geocoder errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lordzsolt committed Oct 5, 2020
1 parent e5f808d commit 17922d8
Showing 1 changed file with 59 additions and 12 deletions.
71 changes: 59 additions & 12 deletions Sources/CLGeocoderEvents+Rx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,33 @@ import CoreLocation
import RxCocoa
#endif

private extension NSError {
static var geocoderFoundNoResult: NSError {
NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil)
}
}

extension Reactive where Base: CLGeocoder {
/// Reactive wrapper for `CLGeocoder`.`reverseGeocodeLocation(_:completionHandler:)`
/// used to search for placemark
public func placemark(with location: CLLocation) -> Observable<CLPlacemark> {
return Observable.create { [base] observer in
base.reverseGeocodeLocation(location) { placemarks, _ in
observer.onNext(placemarks?.first)
base.reverseGeocodeLocation(location) { placemarks, error in
if let error = error {
observer.onError(error)
return
}

guard let placemark = placemarks?.first else {
observer.onError(NSError.geocoderFoundNoResult)
return
}

observer.onNext(placemark)
observer.onCompleted()
}
return Disposables.create {
base.cancelGeocode()
observer.onCompleted()
}
}.unwrap()
}
Expand All @@ -32,12 +48,22 @@ extension Reactive where Base: CLGeocoder {
@available(iOS 11.0, OSX 10.13, watchOSApplicationExtension 4.0, tvOS 11.0, *)
public func placemark(with location: CLLocation, preferredLocale: Locale) -> Observable<CLPlacemark> {
return Observable.create { [base] observer in
base.reverseGeocodeLocation(location, preferredLocale: preferredLocale) { placemarks, _ in
observer.onNext(placemarks?.first)
base.reverseGeocodeLocation(location, preferredLocale: preferredLocale) { placemarks, error in
if let error = error {
observer.onError(error)
return
}

guard let placemark = placemarks?.first else {
observer.onError(NSError.geocoderFoundNoResult)
return
}

observer.onNext(placemark)
observer.onCompleted()
}
return Disposables.create {
base.cancelGeocode()
observer.onCompleted()
}
}.unwrap()
}
Expand All @@ -46,9 +72,16 @@ extension Reactive where Base: CLGeocoder {
/// used to search for placemark
public func placemarks(with addressString: String) -> Observable<[CLPlacemark]> {
return Observable.create { [base] observer in
base.geocodeAddressString(addressString) { placemarks, _ in
base.geocodeAddressString(addressString) { placemarks, error in
if let error = error {
observer.onError(error)
return
}

// Forward placemarks Array, because `geocodeAddressString` might not resolve to a single location.
// See documentation for `CLGeocodeCompletionHandler`
guard let placemarks = placemarks else {
observer.onError(NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil))
observer.onError(NSError.geocoderFoundNoResult)
return
}

Expand All @@ -65,9 +98,16 @@ extension Reactive where Base: CLGeocoder {
/// used to search for placemark
public func placemarks(with addressString: String, in region: CLRegion?) -> Observable<[CLPlacemark]> {
return Observable.create { [base] observer in
base.geocodeAddressString(addressString, in: region) { placemarks, _ in
base.geocodeAddressString(addressString, in: region) { placemarks, error in
if let error = error {
observer.onError(error)
return
}

// Forward placemarks Array, because `geocodeAddressString` might not resolve to a single location.
// See documentation for `CLGeocodeCompletionHandler`
guard let placemarks = placemarks else {
observer.onError(NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil))
observer.onError(NSError.geocoderFoundNoResult)
return
}

Expand All @@ -85,9 +125,16 @@ extension Reactive where Base: CLGeocoder {
@available(iOS 11.0, OSX 10.13, watchOSApplicationExtension 4.0, tvOS 11.0, *)
public func geocodeAddressString(_ addressString: String, in region: CLRegion?, preferredLocale locale: Locale?) -> Observable<[CLPlacemark]> {
return Observable.create { [base] observer in
base.geocodeAddressString(addressString, in: region, preferredLocale: locale) { placemarks, _ in
base.geocodeAddressString(addressString, in: region, preferredLocale: locale) { placemarks, error in
if let error = error {
observer.onError(error)
return
}

// Forward placemarks Array, because `geocodeAddressString` might not resolve to a single location.
// See documentation for `CLGeocodeCompletionHandler`
guard let placemarks = placemarks else {
observer.onError(NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil))
observer.onError(NSError.geocoderFoundNoResult)
return
}

Expand Down

0 comments on commit 17922d8

Please sign in to comment.