Last active
December 9, 2018 13:47
-
-
Save Juanpe/bc99fee4dc43478c7ae1e64979f3bbe1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserLocationService: NSObject, UserLocationProvider { | |
fileprivate var provider: LocationProvider | |
fileprivate var locationCompletionBlock: UserLocationCompletionBlock? | |
init(with provider: LocationProvider) { | |
self.provider = provider | |
super.init() | |
} | |
func findUserLocation(then: @escaping UserLocationCompletionBlock) { | |
self.locationCompletionBlock = then | |
if provider.isUserAuthorized { | |
provider.requestLocation() | |
} else { | |
provider.requestWhenInUseAuthorization() | |
} | |
} | |
} | |
extension UserLocationService: CLLocationManagerDelegate { | |
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { | |
if status == .authorizedWhenInUse { | |
provider.requestLocation() | |
} | |
} | |
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
manager.stopUpdatingLocation() | |
if let location = locations.last { | |
locationCompletionBlock?(location, nil) | |
} else { | |
locationCompletionBlock?(nil, .canNotBeLocated) | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment