Code Monkey home page Code Monkey logo

usekase's Introduction

CircleCI License Download

UseKase

Provides default UseCase implementations for the Clean Architecture

Description

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.

How to use it

Get the dependencies

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")
}

Configurate the sourceSet

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.

Create a UseCase

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>

usekase's People

Contributors

earlofego avatar stefma avatar syex avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.