@@ -27,25 +27,33 @@ extension CLLocationManager {
27
27
case whenInUse
28
28
}
29
29
30
- /**
31
- - Returns: A new promise that fulfills with the most recent CLLocation.
32
- - Note: To return all locations call `allResults()`.
33
- - Parameter requestAuthorizationType: We read your Info plist and try to
34
- determine the authorization type we should request automatically. If you
35
- want to force one or the other, change this parameter from its default
36
- value.
37
- */
38
- public class func requestLocation( authorizationType: RequestAuthorizationType = . automatic) -> Promise < [ CLLocation ] > {
39
- return promise ( yielding: auther ( authorizationType) )
30
+ /// Request a single location using promises
31
+ /// - Note: To return all locations call `allResults()`.
32
+ ///
33
+ /// - Parameters:
34
+ /// - authorizationType: requestAuthorizationType: We read your Info plist and try to
35
+ /// determine the authorization type we should request automatically. If you
36
+ /// want to force one or the other, change this parameter from its default
37
+ /// value.
38
+ /// - block: A block by which to perform any filtering of the locations
39
+ /// that are returned. For example:
40
+ /// - In order to only retrieve accurate locations, only
41
+ /// return true if the locations horizontal accuracy < 50
42
+ ///
43
+ /// - Returns: A new promise that fulfills with the most recent CLLocation
44
+ /// that satisfies the provided block if it exists. If the block
45
+ /// does not exist, simply return the last location.
46
+ public class func requestLocation( authorizationType: RequestAuthorizationType = . automatic, satisfying block: ( ( CLLocation ) -> Bool ) ? = nil ) -> Promise < [ CLLocation ] > {
47
+ return promise ( yielding: auther ( authorizationType) , satisfying: block)
40
48
}
41
49
42
50
@available ( * , deprecated: 5.0 , renamed: " requestLocation " )
43
- public class func promise( _ requestAuthorizationType: RequestAuthorizationType = . automatic) -> Promise < [ CLLocation ] > {
44
- return requestLocation ( authorizationType: requestAuthorizationType)
51
+ public class func promise( _ requestAuthorizationType: RequestAuthorizationType = . automatic, satisfying block : ( ( CLLocation ) -> Bool ) ? = nil ) -> Promise < [ CLLocation ] > {
52
+ return requestLocation ( authorizationType: requestAuthorizationType, satisfying : block )
45
53
}
46
54
47
- private class func promise( yielding yield: ( CLLocationManager ) -> Void = { _ in } ) -> Promise < [ CLLocation ] > {
48
- let manager = LocationManager ( )
55
+ private class func promise( yielding yield: ( CLLocationManager ) -> Void = { _ in } , satisfying block : ( ( CLLocation ) -> Bool ) ? = nil ) -> Promise < [ CLLocation ] > {
56
+ let manager = LocationManager ( satisfying : block )
49
57
manager. delegate = manager
50
58
yield( manager)
51
59
manager. startUpdatingLocation ( )
@@ -58,9 +66,19 @@ extension CLLocationManager {
58
66
59
67
private class LocationManager : CLLocationManager , CLLocationManagerDelegate {
60
68
let ( promise, seal) = Promise< [ CLLocation] > . pending( )
69
+ let satisfyingBlock : ( ( CLLocation ) -> Bool ) ?
61
70
62
71
@objc fileprivate func locationManager( _ manager: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
63
- seal. fulfill ( locations)
72
+ if let block = satisfyingBlock {
73
+ let satisfiedLocations = locations. filter { block ( $0) == true }
74
+ seal. fulfill ( satisfiedLocations)
75
+ } else {
76
+ seal. fulfill ( locations)
77
+ }
78
+ }
79
+
80
+ init ( satisfying block: ( ( CLLocation ) -> Bool ) ? = nil ) {
81
+ self . satisfyingBlock = block
64
82
}
65
83
66
84
@objc func locationManager( _ manager: CLLocationManager , didFailWithError error: Error ) {
0 commit comments