Last active
February 8, 2020 04:57
-
-
Save roscrazy/f00bc4ff2a55ef6874b853710625bf21 to your computer and use it in GitHub Desktop.
RxLocationManager CreateLocationObservable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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