Add device location coordinate update throttling

This commit is contained in:
Ilya Laktyushin 2020-10-29 02:08:32 +04:00
parent 25d0696cd9
commit 3ccf64e63f
2 changed files with 24 additions and 9 deletions

View File

@ -34,8 +34,11 @@ public final class LiveLocationManagerImpl: LiveLocationManager {
} }
private let deviceLocationDisposable = MetaDisposable() private let deviceLocationDisposable = MetaDisposable()
private let updateCoordinateDisposable = MetaDisposable()
private var messagesDisposable: Disposable? private var messagesDisposable: Disposable?
private var deviceLocationPromise = Promise<(CLLocation, Double?)>()
private var broadcastToMessageIds: [MessageId: Int32] = [:] private var broadcastToMessageIds: [MessageId: Int32] = [:]
private var stopMessageIds = Set<MessageId>() private var stopMessageIds = Set<MessageId>()
@ -106,26 +109,36 @@ public final class LiveLocationManagerImpl: LiveLocationManager {
|> deliverOn(self.queue)).start(next: { [weak self] value in |> deliverOn(self.queue)).start(next: { [weak self] value in
if let strongSelf = self { if let strongSelf = self {
if value { if value {
let queue = strongSelf.queue strongSelf.deviceLocationDisposable.set(strongSelf.locationManager.push(mode: .precise, updated: { [weak self] location, heading in
strongSelf.deviceLocationDisposable.set(strongSelf.locationManager.push(mode: .precise, updated: { location, heading in self?.deviceLocationPromise.set(.single((location, heading)))
queue.async {
var effectiveHeading = heading ?? location.course
if location.speed > 1.0 {
effectiveHeading = location.course
}
self?.updateDeviceCoordinate(location.coordinate, accuracyRadius: location.horizontalAccuracy, heading: effectiveHeading)
}
})) }))
} else { } else {
strongSelf.deviceLocationDisposable.set(nil) strongSelf.deviceLocationDisposable.set(nil)
} }
} }
}) })
let throttledDeviceLocation = self.deviceLocationPromise.get()
|> mapToThrottled { next -> Signal<(CLLocation, Double?), NoError> in
return .single(next) |> then(.complete() |> delay(4.0, queue: Queue.concurrentDefaultQueue()))
}
self.updateCoordinateDisposable.set((throttledDeviceLocation
|> deliverOn(self.queue)).start(next: { [weak self] location, heading in
if let strongSelf = self {
var effectiveHeading = heading ?? location.course
if location.speed > 1.0 {
effectiveHeading = location.course
}
strongSelf.updateDeviceCoordinate(location.coordinate, accuracyRadius: location.horizontalAccuracy, heading: effectiveHeading)
}
}))
} }
deinit { deinit {
self.requiredLocationTypeDisposable?.dispose() self.requiredLocationTypeDisposable?.dispose()
self.deviceLocationDisposable.dispose() self.deviceLocationDisposable.dispose()
self.updateCoordinateDisposable.dispose()
self.messagesDisposable?.dispose() self.messagesDisposable?.dispose()
self.editMessageDisposables.dispose() self.editMessageDisposables.dispose()
self.invalidationTimer?.0.invalidate() self.invalidationTimer?.0.invalidate()

View File

@ -305,6 +305,8 @@ public final class LocationViewController: ViewController {
|> deliverOnMainQueue).start(next: { coordinate in |> deliverOnMainQueue).start(next: { coordinate in
params.sendLiveLocation(TelegramMediaMap(coordinate: coordinate, liveBroadcastingTimeout: period)) params.sendLiveLocation(TelegramMediaMap(coordinate: coordinate, liveBroadcastingTimeout: period))
}) })
strongSelf.controllerNode.showAll()
} }
controller.setItemGroups([ controller.setItemGroups([