Skip to content
Permalink
1 contributor

Users who have contributed to this file

executable file 52 lines (42 sloc) 1.63 KB
package org.buffer.android.boilerplate.domain.interactor
import io.reactivex.Flowable
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import io.reactivex.subscribers.DisposableSubscriber
import org.buffer.android.boilerplate.domain.executor.PostExecutionThread
import org.buffer.android.boilerplate.domain.executor.ThreadExecutor
/**
* Abstract class for a UseCase that returns an instance of a [Single].
*/
abstract class FlowableUseCase<T, in Params> constructor(
private val threadExecutor: ThreadExecutor,
private val postExecutionThread: PostExecutionThread) {
private val disposables = CompositeDisposable()
/**
* Builds a [Single] which will be used when the current [FlowableUseCase] is executed.
*/
protected abstract fun buildUseCaseObservable(params: Params? = null): Flowable<T>
/**
* Executes the current use case.
*/
open fun execute(observer: DisposableSubscriber<T>, params: Params? = null) {
val observable = this.buildUseCaseObservable(params)
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.scheduler) as Flowable<T>
addDisposable(observable.subscribeWith(observer))
}
/**
* Dispose from current [CompositeDisposable].
*/
fun dispose() {
if (!disposables.isDisposed) disposables.dispose()
}
/**
* Dispose from current [CompositeDisposable].
*/
private fun addDisposable(disposable: Disposable) {
disposables.add(disposable)
}
}
You can’t perform that action at this time.