Skip to content

Instantly share code, notes, and snippets.

@roscrazy
Created February 8, 2020 08:10
Show Gist options
  • Save roscrazy/8d2f12ddaba44fd71f01367fda20675f to your computer and use it in GitHub Desktop.
Save roscrazy/8d2f12ddaba44fd71f01367fda20675f to your computer and use it in GitHub Desktop.
RxLocationManagerImpl
internal class RxLocationManagerImpl(
private val fusedLocationService: LocationService,
private val androidLocationService: LocationService,
private val locationAttributes: RxLocationAttributes
) : RxLocationManager {
private val sharedLocationObservable = createLocationObservable()
override fun singleLocation(): Single<Location> = observeLocationChange()
.firstOrError()
.timeout(locationAttributes.requestTimeOut, TimeUnit.MILLISECONDS)
override fun observeLocationChange(): Observable<Location> = sharedLocationObservable
private fun createLocationObservable(): Observable<Location> {
return fusedLocationService.requestLocationUpdates(locationAttributes)
.onErrorResumeNext { it: Throwable ->
androidLocationService.requestLocationUpdates(locationAttributes)
}
.replay(1)
.refCount()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment