Skip to content

Instantly share code, notes, and snippets.

View roscrazy's full-sized avatar

Minh Nguyen roscrazy

View GitHub Profile
@roscrazy
roscrazy / SingleLocation
Created February 8, 2020 16:04
SingleLocation
override fun singleLocation(): Single<Location> = observeLocationChange()
.firstOrError()
.timeout(locationAttributes.requestTimeOut, TimeUnit.MILLISECONDS)
fusedLocationService.requestLocationUpdates(locationAttributes)
.onErrorResumeNext { it: Throwable ->
androidLocationService.requestLocationUpdates(locationAttributes)
}
@roscrazy
roscrazy / LocationService
Created February 8, 2020 08:12
LocationService
internal interface LocationService {
fun requestLocationUpdates(attributes: RxLocationAttributes): Observable<Location>
}
@roscrazy
roscrazy / RxLocationManagerImpl
Created February 8, 2020 08:10
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)
@roscrazy
roscrazy / AndroidLocationService
Created February 8, 2020 08:04
AndroidLocationService
internal class AndroidLocationService(private val locationManager: LocationManager) :
LocationService {
override fun requestLocationUpdates(attributes: RxLocationAttributes): Observable<Location> {
return Observable.create<Location> { emitter ->
log { "AndroidLocationService ObservableOnSubscribe create" }
val locationListener = RxLocationListener(emitter)
emitter.setCancellable {
log { "AndroidLocationService ObservableOnSubscribe canceled" }
locationManager.removeUpdates(locationListener)
}
@roscrazy
roscrazy / FusedLocationService.requestLocationUpdates
Created February 8, 2020 07:52
FusedLocationService requestLocationUpdates
override fun requestLocationUpdates(attributes: RxLocationAttributes): Observable<Location> {
return createLocationObservable(attributes)
.retry { n: Int, e: Throwable ->
(n < attributes.retryAttempt
&& e !is GooglePlayServicesNotAvailableException
&& e !is SecurityException)
}
}
@roscrazy
roscrazy / gist:0f3c1a43f2058f5490bebdd7a30e8f7a
Created February 8, 2020 04:58
RxLocationManager SetCancellable
emitter.setCancellable(getCancellable(listener))
...
private fun getCancellable(locationListener: LocationCallback): Cancellable {
return Cancellable {
log { "FusedLocationService Observable has canceled" }
fusedLocationProviderClient.removeLocationUpdates(locationListener)
}
}
@roscrazy
roscrazy / CreateLocationObservable
Last active February 8, 2020 04:57
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)
}
@roscrazy
roscrazy / FusedLocationService.kt
Last active February 8, 2020 04:26
FusedLocationService
internal class FusedLocationService(
private val fusedLocationProviderClient: FusedLocationProviderClient
) : LocationService {
override fun requestLocationUpdates(attributes: RxLocationAttributes): Observable<Location> {
return getLocationObservable(attributes)
.retry { n: Int, e: Throwable ->
(n < attributes.retryAttempt
&& e !is GooglePlayServicesNotAvailableException
&& e !is SecurityException)
}