Location fixes

This commit is contained in:
Selim Mustafaev 2020-07-29 00:33:47 +03:00
parent 87843ec692
commit 224c9327e5
2 changed files with 56 additions and 11 deletions

View File

@ -82,16 +82,32 @@
<BreakpointProxy <BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint"> BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent <BreakpointContent
uuid = "DBCA971D-D424-4108-BA32-882EEF44B5A8" uuid = "664BF878-7362-4887-BFD4-6938FCAB4750"
shouldBeEnabled = "Yes" shouldBeEnabled = "Yes"
ignoreCount = "0" ignoreCount = "0"
continueAfterRunningActions = "No" continueAfterRunningActions = "No"
filePath = "AutoCat/Utils/Location.swift" filePath = "AutoCat/Utils/Location.swift"
startingColumnNumber = "9223372036854775807" startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807"
startingLineNumber = "86" startingLineNumber = "46"
endingLineNumber = "86" endingLineNumber = "46"
landmarkName = "requestLocation()" landmarkName = "locationManager(_:didFailWithError:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "3661DA4A-8777-45F2-A6FD-0F1D3F487FF8"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "AutoCat/Utils/Location.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "40"
endingLineNumber = "40"
landmarkName = "locationManager(_:didUpdateLocations:)"
landmarkType = "7"> landmarkType = "7">
</BreakpointContent> </BreakpointContent>
</BreakpointProxy> </BreakpointProxy>

View File

@ -5,10 +5,17 @@ import CoreLocation
class RxLocationManagerDelegateProxy: DelegateProxy<CLLocationManager, CLLocationManagerDelegate>, DelegateProxyType, CLLocationManagerDelegate { class RxLocationManagerDelegateProxy: DelegateProxy<CLLocationManager, CLLocationManagerDelegate>, DelegateProxyType, CLLocationManagerDelegate {
let authSubject = PublishSubject<CLAuthorizationStatus>()
let locationSubject = PublishSubject<CLLocation>()
init(locationManager: ParentObject) { init(locationManager: ParentObject) {
super.init(parentObject: locationManager, delegateProxy: RxLocationManagerDelegateProxy.self) super.init(parentObject: locationManager, delegateProxy: RxLocationManagerDelegateProxy.self)
} }
deinit {
print("deinit")
}
// MARK: - DelegateProxyType // MARK: - DelegateProxyType
static func registerKnownImplementations() { static func registerKnownImplementations() {
@ -22,8 +29,25 @@ class RxLocationManagerDelegateProxy: DelegateProxy<CLLocationManager, CLLocatio
static func setCurrentDelegate(_ delegate: CLLocationManagerDelegate?, to object: CLLocationManager) { static func setCurrentDelegate(_ delegate: CLLocationManagerDelegate?, to object: CLLocationManager) {
object.delegate = delegate object.delegate = delegate
} }
// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.authSubject.onNext(status)
} }
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
self.locationSubject.onNext(location)
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
}
/*
extension Reactive where Base: CLLocationManager { extension Reactive where Base: CLLocationManager {
var delegate: DelegateProxy<CLLocationManager, CLLocationManagerDelegate> { var delegate: DelegateProxy<CLLocationManager, CLLocationManagerDelegate> {
return RxLocationManagerDelegateProxy.proxy(for: base) return RxLocationManagerDelegateProxy.proxy(for: base)
@ -51,9 +75,14 @@ extension Reactive where Base: CLLocationManager {
} }
} }
} }
*/
class LocationManager { 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 let bag = DisposeBag()
private static func checkPermissions() -> Single<Void> { private static func checkPermissions() -> Single<Void> {
@ -64,7 +93,7 @@ class LocationManager {
break break
case .notDetermined: case .notDetermined:
self.manager.requestWhenInUseAuthorization() 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 { if let status = result, status == .authorizedWhenInUse {
observer(.success(())) observer(.success(()))
} else { } else {
@ -81,11 +110,11 @@ class LocationManager {
} }
private static func requestLocation() -> Single<VehicleEvent> { private static func requestLocation() -> Single<VehicleEvent> {
return self.manager.rx.didUpdateLocations.take(1).asSingle().do(onSubscribed: { let single = RxLocationManagerDelegateProxy.proxy(for: self.manager).locationSubject.take(1).asSingle().map { location in
DispatchQueue.main.async { return VehicleEvent(lat: location.coordinate.latitude, lon: location.coordinate.longitude, speed: location.speed, dir: location.course)
self.manager.requestLocation()
} }
}) self.manager.requestLocation()
return single
} }
static func requestCurrentLocation() -> Single<VehicleEvent> { static func requestCurrentLocation() -> Single<VehicleEvent> {