Created
July 6, 2022 07:14
-
-
Save alkozin/d208a151a7cbd23c411e3dcc85e4dba7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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