diff --git a/AsyncLocationKit.podspec b/AsyncLocationKit.podspec index 796c437..daa33ac 100644 --- a/AsyncLocationKit.podspec +++ b/AsyncLocationKit.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = 'AsyncLocationKit' s.module_name = 'AsyncLocationKit' - s.version = '1.5.3' + s.version = '1.5.4' s.summary = '📍async/await CoreLocation' s.homepage = 'https://github.com/AsyncSwift/AsyncLocationKit' s.license = 'MIT' diff --git a/README.md b/README.md index b770d0c..d3f7033 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,13 @@ Wrapper for Apple `CoreLocation` framework with new Concurency Model. No more `d ##### SPM ```swift dependencies: [ - .package(url: "https://github.com/AsyncSwift/AsyncLocationKit.git", .upToNextMinor(from: "1.5.0")) + .package(url: "https://github.com/AsyncSwift/AsyncLocationKit.git", .upToNextMinor(from: "1.5.4")) ] ``` #### Cocoapods ``` -pod 'AsyncLocationKit', :git => 'https://github.com/AsyncSwift/AsyncLocationKit.git', :tag => '1.5.2' +pod 'AsyncLocationKit', :git => 'https://github.com/AsyncSwift/AsyncLocationKit.git', :tag => '1.5.4' ``` diff --git a/Sources/AsyncLocationKit/AsyncLocationManager.swift b/Sources/AsyncLocationKit/AsyncLocationManager.swift index c327886..be62552 100644 --- a/Sources/AsyncLocationKit/AsyncLocationManager.swift +++ b/Sources/AsyncLocationKit/AsyncLocationManager.swift @@ -45,6 +45,14 @@ public final class AsyncLocationManager { locationManager.desiredAccuracy = desiredAccuracy.convertingAccuracy } + public init(locationManager: CLLocationManager, desiredAccuracy: LocationAccuracy) { + self.locationManager = locationManager + self.locationManager.delegate = locationDelegate + self.locationManager.desiredAccuracy = desiredAccuracy.convertingAccuracy + proxyDelegate = AsyncDelegateProxy() + locationDelegate = LocationDelegate(delegateProxy: proxyDelegate) + } + public convenience init(desiredAccuracy: LocationAccuracy) { self.init() self.desiredAccuracy = desiredAccuracy @@ -62,6 +70,7 @@ public final class AsyncLocationManager { locationManager.desiredAccuracy = newAccuracy.convertingAccuracy } + @available(*, deprecated, message: "Use new function requestPermission(with:)") public func requestAuthorizationWhenInUse() async -> CLAuthorizationStatus { let authorizationPerformer = RequestAuthorizationPerformer() return await withTaskCancellationHandler { @@ -80,6 +89,8 @@ public final class AsyncLocationManager { } } +#if !APPCLIP + @available(*, deprecated, message: "Use new function requestPermission(with:)") public func requestAuthorizationAlways() async -> CLAuthorizationStatus { let authorizationPerformer = RequestAuthorizationPerformer() return await withTaskCancellationHandler { @@ -96,6 +107,20 @@ public final class AsyncLocationManager { } } } +#endif + + public func requestPermission(with permissionType: LocationPermission) async -> CLAuthorizationStatus { + switch permissionType { + case .always: + #if APPCLIP + return await requestAuthorizationWhenInUse() + #else + return await requestAuthorizationAlways() + #endif + case .whenInUsage: + return await requestAuthorizationWhenInUse() + } + } public func startUpdatingLocation() async -> LocationStream { let monitoringPerformer = MonitoringUpdateLocationPerformer() diff --git a/Sources/AsyncLocationKit/Settings/LocationPermission.swift b/Sources/AsyncLocationKit/Settings/LocationPermission.swift new file mode 100644 index 0000000..88497c7 --- /dev/null +++ b/Sources/AsyncLocationKit/Settings/LocationPermission.swift @@ -0,0 +1,13 @@ +// +// File.swift +// +// +// Created by Pavel Grechikhin on 29.10.2022. +// + +import Foundation + +public enum LocationPermission { + case always + case whenInUsage +} diff --git a/Tests/AsyncLocationKitTests/AsyncLocationKitTests.swift b/Tests/AsyncLocationKitTests/AsyncLocationKitTests.swift index 4b5c272..d9e3507 100644 --- a/Tests/AsyncLocationKitTests/AsyncLocationKitTests.swift +++ b/Tests/AsyncLocationKitTests/AsyncLocationKitTests.swift @@ -1,5 +1,24 @@ import XCTest +import CoreLocation @testable import AsyncLocationKit final class AsyncLocationKitTests: XCTestCase { + let locationManager = AsyncLocationManager(locationManager: MockLocationManager(), desiredAccuracy: .bestAccuracy) + + func testRequestLocation() async { + do { + let location = try await locationManager.requestLocation() + + switch location { + case .didUpdateLocations(let locations): + print(locations) + XCTAssert(true) + default: + XCTAssert(false, "Something went wrong") + } + + } catch { + XCTAssert(false, error.localizedDescription) + } + } } diff --git a/Tests/AsyncLocationKitTests/LocationManager.swift b/Tests/AsyncLocationKitTests/LocationManager.swift new file mode 100644 index 0000000..d7d33c2 --- /dev/null +++ b/Tests/AsyncLocationKitTests/LocationManager.swift @@ -0,0 +1,19 @@ +// +// File.swift +// +// +// Created by Pavel Grechikhin on 29.10.2022. +// + +import Foundation +import CoreLocation + +class MockLocationManager: CLLocationManager { + override var location: CLLocation? { + return CLLocation(latitude: 100, longitude: 200) + } + + override func requestLocation() { + delegate?.locationManager?(self, didUpdateLocations: [location!]) + } +}