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")
// Alternatively (but highly recommended): usekase-processor
kapt("guru.stefma.cleancomponents:usekase-processor:$useKaseVersion")
}
First you have to add the generated source dir to your sourceSet
.
You can do this by adding the following into your build.gradle
file:
// Add the generated source as sourceSet
android.applicationVariants.all { variant ->
variant.addJavaSourceFoldersToModel()
def kotlinGenerated = file("$buildDir/generated/source/kaptKotlin/${variant.name}")
variant.addJavaSourceFoldersToModel(kotlinGenerated)
}
For pure Kotlin projects the syntax is a little bit different. See this issue for more.
You can just implement one of the provided default UseCase implementations
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>
FlowableUseCase<R, P>
ObservableUseCase<R, P>
MaybeUseCase<R, P>
CompletableUseCase<P>