Kaskade

Project Url: gumil/Kaskade
Introduction: Unidirectional state container for Android and Kotlin
More: Author   ReportBugs   
Tags:

Build Status codebeat badge

State Container for Kotlin and Android.

The name comes from cascade, a waterfall, which reflects the objective of the library to make flows easier with unidirectional data flow.

Inspired by MVI or Model View Intent.

Kaskade

Kaskade is:

  • Lightweight - does not have external dependencies
  • Modular - add modules as you need
  • Extendable - create user defined Reducers
  • Unidirectional - data flows in one direction
  • Predictable - control on state changes and action triggers
  • DSL - easy to setup DSL to get started

Usage

Create the Action and State objects.

Note: objects are only used here for simplicity in real projects data classes are more appropriate

internal sealed class TestState : State {
    object State1 : TestState()
    object State2 : TestState()
    object State3 : TestState()
}

internal sealed class TestAction : Action {
    object Action1 : TestAction()
    object Action2 : TestAction()
    object Action3 : TestAction()
}

Create Kaskade with TestState.State1 as initial state

val kaskade = Kaskade.create<TestAction, TestState>(TestState.State1) {
    on<TestAction.Action1> {
        TestState.State1
    }

    on<TestAction.Action2> {
        TestState.State2
    }

    on<TestAction.Action3> {
        TestState.State3
    }
}

Adding actions to Action with parameter ActionState

on<TestAction.Action1> { actionState ->
    // do any side effects when returning a new state
    TestState.State1
}

Observing states

kaskade.onStateChanged = {
    // Do something with new state
    render(it)
}

Observing states with Flow

kaskade.stateFlow.subscribe {
    // Do something with new state
    render(it)
}

Executing actions

kaskade.process(TestAction.Action1)

Documentation

Check out the wiki for documentation.

Some of the topics covered are:

Also check out sample-android for Android use cases and sample-kotlin for kotlin only project

Installation

Add the JitPack repository to your build file

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Add the dependency

dependencies {
  // core module
  implementation 'com.github.gumil.kaskade:kaskade:0.2.2'
  // coroutines module
  implementation 'com.github.gumil.kaskade:kaskade-coroutines:0.2.2'
  // rx module
  implementation 'com.github.gumil.kaskade:kaskade-rx:0.2.2'
  // livedata module
  implementation 'com.github.gumil.kaskade:kaskade-livedata:0.2.2'
}
Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea