From 224c9327e5597c935a840c91e4e3bb4a97477b25 Mon Sep 17 00:00:00 2001 From: Selim Mustafaev Date: Wed, 29 Jul 2020 00:33:47 +0300 Subject: [PATCH] Location fixes --- .../xcdebugger/Breakpoints_v2.xcbkptlist | 24 +++++++++-- AutoCat/Utils/Location.swift | 43 ++++++++++++++++--- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index da8c074..7723b83 100644 --- a/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/AutoCat.xcodeproj/xcuserdata/selim.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -82,16 +82,32 @@ + + + + diff --git a/AutoCat/Utils/Location.swift b/AutoCat/Utils/Location.swift index 05e4953..e5cba4a 100644 --- a/AutoCat/Utils/Location.swift +++ b/AutoCat/Utils/Location.swift @@ -4,11 +4,18 @@ import RxCocoa import CoreLocation class RxLocationManagerDelegateProxy: DelegateProxy, DelegateProxyType, CLLocationManagerDelegate { + + let authSubject = PublishSubject() + let locationSubject = PublishSubject() init(locationManager: ParentObject) { super.init(parentObject: locationManager, delegateProxy: RxLocationManagerDelegateProxy.self) } + deinit { + print("deinit") + } + // MARK: - DelegateProxyType static func registerKnownImplementations() { @@ -22,8 +29,25 @@ class RxLocationManagerDelegateProxy: DelegateProxy { return RxLocationManagerDelegateProxy.proxy(for: base) @@ -51,9 +75,14 @@ extension Reactive where Base: CLLocationManager { } } } + */ class LocationManager { - private static let manager = CLLocationManager() + private static let manager: CLLocationManager = { + let mgr = CLLocationManager() + mgr.desiredAccuracy = kCLLocationAccuracyBest + return mgr + }() private static let bag = DisposeBag() private static func checkPermissions() -> Single { @@ -64,7 +93,7 @@ class LocationManager { break case .notDetermined: self.manager.requestWhenInUseAuthorization() - _ = self.manager.rx.didChangeAuthorization.skip(1).first().subscribe(onSuccess: { result in + _ = RxLocationManagerDelegateProxy.proxy(for: self.manager).authSubject.skip(1).first().subscribe(onSuccess: { result in if let status = result, status == .authorizedWhenInUse { observer(.success(())) } else { @@ -81,11 +110,11 @@ class LocationManager { } private static func requestLocation() -> Single { - return self.manager.rx.didUpdateLocations.take(1).asSingle().do(onSubscribed: { - DispatchQueue.main.async { - self.manager.requestLocation() - } - }) + let single = RxLocationManagerDelegateProxy.proxy(for: self.manager).locationSubject.take(1).asSingle().map { location in + return VehicleEvent(lat: location.coordinate.latitude, lon: location.coordinate.longitude, speed: location.speed, dir: location.course) + } + self.manager.requestLocation() + return single } static func requestCurrentLocation() -> Single {