Last active
September 15, 2018 23:07
-
-
Save vvsevolodovich/570e45fc46a569064562e01a14db9b6f to your computer and use it in GitHub Desktop.
Ugly code written with RxJava due to lack of RxJava understanding
This file contains 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh man! Such a hell!