Created
March 4, 2020 00:43
-
-
Save GuilhE/8a1e5f35061d8f1797768aee4c61923c to your computer and use it in GitHub Desktop.
Helper compose functions for RxKotlin
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
import android.annotation.SuppressLint | |
import com.blissapplications.kotlin.core.common.threads.ReactivexThreadSchedulerProvider | |
import io.reactivex.Completable | |
import io.reactivex.Single | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.disposables.Disposable | |
/** | |
* Examples of usage of this extensions functions: | |
* | |
* private val compositeDisposable by lazy { CompositeDisposable() } | |
* | |
* Single.composeForLoading(schedulerProvider, compositeDisposable){ toggle -> toggleLoading(toggle)} | |
* Completable.composeForLoading(schedulerProvider, compositeDisposable){ toggle -> toggleLoading(toggle)} | |
*/ | |
fun Disposable.addToComposite(bag: CompositeDisposable) { | |
bag.add(this) | |
} | |
@Suppress("unused") | |
@SuppressLint("CheckResult") | |
fun Completable.composeWithBag(bag: CompositeDisposable) { | |
compose { upstream -> upstream.doOnSubscribe { it.addToComposite(bag) } } | |
} | |
@Suppress("unused") | |
@SuppressLint("CheckResult") | |
fun Completable.composeForLoading( | |
schedulerProvider: ReactivexThreadSchedulerProvider, | |
bag: CompositeDisposable, | |
toggleLoading: (Boolean) -> Unit | |
) { | |
compose { upstream: Completable -> | |
upstream | |
.doOnSubscribe { | |
it.addToComposite(bag) | |
toggleLoading(true) | |
} | |
.observeOn(schedulerProvider.ui) | |
.doFinally { toggleLoading(false) } | |
} | |
} | |
@Suppress("unused") | |
@SuppressLint("CheckResult") | |
fun <T> Single<T>.composeWithBag(bag: CompositeDisposable) { | |
compose { upstream -> upstream.doOnSubscribe { it.addToComposite(bag) } } | |
} | |
@Suppress("unused") | |
@SuppressLint("CheckResult") | |
fun <T> Single<T>.composeForLoading( | |
schedulerProvider: ReactivexThreadSchedulerProvider, | |
bag: CompositeDisposable, | |
toggleLoading: (Boolean) -> Unit | |
) { | |
compose { upstream -> | |
upstream | |
.doOnSubscribe { | |
it.addToComposite(bag) | |
toggleLoading(true) | |
} | |
.observeOn(schedulerProvider.ui) | |
.doFinally { toggleLoading(false) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment