Skip to content

Instantly share code, notes, and snippets.

@vvsevolodovich
Last active September 15, 2018 23:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvsevolodovich/570e45fc46a569064562e01a14db9b6f to your computer and use it in GitHub Desktop.
Save vvsevolodovich/570e45fc46a569064562e01a14db9b6f to your computer and use it in GitHub Desktop.
Ugly code written with RxJava due to lack of RxJava understanding
fun connectionObserveration(context: Context): Disposable? {
if ((firstObserver == null || firstObserver!!.isDisposed) && (secondObserver == null || secondObserver!!.isDisposed())) {
secondObserver = ReactiveNetwork.observeNetworkConnectivity(context)
.subscribeOn(AndroidSchedulers.mainThread())
.filter(ConnectivityPredicate.hasState(NetworkInfo.State.CONNECTED, NetworkInfo.State.DISCONNECTED))
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ connectivity ->
if (connectivity.state == NetworkInfo.State.CONNECTED) {
firstObserver = ReactiveNetwork.observeInternetConnectivity()
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ connected ->
getEventBus().post(ConnectionChangedEvent(connected))
if (firstObserver != null) {
firstObserver?.dispose()
secondObserver?.dispose()
}
firstObserver = null
}, { it.printStackTrace() })
} else {
getEventBus().post(ConnectionChangedEvent(connected))
}
}, { it.printStackTrace() })
}
return firstObserver
}
@thuytrinh
Copy link

Oh man! Such a hell!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment