-
Notifications
You must be signed in to change notification settings - Fork 178
/
FlowableUseCase.kt
executable file
·52 lines (42 loc) · 1.63 KB
/
FlowableUseCase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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)
}
}