Created
December 7, 2018 11:31
-
-
Save photizzo/2201cca70d4ca083fbed62d691464c6c to your computer and use it in GitHub Desktop.
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
abstract class CompletableUseCase<in Params> constructor( | |
private val postExecutionThread: PostExecutionThread) { | |
private val disposables = CompositeDisposable() | |
protected abstract fun buildUseCaseCompletable(params: Params? = null): Completable | |
open fun execute(observer: DisposableCompletableObserver, params: Params? = null) { | |
val completable = this.buildUseCaseCompletable(params) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(postExecutionThread.scheduler) | |
addDisposable(completable.subscribeWith(observer)) | |
} | |
fun addDisposable(disposable: Disposable) { | |
disposables.add(disposable) | |
} | |
fun dispose() { | |
if (!disposables.isDisposed) disposables.dispose() | |
} | |
} | |
abstract class ObservableUseCase<T, in Params> constructor( | |
private val postExecutionThread: PostExecutionThread) { | |
private val disposables = CompositeDisposable() | |
protected abstract fun buildUseCaseObservable(params: Params? = null): Observable<T> | |
open fun execute(singleObserver: DisposableObserver<T>, params: Params? = null) { | |
val single = this.buildUseCaseObservable(params) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(postExecutionThread.scheduler) | |
addDisposable(single.subscribeWith(singleObserver)) | |
} | |
fun addDisposable(disposable: Disposable) { | |
disposables.add(disposable) | |
} | |
fun dispose() { | |
if (!disposables.isDisposed) disposables.dispose() | |
} | |
} | |
abstract class FlowableUseCase<T, in Params> constructor( | |
private val postExecutionThread: PostExecutionThread) { | |
private val disposables = CompositeDisposable() | |
protected abstract fun buildUseCaseObservable(params: Params? = null): Flowable<T> | |
open fun execute(singleObserver: DisposableSubscriber<T>, params: Params? = null) { | |
val single = this.buildUseCaseObservable(params) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(postExecutionThread.scheduler) | |
addDisposable(single.subscribeWith(singleObserver)) | |
} | |
fun addDisposable(disposable: Disposable) { | |
disposables.add(disposable) | |
} | |
fun dispose() { | |
if (!disposables.isDisposed) disposables.dispose() | |
} | |
} | |
interface PostExecutionThread { | |
val scheduler: Scheduler | |
} | |
class UiThread @Inject constructor(): PostExecutionThread { | |
override val scheduler: Scheduler | |
get() = AndroidSchedulers.mainThread() | |
} | |
inner class LoginSubscriber : DisposableObserver<String>() { | |
override fun onComplete() { | |
} | |
override fun onNext(t: String) { | |
liveData.postValue(Resource(ResourceState.SUCCESS, t, null)) | |
} | |
override fun onError(e: Throwable) { | |
liveData.postValue(Resource(ResourceState.ERROR, null, e.errorMessage())) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment