197 lines
6.6 KiB
Swift
197 lines
6.6 KiB
Swift
//
|
|
// Copyright (c) 2023 Touch Instinct
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the Software), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
//
|
|
|
|
import CoreLocation
|
|
import TILogging
|
|
|
|
open class UserLocationFetcher: NSObject, CLLocationManagerDelegate {
|
|
public enum AccuracyRequest {
|
|
case `default`
|
|
case fullAccuracy(purposeKey: String)
|
|
}
|
|
|
|
public enum Failure: Error {
|
|
case restricted
|
|
case denied
|
|
case fullAccuracyDenied
|
|
}
|
|
|
|
public typealias OnAuthSuccessCallback = (CLLocationManager) -> Void
|
|
public typealias OnAuthFailureCallback = (Failure) -> Void
|
|
|
|
public typealias OnLocationCallback = (CLLocation) -> Void
|
|
public typealias OnLocationFetchFailureCallback = (Error) -> Void
|
|
|
|
public var locationManager: CLLocationManager
|
|
public var accuracyRequest: AccuracyRequest
|
|
|
|
public var authorized: Bool {
|
|
if #available(iOS 14.0, *) {
|
|
return isAuthorized(status: locationManager.authorizationStatus)
|
|
} else {
|
|
return isAuthorized(status: CLLocationManager.authorizationStatus())
|
|
}
|
|
}
|
|
|
|
var authorizedFullAccuracy: Bool {
|
|
if #available(iOS 14.0, *) {
|
|
switch locationManager.accuracyAuthorization {
|
|
case .fullAccuracy:
|
|
return true
|
|
|
|
case .reducedAccuracy:
|
|
return false
|
|
|
|
@unknown default:
|
|
assertionFailure("Unimplemented accuracyAuthorization case: \(locationManager.accuracyAuthorization)")
|
|
return true
|
|
}
|
|
} else {
|
|
return true
|
|
}
|
|
}
|
|
|
|
public var authorizedAccuracy: Bool {
|
|
switch accuracyRequest {
|
|
case .default:
|
|
return true
|
|
|
|
case .fullAccuracy:
|
|
return authorizedFullAccuracy
|
|
}
|
|
}
|
|
|
|
public var authSuccessCallback: OnAuthSuccessCallback
|
|
public var authFailureCallback: OnAuthFailureCallback?
|
|
|
|
public var onLocationCallback: OnLocationCallback?
|
|
public var onLocationFetchFailureCallback: OnLocationFetchFailureCallback?
|
|
|
|
public var logger: ErrorLogger = TIMapLogger(category: "UserLocationFetcher")
|
|
|
|
public init(locationManager: CLLocationManager = CLLocationManager(),
|
|
accuracyRequest: AccuracyRequest = .default,
|
|
onSuccess: @escaping OnAuthSuccessCallback = { $0.requestLocation() },
|
|
onFailure: OnAuthFailureCallback? = nil,
|
|
onLocationFetchFailureCallback: OnLocationFetchFailureCallback? = nil,
|
|
onLocationCallback: OnLocationCallback? = nil) {
|
|
|
|
self.locationManager = locationManager
|
|
self.accuracyRequest = accuracyRequest
|
|
|
|
self.authSuccessCallback = onSuccess
|
|
self.authFailureCallback = onFailure
|
|
|
|
self.onLocationCallback = onLocationCallback
|
|
self.onLocationFetchFailureCallback = onLocationFetchFailureCallback
|
|
|
|
super.init()
|
|
}
|
|
|
|
open func requestLocationUpdates(onlyIfHasAccess: Bool = false) {
|
|
if authorized && authorizedAccuracy || !onlyIfHasAccess {
|
|
locationManager.delegate = self
|
|
}
|
|
}
|
|
|
|
open func requestAuth(for manager: CLLocationManager) {
|
|
manager.requestWhenInUseAuthorization()
|
|
}
|
|
|
|
open func isAuthorized(status: CLAuthorizationStatus) -> Bool {
|
|
switch status {
|
|
case .authorizedWhenInUse, .authorizedAlways:
|
|
return true
|
|
|
|
case .restricted, .denied:
|
|
return false
|
|
|
|
case .notDetermined:
|
|
return false
|
|
|
|
@unknown default:
|
|
assertionFailure("Unimplemented authorizationStatus case: \(status))")
|
|
return true
|
|
}
|
|
}
|
|
|
|
// MARK: - CLLocationManagerDelegate
|
|
|
|
open func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
|
|
switch status {
|
|
case .notDetermined:
|
|
requestAuth(for: manager)
|
|
|
|
case .restricted:
|
|
authFailureCallback?(.restricted)
|
|
|
|
case .denied:
|
|
authFailureCallback?(.denied)
|
|
|
|
case .authorizedAlways, .authorizedWhenInUse:
|
|
handleSuccessAuthorization(with: status, for: manager)
|
|
|
|
@unknown default:
|
|
assertionFailure("Unimplemented authorizationStatus case: \(status))")
|
|
authSuccessCallback(manager)
|
|
}
|
|
}
|
|
|
|
open func handleSuccessAuthorization(with status: CLAuthorizationStatus, for manager: CLLocationManager) {
|
|
if authorizedFullAccuracy {
|
|
authSuccessCallback(manager)
|
|
} else {
|
|
switch accuracyRequest {
|
|
case let .fullAccuracy(purposeKey):
|
|
if #available(iOS 14.0, *) {
|
|
locationManager.requestTemporaryFullAccuracyAuthorization(withPurposeKey: purposeKey) { [weak self] in
|
|
if $0 != nil {
|
|
self?.authFailureCallback?(.fullAccuracyDenied)
|
|
} else {
|
|
self?.authSuccessCallback(manager)
|
|
}
|
|
}
|
|
} else {
|
|
authSuccessCallback(manager)
|
|
}
|
|
|
|
case .default:
|
|
authSuccessCallback(manager)
|
|
}
|
|
}
|
|
}
|
|
|
|
open func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
|
guard let onLocationCallback else {
|
|
return
|
|
}
|
|
|
|
locations.forEach(onLocationCallback)
|
|
}
|
|
|
|
open func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
|
onLocationFetchFailureCallback?(error)
|
|
|
|
logger.log(error: error, file: #file, line: #line)
|
|
}
|
|
}
|