Provides default UseCase implementations for the Clean Architecture
UseKase is a Kotlin library which provides a set of default implementations of UseCases. Most of the UseCases depend on heavily on RxJava2.
I found my self copying these implementations in new projects again and again. Even if these contain only some lines of code it was just annoying. So I decided to create this library which provides these for me.
Because all Kotlin classes are final
by default I created for each UseCase a typealias
to the correct interface
. This make the UseCase in Presenters testable. Because it was as annoying as copying and pasting the UseCase implementations I created an annotation processor which creates the typealias
at runtime for me. You can find the code for that in the usekase-processor module.
UseKase is available via jcenter:
repositories {
jcenter()
}
dependencies {
implementation("guru.stefma.cleancomponents:usekase:$useKaseVersion")
// Needed for UseKase: Kotlin and RxJava2
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion")
implementation("io.reactivex.rxjava2:rxjava:$rxJava2Version")
// Alternatively (but highly recommended): usekase-processor
implementation("guru.stefma.cleancomponents:usekase-processor:$useKaseVersion")
}
Just implement one of the provided default UseCase implementation and add the @UseCase
annotation to it:
@UseCase
GetUserUseCase(
override val executionScheduler: Scheduler = Schedulers.io(),
override val postExecutionScheduler: Scheduler = AndroidSchedulers.mainThread(),
private val api: BackendApi
) : SingleUseCase<User, String> {
override fun buildUseCase(params: String): Single<User> {
return api.getUserByUserId(params)
}
}
If the UseKase-Processor
is applied it will generated the following typealias
:
typealias GetUser = SingleUseCase<User, String>
All available UseCase
implementations are:
UseCase<R, P>
RxUseCase<R, P>
SingleUseCase<R, P>
ObservableUseCase<R, P>
MaybeUseCase<R, P>
CompletableUseCase<P>