Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CoreLocation returns .notDetermined before Pop Up Button is pressed #380

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ let package = Package(
products: [
.library(
name: "SwiftLocation",
targets: ["SwiftLocation"]),
targets: ["SwiftLocation"])
],
targets: [
.target(
name: "SwiftLocation"),
.testTarget(
name: "SwiftLocationTests",
dependencies: ["SwiftLocation"]),
dependencies: ["SwiftLocation"])
]
)
18 changes: 9 additions & 9 deletions Sources/SwiftLocation/Async Tasks/AccuracyAuthorization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,43 @@ import Foundation
import CoreLocation

extension Tasks {

public final class AccuracyAuthorization: AnyTask {

// MARK: - Support Structures

/// Stream produced by the task.
public typealias Stream = AsyncStream<StreamEvent>

/// The event produced by the stream.
public enum StreamEvent: CustomStringConvertible, Equatable {

/// A new change in accuracy level authorization has been captured.
case didUpdateAccuracyAuthorization(_ accuracyAuthorization: CLAccuracyAuthorization)

/// Return the accuracy authorization of the event
var accuracyAuthorization: CLAccuracyAuthorization {
switch self {
case let .didUpdateAccuracyAuthorization(accuracyAuthorization):
return accuracyAuthorization
}
}

public var description: String {
switch self {
case .didUpdateAccuracyAuthorization:
return "didUpdateAccuracyAuthorization"
}
}

}

// MARK: - Public Properties

public let uuid = UUID()
public var stream: Stream.Continuation?
public var cancellable: CancellableTask?

// MARK: - Functions

public func receivedLocationManagerEvent(_ event: LocationManagerBridgeEvent) {
Expand All @@ -75,5 +75,5 @@ extension Tasks {
}
}
}

}
31 changes: 16 additions & 15 deletions Sources/SwiftLocation/Async Tasks/AccuracyPermission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ import Foundation
import CoreLocation

extension Tasks {

public final class AccuracyPermission: AnyTask {

// MARK: - Support Structures

public typealias Continuation = CheckedContinuation<CLAccuracyAuthorization, Error>

// MARK: - Public Properties

public let uuid = UUID()
public var cancellable: CancellableTask?
var continuation: Continuation?

// MARK: - Private Properties

private weak var instance: Location?

// MARK: - Initialization

init(instance: Location) {
self.instance = instance
}

// MARK: - Functions

public func receivedLocationManagerEvent(_ event: LocationManagerBridgeEvent) {
switch event {
case .didChangeAccuracyAuthorization(let auth):
Expand All @@ -60,36 +60,37 @@ extension Tasks {
break
}
}


@MainActor
func requestTemporaryPermission(purposeKey: String) async throws -> CLAccuracyAuthorization {
try await withCheckedThrowingContinuation { continuation in
guard let instance = self.instance else { return }

guard instance.locationManager.locationServicesEnabled() else {
continuation.resume(throwing: LocationErrors.locationServicesDisabled)
return
}

let authorizationStatus = instance.authorizationStatus
guard authorizationStatus != .notDetermined else {
continuation.resume(throwing: LocationErrors.authorizationRequired)
return
}

let accuracyAuthorization = instance.accuracyAuthorization
guard accuracyAuthorization != .fullAccuracy else {
continuation.resume(with: .success(accuracyAuthorization))
return
}

self.continuation = continuation
instance.asyncBridge.add(task: self)
instance.locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: purposeKey) { error in
if let error {
continuation.resume(throwing: error)
return
}

// If the user chooses reduced accuracy, the didChangeAuthorization delegate method will not called.
if instance.locationManager.accuracyAuthorization == .reducedAccuracy {
let accuracyAuthorization = instance.accuracyAuthorization
Expand All @@ -98,7 +99,7 @@ extension Tasks {
}
}
}

}

}
19 changes: 10 additions & 9 deletions Sources/SwiftLocation/Async Tasks/AnyTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,33 @@ import Foundation

public enum Tasks { }

@MainActor
public protocol AnyTask: AnyObject {

var cancellable: CancellableTask? { get set }
var uuid: UUID { get }
var taskType: ObjectIdentifier { get }

func receivedLocationManagerEvent(_ event: LocationManagerBridgeEvent)
func didCancelled()
func willStart()

}

public extension AnyTask {

var taskType: ObjectIdentifier {
ObjectIdentifier(Self.self)
}

func didCancelled() { }
func willStart() { }

}

}

@MainActor
public protocol CancellableTask: AnyObject {

func cancel(task: any AnyTask)

}
16 changes: 8 additions & 8 deletions Sources/SwiftLocation/Async Tasks/Authorization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,36 @@ import Foundation
import CoreLocation

extension Tasks {

public final class Authorization: AnyTask {

// MARK: - Support Structures

/// Stream produced by the task.
public typealias Stream = AsyncStream<StreamEvent>

/// The event produced by the stream.
public enum StreamEvent {

/// Authorization did change with a new value
case didChangeAuthorization(_ status: CLAuthorizationStatus)

/// The current status of the authorization.
public var authorizationStatus: CLAuthorizationStatus {
switch self {
case let .didChangeAuthorization(status):
return status
}
}

}

// MARK: - Public Properties

public let uuid = UUID()
public var stream: Stream.Continuation?
public var cancellable: CancellableTask?

// MARK: - Functions

public func receivedLocationManagerEvent(_ event: LocationManagerBridgeEvent) {
Expand All @@ -68,5 +68,5 @@ extension Tasks {
}
}
}

}
26 changes: 13 additions & 13 deletions Sources/SwiftLocation/Async Tasks/BeaconMonitoring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@ import CoreLocation

#if !os(watchOS) && !os(tvOS)
extension Tasks {

public final class BeaconMonitoring: AnyTask {

// MARK: - Support Structures

/// The event produced by the stream.
public typealias Stream = AsyncStream<StreamEvent>

/// The event produced by the stream.
public enum StreamEvent: CustomStringConvertible, Equatable {
case didRange(beacons: [CLBeacon], constraint: CLBeaconIdentityConstraint)
case didFailRanginFor(constraint: CLBeaconIdentityConstraint, error: Error)

public static func == (lhs: Tasks.BeaconMonitoring.StreamEvent, rhs: Tasks.BeaconMonitoring.StreamEvent) -> Bool {
switch (lhs, rhs) {
case (let .didRange(b1, _), let .didRange(b2, _)):
return b1 == b2

case (let .didFailRanginFor(c1, _), let .didFailRanginFor(c2, _)):
return c1 == c2

default:
return false

}
}

public var description: String {
switch self {
case .didFailRanginFor:
Expand All @@ -64,20 +64,20 @@ extension Tasks {
}
}
}

// MARK: - Public Properties

public let uuid = UUID()
public var stream: Stream.Continuation?
public var cancellable: CancellableTask?
public private(set) var satisfying: CLBeaconIdentityConstraint

// MARK: - Initialization

init(satisfying: CLBeaconIdentityConstraint) {
self.satisfying = satisfying
}

// MARK: - Functions

public func receivedLocationManagerEvent(_ event: LocationManagerBridgeEvent) {
Expand All @@ -90,8 +90,8 @@ extension Tasks {
break
}
}

}

}
#endif
Loading