实现订单 Tab 并接入扫码核销流程
This commit is contained in:
55
suixinkan/Features/Orders/Utils/OrderLocationProvider.swift
Normal file
55
suixinkan/Features/Orders/Utils/OrderLocationProvider.swift
Normal file
@ -0,0 +1,55 @@
|
||||
//
|
||||
// OrderLocationProvider.swift
|
||||
// suixinkan
|
||||
//
|
||||
|
||||
import CoreLocation
|
||||
|
||||
/// 一次性定位,用于多点位核销拉取打卡点。
|
||||
final class OrderLocationProvider: NSObject, CLLocationManagerDelegate {
|
||||
private let manager = CLLocationManager()
|
||||
private var continuation: ((Result<CLLocationCoordinate2D, Error>) -> Void)?
|
||||
|
||||
override init() {
|
||||
super.init()
|
||||
manager.delegate = self
|
||||
manager.desiredAccuracy = kCLLocationAccuracyBest
|
||||
}
|
||||
|
||||
func requestCoordinate() async throws -> CLLocationCoordinate2D {
|
||||
let status = manager.authorizationStatus
|
||||
if status == .notDetermined {
|
||||
manager.requestWhenInUseAuthorization()
|
||||
}
|
||||
guard status == .authorizedWhenInUse || status == .authorizedAlways || manager.authorizationStatus == .authorizedWhenInUse else {
|
||||
throw OrderLocationError.permissionDenied
|
||||
}
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
self.continuation = { result in
|
||||
continuation.resume(with: result)
|
||||
}
|
||||
manager.requestLocation()
|
||||
}
|
||||
}
|
||||
|
||||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||
guard let location = locations.last else { return }
|
||||
continuation?(.success(location.coordinate))
|
||||
continuation = nil
|
||||
}
|
||||
|
||||
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
|
||||
continuation?(.failure(error))
|
||||
continuation = nil
|
||||
}
|
||||
}
|
||||
|
||||
enum OrderLocationError: LocalizedError {
|
||||
case permissionDenied
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .permissionDenied: "缺少定位权限,功能无法进行"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user