Skip to content

Instantly share code, notes, and snippets.

@alkozin
Created July 6, 2022 07:14
Show Gist options
  • Save alkozin/d208a151a7cbd23c411e3dcc85e4dba7 to your computer and use it in GitHub Desktop.
Save alkozin/d208a151a7cbd23c411e3dcc85e4dba7 to your computer and use it in GitHub Desktop.
extension CLLocation: Asking, Expectable {
static func ask<E>(with: Any?, in pipe: Pipe, expect: Expect<E>) {
let source = with as? CLLocationManager ?? pipe.get()
let handler = { (status: CLAuthorizationStatus) -> Bool in
guard status != .notDetermined else {
return true
}
switch (expect.condition) {
case .one:
source.requestLocation()
default:
source.startUpdatingLocation()
}
return false
}
//Expect specific status
if let status = with as? CLAuthorizationStatus ?? pipe.get() {
status | .while(inner: true, handler: handler)
} else {
//Or start expecting from specific manager
source | .while(inner: true, handler: handler)
}
expect.cleaner = {
source.stopUpdatingLocation()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment