Skip to content

Instantly share code, notes, and snippets.

@roscrazy
Last active February 8, 2020 04:57
Show Gist options
  • Save roscrazy/f00bc4ff2a55ef6874b853710625bf21 to your computer and use it in GitHub Desktop.
Save roscrazy/f00bc4ff2a55ef6874b853710625bf21 to your computer and use it in GitHub Desktop.
RxLocationManager CreateLocationObservable
private fun createLocationObservable(attributes: RxLocationAttributes): Observable<Location> {
return Observable.create { emitter ->
val listener = getLocationListener(emitter)
val completeListener = getOnCompleteListener(emitter)
try {
fusedLocationProviderClient.lastLocation.addOnSuccessListener {
if (!emitter.isDisposed && it != null) emitter.onNext(it)
}
val task = fusedLocationProviderClient.requestLocationUpdates(
getLocationRequest(attributes),
listener,
if (attributes.useCalledThreadToEmitValue) null else Looper.getMainLooper()
)
task.addOnCompleteListener(completeListener)
} catch (e: Exception) {
emitter.tryOnError(e)
}
emitter.setCancellable(getCancellable(listener))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment