Created
May 18, 2020 19:46
-
-
Save dmitriy-chernysh/f6234913fb5a6491a4a8f3ff0efb4b35 to your computer and use it in GitHub Desktop.
Template | Use Case | Domain Layer
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
package com.mobiledevpro.interactor.usecase.base | |
import com.mobiledevpro.interactor.usecase.base.executor.ExecutionThread | |
import com.mobiledevpro.interactor.usecase.base.executor.PostExecutionThread | |
import io.reactivex.Scheduler | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.disposables.Disposable | |
abstract class BaseRxUseCase( | |
executionThread: ExecutionThread, | |
postExecutionThread: PostExecutionThread | |
) { | |
protected val executionThreadScheduler: Scheduler = executionThread.scheduler | |
protected val postExecutionThreadScheduler: Scheduler = postExecutionThread.scheduler | |
private val disposables = CompositeDisposable() | |
open fun dispose() { | |
if (!disposables.isDisposed) { | |
disposables.dispose() | |
} | |
} | |
protected fun addToDisposable(disposable: Disposable) { | |
disposables.add(disposable) | |
} | |
} |
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
interface ExecutionThread { | |
val scheduler: Scheduler | |
} |
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 ObservableUseCase<Results, in Params>( | |
executionThread: ExecutionThread, | |
postExecutionThread: PostExecutionThread | |
) : BaseRxUseCase(executionThread, postExecutionThread) { | |
/** | |
* Builds Observable | |
*/ | |
abstract fun buildUseCaseObservable(params: Params? = null): Observable<Results> | |
/** | |
* Executes the current use case. | |
*/ | |
fun execute(observer: DisposableObserver<Results>, params: Params? = null) { | |
val observable = this.buildUseCaseObservable(params) | |
.subscribeOn(executionThreadScheduler) | |
.observeOn(postExecutionThreadScheduler) | |
addToDisposable(observable.subscribeWith(observer)) | |
} | |
/** | |
* Executes the current use case. | |
*/ | |
fun execute(params: Params? = null): Observable<Results> = | |
this.buildUseCaseObservable(params) | |
.subscribeOn(executionThreadScheduler) | |
.observeOn(postExecutionThreadScheduler) | |
} |
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
interface PostExecutionThread { | |
val scheduler: Scheduler | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a real Use Case from my app, based on templates above
How to execute Use case:
getVehicleUseCase.execute(stockNumber)