@@ -18,7 +18,7 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate{
1818 get throws {
1919 try checkStatus ( )
2020 return AsyncStream ( CLLocation . self) { continuation in
21- start ( with: continuation)
21+ streaming ( with: continuation)
2222 }
2323 }
2424 }
@@ -67,7 +67,33 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate{
6767
6868 }
6969
70- // MARK: - API
70+ // MARK: - API
71+
72+ /// Check status and get stream of async data
73+ public var start : AsyncStream < CLLocation > ? {
74+ get async throws {
75+ if await getStatus {
76+ return try locations
77+ }
78+ throw LocationManagerErrors. accessIsNotAuthorized
79+ }
80+ }
81+
82+ /// Stop updating
83+ public func stop( ) {
84+ stream = nil
85+ manager. stopUpdatingLocation ( )
86+ }
87+
88+ // MARK: - Private
89+
90+ /// Get status
91+ private var getStatus : Bool {
92+ get async {
93+ let isAuthorized = await requestPermission ( )
94+ return check ( status: isAuthorized)
95+ }
96+ }
7197
7298 /// Request permission
7399 /// Don't forget to add in Info "Privacy - Location When In Use Usage Description" something like "Show list of locations"
@@ -84,14 +110,6 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate{
84110
85111 }
86112
87- /// Stop updating
88- public func stop( ) {
89- stream = nil
90- manager. stopUpdatingLocation ( )
91- }
92-
93- // MARK: - Private
94-
95113 /// Set manager's properties
96114 /// - Parameter accuracy: Desired accuracy
97115 private func updateSettings( accuracy : CLLocationAccuracy ? ) {
@@ -100,7 +118,7 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate{
100118 }
101119
102120 /// Start updating
103- private func start ( with continuation : StreamType ) {
121+ private func streaming ( with continuation : StreamType ) {
104122 stream = continuation
105123 manager. startUpdatingLocation ( )
106124 }
@@ -119,6 +137,10 @@ public final class LocationManagerAsync: NSObject, CLLocationManagerDelegate{
119137 }
120138 }
121139
140+ private func check( status : CLAuthorizationStatus ) -> Bool {
141+ [ CLAuthorizationStatus . authorizedWhenInUse, . authorizedAlways] . contains ( status)
142+ }
143+
122144 // MARK: - Delegate
123145
124146 /// Pass locations into the async stream
0 commit comments