Skip to content

Instantly share code, notes, and snippets.

@juliuscanute
Last active December 8, 2019 10:18
Show Gist options
  • Select an option

  • Save juliuscanute/9c14e5b3349fed8909e714ac8a9734a7 to your computer and use it in GitHub Desktop.

Select an option

Save juliuscanute/9c14e5b3349fed8909e714ac8a9734a7 to your computer and use it in GitHub Desktop.
[Domain Layer] #clean #architecture #android
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)
}
}
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()
}
}
package com.raywenderlich.android.creatures.domain.executor
import io.reactivex.Scheduler
interface PostExecutionThread {
val scheduler: Scheduler
}
package com.raywenderlich.android.creatures.domain.executor
import java.util.concurrent.Executor
interface ThreadExecutor : Executor
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