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
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "DBCA971D-D424-4108-BA32-882EEF44B5A8"
uuid = "664BF878-7362-4887-BFD4-6938FCAB4750"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "AutoCat/Utils/Location.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "86"
endingLineNumber = "86"
landmarkName = "requestLocation()"
startingLineNumber = "46"
endingLineNumber = "46"
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">
</BreakpointContent>
</BreakpointProxy>

View File

@ -5,10 +5,17 @@ import CoreLocation
class RxLocationManagerDelegateProxy: DelegateProxy<CLLocationManager, CLLocationManagerDelegate>, DelegateProxyType, CLLocationManagerDelegate {
let authSubject = PublishSubject<CLAuthorizationStatus>()
let locationSubject = PublishSubject<CLLocation>()
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<CLLocationManager, CLLocatio
static func setCurrentDelegate(_ delegate: CLLocationManagerDelegate?, to object: CLLocationManager) {
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 {
var delegate: DelegateProxy<CLLocationManager, CLLocationManagerDelegate> {
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<Void> {
@ -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<VehicleEvent> {
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<VehicleEvent> {