@@ -10,7 +10,6 @@ import CoreLocation
1010///Location manager streaming data asynchronously via instance of ``AsyncThrowingStream`` returning from ``start`` asking permission in advance if it's not determined.
1111@available ( iOS 15 . 0 , watchOS 7 . 0 , * )
1212public final class LocationManagerAsync : NSObject , CLLocationManagerDelegate , ILocationManagerAsync {
13-
1413
1514 // MARK: - Private properties
1615
@@ -36,18 +35,8 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
3635 }
3736 }
3837
39- // Authorization
40-
41- /// Continuation to get permission if status is not defined
42- private var permissioning : Permissioning ?
43-
44- /// Current status
45- private var status : CLAuthorizationStatus
46-
47- /// Check if status is determined
48- private var isDetermined : Bool {
49- status != . notDetermined
50- }
38+ /// Authorization Permission helper
39+ private var permission : Permission
5140
5241 // MARK: - Life circle
5342
@@ -64,9 +53,8 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
6453
6554 override init ( ) {
6655
67- manager = CLLocationManager ( )
68-
69- status = manager. authorizationStatus
56+ manager = . init( )
57+ permission = . init( status: manager. authorizationStatus)
7058
7159 super. init ( )
7260
@@ -77,7 +65,7 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
7765 /// Check status and get stream of async data Throw an error ``LocationManagerErrors`` if permission is not granted
7866 public var start : AsyncThrowingStream < CLLocation , Error > {
7967 get async throws {
80- if await getPermission {
68+ if await permission . isGranted ( for : manager ) {
8169 #if DEBUG
8270 print ( " start " )
8371 #endif
@@ -89,7 +77,6 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
8977
9078 /// Stop streaming
9179 public func stop( ) {
92-
9380 stream = nil
9481 manager. stopUpdatingLocation ( )
9582
@@ -99,37 +86,6 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
9986 }
10087
10188 // MARK: - Private
102-
103- // Authorization
104-
105- /// Get status asynchronously and check is it authorized to start getting the stream of locations
106- private var getPermission : Bool {
107- get async {
108- let status = await requestPermission ( )
109- return isAuthorized ( status)
110- }
111- }
112-
113- /// Check permission status
114- /// - Parameter status: Status for checking
115- /// - Returns: Return `True` if is allowed
116- private func isAuthorized( _ status : CLAuthorizationStatus ) -> Bool {
117- [ CLAuthorizationStatus . authorizedWhenInUse, . authorizedAlways] . contains ( status)
118- }
119-
120- /// Request permission
121- /// Don't forget to add in Info "Privacy - Location When In Use Usage Description" something like "Show list of locations"
122- /// - Returns: Permission status
123- private func requestPermission( ) async -> CLAuthorizationStatus {
124- manager. requestWhenInUseAuthorization ( )
125-
126- if isDetermined{ return status }
127-
128- /// Suspension point until we get permission from the user
129- return await withCheckedContinuation { continuation in
130- permissioning = continuation
131- }
132- }
13389
13490 // Streaming locations
13591
@@ -180,8 +136,7 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
180136 /// Determine status after the request permission
181137 /// - Parameter manager: Location manager
182138 public func locationManagerDidChangeAuthorization( _ manager: CLLocationManager ) {
183- status = manager. authorizationStatus
184- permissioning? . resume ( returning: status)
139+ permission. locationManagerDidChangeAuthorization ( manager)
185140 }
186141}
187142
@@ -190,5 +145,3 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate, IL
190145fileprivate typealias Termination = AsyncThrowingStream < CLLocation , Error > . Continuation . Termination
191146
192147fileprivate typealias Streaming = AsyncThrowingStream < CLLocation , Error > . Continuation
193-
194- fileprivate typealias Permissioning = CheckedContinuation < CLAuthorizationStatus , Never >
0 commit comments