1
1
import CoreLocation. CLLocationManager
2
- #if !COCOAPODS
2
+ #if !PMKCOCOAPODS
3
3
import PromiseKit
4
4
#endif
5
5
@@ -27,11 +27,6 @@ extension CLLocationManager {
27
27
case whenInUse
28
28
}
29
29
30
- fileprivate class func promiseDoneForLocationManager( _ manager: CLLocationManager ) -> Void {
31
- manager. delegate = nil
32
- manager. stopUpdatingLocation ( )
33
- }
34
-
35
30
/**
36
31
- Returns: A new promise that fulfills with the most recent CLLocation.
37
32
- Note: To return all locations call `allResults()`.
@@ -40,38 +35,40 @@ extension CLLocationManager {
40
35
want to force one or the other, change this parameter from its default
41
36
value.
42
37
*/
43
- public class func promise( _ requestAuthorizationType: RequestAuthorizationType = . automatic) -> LocationPromise {
44
- return promise ( yielding: auther ( requestAuthorizationType) )
38
+ public class func requestLocation( authorizationType: RequestAuthorizationType = . automatic) -> Promise < [ CLLocation ] > {
39
+ return promise ( yielding: auther ( authorizationType) )
40
+ }
41
+
42
+ @available ( * , deprecated: 5.0 , renamed: " requestLocation " )
43
+ public class func promise( _ requestAuthorizationType: RequestAuthorizationType = . automatic) -> Promise < [ CLLocation ] > {
44
+ return requestLocation ( authorizationType: requestAuthorizationType)
45
45
}
46
46
47
- private class func promise( yielding yield: ( CLLocationManager ) -> Void = { _ in } ) -> LocationPromise {
47
+ private class func promise( yielding yield: ( CLLocationManager ) -> Void = { _ in } ) -> Promise < [ CLLocation ] > {
48
48
let manager = LocationManager ( )
49
49
manager. delegate = manager
50
50
yield( manager)
51
51
manager. startUpdatingLocation ( )
52
- _ = manager. promise. always {
53
- CLLocationManager . promiseDoneForLocationManager ( manager )
52
+ _ = manager. promise. ensure {
53
+ manager . stopUpdatingLocation ( )
54
54
}
55
55
return manager. promise
56
56
}
57
57
}
58
58
59
59
private class LocationManager : CLLocationManager , CLLocationManagerDelegate {
60
- let ( promise, fulfill , reject ) = LocationPromise . foo ( )
60
+ let ( promise, seal ) = Promise < [ CLLocation ] > . pending ( )
61
61
62
- @objc fileprivate func locationManager( _ manager: CLLocationManager , didUpdateLocations ll: [ CLLocation ] ) {
63
- let locations = ll
64
- fulfill ( locations)
65
- CLLocationManager . promiseDoneForLocationManager ( manager)
62
+ @objc fileprivate func locationManager( _ manager: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
63
+ seal. fulfill ( locations)
66
64
}
67
65
68
66
@objc func locationManager( _ manager: CLLocationManager , didFailWithError error: Error ) {
69
- let error = error as NSError
70
- if error . code == CLError . locationUnknown. rawValue && error . domain == kCLErrorDomain {
67
+ let ( domain , code ) = { ( $0 . domain , $0 . code ) } ( error as NSError )
68
+ if code == CLError . locationUnknown. rawValue && domain == kCLErrorDomain {
71
69
// Apple docs say you should just ignore this error
72
70
} else {
73
- reject ( error)
74
- CLLocationManager . promiseDoneForLocationManager ( manager)
71
+ seal. reject ( error)
75
72
}
76
73
}
77
74
}
@@ -80,22 +77,17 @@ private class LocationManager: CLLocationManager, CLLocationManagerDelegate {
80
77
#if os(iOS)
81
78
82
79
extension CLLocationManager {
83
- /**
84
- Cannot error, despite the fact this might be more useful in some
85
- circumstances, we stick with our decision that errors are errors
86
- and errors only. Thus your catch handler is always catching failures
87
- and not being abused for logic.
88
- */
80
+ /// request CoreLocation authorization from user
89
81
@available ( iOS 8 , * )
90
- public class func requestAuthorization( type: RequestAuthorizationType = . automatic) -> Promise < CLAuthorizationStatus > {
82
+ public class func requestAuthorization( type: RequestAuthorizationType = . automatic) -> Guarantee < CLAuthorizationStatus > {
91
83
return AuthorizationCatcher ( auther: auther ( type) , type: type) . promise
92
84
}
93
85
}
94
86
95
87
@available ( iOS 8 , * )
96
88
private class AuthorizationCatcher : CLLocationManager , CLLocationManagerDelegate {
97
- let ( promise, fulfill, _ ) = Promise < CLAuthorizationStatus> . pending( )
98
- var retainCycle : AnyObject ?
89
+ let ( promise, fulfill) = Guarantee < CLAuthorizationStatus> . pending( )
90
+ var retainCycle : AuthorizationCatcher ?
99
91
100
92
init ( auther: ( CLLocationManager ) -> Void , type: CLLocationManager . RequestAuthorizationType ) {
101
93
super. init ( )
@@ -108,12 +100,14 @@ private class AuthorizationCatcher: CLLocationManager, CLLocationManagerDelegate
108
100
default :
109
101
fulfill ( status)
110
102
}
103
+ promise. done { _ in
104
+ self . retainCycle = nil
105
+ }
111
106
}
112
107
113
108
@objc fileprivate func locationManager( _ manager: CLLocationManager , didChangeAuthorization status: CLAuthorizationStatus ) {
114
109
if status != . notDetermined {
115
110
fulfill ( status)
116
- retainCycle = nil
117
111
}
118
112
}
119
113
}
@@ -156,27 +150,4 @@ private func auther(_ requestAuthorizationType: CLLocationManager.RequestAuthori
156
150
157
151
#endif
158
152
159
-
160
- /// The promise returned by CLLocationManager.promise()
161
- public class LocationPromise : Promise < CLLocation > {
162
- // convoluted for concurrency guarantees
163
- private let ( parentPromise, fulfill, reject) = Promise< [ CLLocation] > . pending( )
164
-
165
- /// Convert the promise so that all Location results are returned
166
- public func asArray( ) -> Promise < [ CLLocation ] > {
167
- return parentPromise
168
- }
169
-
170
- fileprivate class func foo( ) -> ( LocationPromise , ( [ CLLocation ] ) -> Void , ( Error ) -> Void ) {
171
- var fulfill : ( ( CLLocation ) -> Void ) !
172
- var reject : ( ( Error ) -> Void ) !
173
- let promise = LocationPromise { fulfill = $0; reject = $1 }
174
-
175
- _ = promise. parentPromise. then ( on: zalgo) { fulfill ( $0. last!) }
176
- promise. parentPromise. catch ( on: zalgo, execute: reject)
177
-
178
- return ( promise, promise. fulfill, promise. reject)
179
- }
180
- }
181
-
182
153
#endif
0 commit comments