Skip to content

Commit cd74800

Browse files
committed
refactoring
1 parent 1a23868 commit cd74800

File tree

2 files changed

+72
-53
lines changed

2 files changed

+72
-53
lines changed

Sources/d3-async-location/LocationManagerAsync.swift

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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, *)
1212
public 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
190145
fileprivate typealias Termination = AsyncThrowingStream<CLLocation, Error>.Continuation.Termination
191146

192147
fileprivate typealias Streaming = AsyncThrowingStream<CLLocation, Error>.Continuation
193-
194-
fileprivate typealias Permissioning = CheckedContinuation<CLAuthorizationStatus,Never>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Permission.swift
3+
//
4+
//
5+
// Created by Igor on 06.02.2023.
6+
//
7+
8+
import CoreLocation
9+
10+
11+
/// Helper class to determine permission to get access for streaming CLLocations
12+
final class Permission{
13+
14+
/// Current status
15+
private var status : CLAuthorizationStatus
16+
17+
/// Continuation to get permission if status is not defined
18+
private var premising : CheckedContinuation<CLAuthorizationStatus, Never>?
19+
20+
/// Check if status is determined
21+
private var isDetermined : Bool{
22+
status != .notDetermined
23+
}
24+
25+
// MARK: - Life circle
26+
27+
init(status: CLAuthorizationStatus){
28+
self.status = status
29+
}
30+
31+
// MARK: - API
32+
33+
/// Get status asynchronously and check is it authorized to start getting the stream of locations
34+
public func isGranted(for manager: CLLocationManager) async -> Bool{
35+
let status = await requestPermission(manager)
36+
return isAuthorized(status)
37+
}
38+
39+
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
40+
status = manager.authorizationStatus
41+
42+
premising?.resume(returning: status)
43+
}
44+
45+
// MARK: - Private methods
46+
47+
/// Check permission status
48+
/// - Parameter status: Status for checking
49+
/// - Returns: Return `True` if is allowed
50+
private func isAuthorized(_ status : CLAuthorizationStatus) -> Bool{
51+
[CLAuthorizationStatus.authorizedWhenInUse, .authorizedAlways].contains(status)
52+
}
53+
54+
private func requestPermission(_ manager : CLLocationManager) async -> CLAuthorizationStatus{
55+
manager.requestWhenInUseAuthorization()
56+
57+
if status != .notDetermined{
58+
return status
59+
}
60+
61+
return await withCheckedContinuation{ continuation in
62+
premising = continuation
63+
}
64+
}
65+
66+
}

0 commit comments

Comments
 (0)