Skip to content

Commit f800941

Browse files
committed
refactoring
1 parent 7dbd49f commit f800941

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

Sources/d3-async-location/LocationManagerAsync.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Sources/d3-async-location/viewmodel/LMViewModel.swift

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,8 @@ public final class LMViewModel: ILocationManagerViewModel{
2121

2222
/// Async locations manager
2323
private let manager : LocationManagerAsync
24-
25-
/// Check status and get stream of async data
26-
private var getStream : AsyncStream<CLLocation>?{
27-
get async throws {
28-
if await getStatus{
29-
return try manager.locations
30-
}
31-
throw LocationManagerErrors.accessIsNotAuthorized
32-
}
33-
}
34-
35-
/// Get status
36-
private var getStatus: Bool{
37-
get async{
38-
let isAuthorized = await manager.requestPermission()
39-
return check(status: isAuthorized)
40-
}
41-
}
42-
24+
25+
4326
// MARK: - Life circle
4427

4528
public init(accuracy : CLLocationAccuracy? = nil){
@@ -57,7 +40,7 @@ public final class LMViewModel: ILocationManagerViewModel{
5740

5841
/// Start streaming locations
5942
public func start() async throws{
60-
if let stream = try await getStream{
43+
if let stream = try await manager.start{
6144
for await coordinate in stream{
6245
await update(coordinate: coordinate)
6346
}
@@ -76,9 +59,5 @@ public final class LMViewModel: ILocationManagerViewModel{
7659
private func update(coordinate : CLLocation) {
7760
locations.append(coordinate)
7861
}
79-
80-
private func check(status : CLAuthorizationStatus) -> Bool{
81-
[CLAuthorizationStatus.authorizedWhenInUse, .authorizedAlways].contains(status)
82-
}
8362

8463
}

0 commit comments

Comments
 (0)