Last active
December 8, 2019 10:18
-
-
Save juliuscanute/9c14e5b3349fed8909e714ac8a9734a7 to your computer and use it in GitHub Desktop.
[Domain Layer] #clean #architecture #android
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.raywenderlich.android.creatures.domain.interactor | |
| /** | |
| * 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) | |
| } | |
| } |
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.raywenderlich.android.creatures.domain.interactor.browse | |
| open class GetCreatures @Inject constructor(private val creatureRepository: CreatureRepository, | |
| threadExecutor: ThreadExecutor, | |
| postExecutionThread: PostExecutionThread): | |
| FlowableUseCase<List<Creature>, Void?>(threadExecutor, postExecutionThread) { | |
| public override fun buildUseCaseObservable(params: Void?): Flowable<List<Creature>> { | |
| return creatureRepository.getCreatures() | |
| } | |
| } |
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.raywenderlich.android.creatures.domain.executor | |
| import io.reactivex.Scheduler | |
| interface PostExecutionThread { | |
| 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
| package com.raywenderlich.android.creatures.domain.executor | |
| import java.util.concurrent.Executor | |
| interface ThreadExecutor : Executor |
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.raywenderlich.android.creatures.ui | |
| class UiThread @Inject internal constructor() : PostExecutionThread { | |
| override val scheduler: Scheduler | |
| get() = AndroidSchedulers.mainThread() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment