Skip to content

Commit 2a7c161

Browse files
committed
Check app clip
1 parent 291e6ae commit 2a7c161

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

Sources/AsyncLocationKit/AsyncLocationManager.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public final class AsyncLocationManager {
4545
locationManager.desiredAccuracy = desiredAccuracy.convertingAccuracy
4646
}
4747

48+
public init(locationManager: CLLocationManager, desiredAccuracy: LocationAccuracy) {
49+
self.locationManager = locationManager
50+
self.locationManager.delegate = locationDelegate
51+
self.locationManager.desiredAccuracy = desiredAccuracy.convertingAccuracy
52+
proxyDelegate = AsyncDelegateProxy()
53+
locationDelegate = LocationDelegate(delegateProxy: proxyDelegate)
54+
}
55+
4856
public convenience init(desiredAccuracy: LocationAccuracy) {
4957
self.init()
5058
self.desiredAccuracy = desiredAccuracy
@@ -62,6 +70,7 @@ public final class AsyncLocationManager {
6270
locationManager.desiredAccuracy = newAccuracy.convertingAccuracy
6371
}
6472

73+
@available(*, deprecated, message: "Use new function requestPermission(with:)")
6574
public func requestAuthorizationWhenInUse() async -> CLAuthorizationStatus {
6675
let authorizationPerformer = RequestAuthorizationPerformer()
6776
return await withTaskCancellationHandler {
@@ -80,6 +89,8 @@ public final class AsyncLocationManager {
8089
}
8190
}
8291

92+
#if !APPCLIP
93+
@available(*, deprecated, message: "Use new function requestPermission(with:)")
8394
public func requestAuthorizationAlways() async -> CLAuthorizationStatus {
8495
let authorizationPerformer = RequestAuthorizationPerformer()
8596
return await withTaskCancellationHandler {
@@ -96,6 +107,20 @@ public final class AsyncLocationManager {
96107
}
97108
}
98109
}
110+
#endif
111+
112+
public func requestPermission(with permissionType: LocationPermission) async -> CLAuthorizationStatus {
113+
switch permissionType {
114+
case .always:
115+
#if APPCLIP
116+
return await requestAuthorizationWhenInUse()
117+
#else
118+
return await requestAuthorizationAlways()
119+
#endif
120+
case .whenInUsage:
121+
return await requestAuthorizationWhenInUse()
122+
}
123+
}
99124

100125
public func startUpdatingLocation() async -> LocationStream {
101126
let monitoringPerformer = MonitoringUpdateLocationPerformer()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Pavel Grechikhin on 29.10.2022.
6+
//
7+
8+
import Foundation
9+
10+
public enum LocationPermission {
11+
case always
12+
case whenInUsage
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
import XCTest
2+
import CoreLocation
23
@testable import AsyncLocationKit
34

45
final class AsyncLocationKitTests: XCTestCase {
6+
let locationManager = AsyncLocationManager(locationManager: MockLocationManager(), desiredAccuracy: .bestAccuracy)
7+
8+
func testRequestLocation() async {
9+
do {
10+
let location = try await locationManager.requestLocation()
11+
12+
switch location {
13+
case .didUpdateLocations(let locations):
14+
print(locations)
15+
XCTAssert(true)
16+
default:
17+
XCTAssert(false, "Something went wrong")
18+
}
19+
20+
} catch {
21+
XCTAssert(false, error.localizedDescription)
22+
}
23+
}
524
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Pavel Grechikhin on 29.10.2022.
6+
//
7+
8+
import Foundation
9+
import CoreLocation
10+
11+
class MockLocationManager: CLLocationManager {
12+
override var location: CLLocation? {
13+
return CLLocation(latitude: 100, longitude: 200)
14+
}
15+
16+
override func requestLocation() {
17+
delegate?.locationManager?(self, didUpdateLocations: [location!])
18+
}
19+
}

0 commit comments

Comments
 (0)