Created
March 20, 2022 14:06
-
-
Save NoNews/c9115f202696fc1794837c6a14b33f11 to your computer and use it in GitHub Desktop.
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
abstract class BaseViewModel : ViewModel() { | |
protected val defaultErrorHandler: (Throwable) -> Unit by lazy { | |
{ throwable: Throwable -> | |
Log.e(javaClass.simpleName, "DefaultErrorHandler", throwable) | |
Unit | |
} | |
} | |
private val defaultCompleteHandler: () -> Unit by lazy { {} } | |
private val compositeDisposable = CompositeDisposable() | |
override fun onCleared() { | |
compositeDisposable.clear() | |
super.onCleared() | |
} | |
protected fun <T> Observable<T>.subscribeTillCleared(onNext: (T) -> Unit = {}): Disposable = | |
subscribeTillCleared(onNext, defaultErrorHandler) | |
protected fun <T> Observable<T>.subscribeTillCleared( | |
onNext: (T) -> Unit, | |
onError: (Throwable) -> Unit = defaultErrorHandler, | |
onComplete: () -> Unit = defaultCompleteHandler | |
): Disposable { | |
val disposable = subscribe(onNext, onError, onComplete) | |
compositeDisposable += disposable | |
return disposable | |
} | |
protected fun <T> Single<T>.subscribeTillCleared(onSuccess: (T) -> Unit = {}): Disposable = | |
subscribeTillCleared(onSuccess, defaultErrorHandler) | |
protected fun <T> Single<T>.subscribeTillCleared( | |
onSuccess: (T) -> Unit, | |
onError: (Throwable) -> Unit = defaultErrorHandler | |
): Disposable { | |
val disposable = subscribe(onSuccess, onError) | |
compositeDisposable += disposable | |
return disposable | |
} | |
protected fun <T> Maybe<T>.subscribeTillCleared(onSuccess: (T) -> Unit = {}): Disposable = | |
subscribeTillCleared(onSuccess, defaultErrorHandler) | |
protected fun <T> Maybe<T>.subscribeTillCleared( | |
onSuccess: (T) -> Unit, | |
onError: (Throwable) -> Unit = defaultErrorHandler, | |
onComplete: () -> Unit = defaultCompleteHandler | |
): Disposable { | |
val disposable = subscribe(onSuccess, onError, onComplete) | |
compositeDisposable += disposable | |
return disposable | |
} | |
protected fun <T> Flowable<T>.subscribeTillCleared(onSuccess: (T) -> Unit = {}): Disposable = | |
subscribeTillCleared(onSuccess, defaultErrorHandler) | |
protected fun <T> Flowable<T>.subscribeTillCleared( | |
onSuccess: (T) -> Unit, | |
onError: (Throwable) -> Unit = defaultErrorHandler, | |
onComplete: () -> Unit = defaultCompleteHandler | |
): Disposable { | |
val disposable = subscribe(onSuccess, onError, onComplete) | |
compositeDisposable.add(disposable) | |
return disposable | |
} | |
protected fun Completable.subscribeTillCleared(onSuccess: () -> Unit = {}): Disposable = | |
subscribeTillCleared(onSuccess, defaultErrorHandler) | |
protected fun Completable.subscribeTillCleared( | |
onSuccess: () -> Unit, | |
onError: (Throwable) -> Unit = defaultErrorHandler | |
): Disposable { | |
val disposable = subscribe(onSuccess, onError) | |
compositeDisposable += disposable | |
return disposable | |
} | |
} | |
private operator fun CompositeDisposable.plusAssign(disposable: Disposable) { | |
add(disposable) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment