Kaskade

General

Category
Free
Tag
FSM
License
Apache License, Version 2.0
Min SDK
21 (Android 5.0–5.0.2 Lollipop)
Registered
Jan 5, 2019
Favorites
1
Link
https://github.com/gumil/Kaskade/
See also
Ken-Ken-Pa
TinyMachine
Engine
EasyFlow

Additional

Language
Kotlin
Version
0.2.0 (Dec 21, 2018)
Created
Feb 4, 2018
Updated
Dec 21, 2018
Owner
Miguel Panelo (gumil)
Contributor
Miguel Panelo (gumil)
1
Activity
Badge
Generate
Download
Source code

Announcement

Kaskade

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.

Installation

Add the JitPack repository to your build file

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

Add the dependency

dependencies {
  implementation 'com.github.gumil.kaskade:kaskade:0.2.0'
}

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
}

Syntax is made as simple as possible with lesser ceremonies in transfroming an action to state.

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(ToastAction.Action1)

Coroutines

dependencies {
  implementation 'com.github.gumil.kaskade:kaskade-coroutines:0.2.0'
}

Creating Kaskade with coroutines dsl:

Shared scope

Kaskade.create<TestAction, TestState>(TestState.State1) {
    coroutines(localScope) {
        on<TestAction.Action1> {
            TestState.State1
        }
        on<TestAction.Action2> {
            TestState.State2
        }
    }
}

Independent scopes

Kaskade.create<TestAction, TestState>(TestState.State1) {
    coroutines {
        on<TestAction.Action1>(localScope) {
            TestState.State1
        }
        on<TestAction.Action2>(newScope) {
            TestState.State2
        }
    }
}

RxJava2

Add the dependency

dependencies {
  implementation 'com.github.gumil.kaskade:kaskade-rx:0.2.0'
}

Creating Kaskade with rx dsl:

Shared observer

Kaskade.create<TestAction, TestState>(TestState.State1) {
    rx({ observer }) {
        on<TestAction.Action1> {
            TestState.State1
        }
        on<TestAction.Action2> {
            TestState.State2
        }
    }
}

Independent observers

Kaskade.create<TestAction, TestState>(TestState.State1) {
    rx {
        on<TestAction.Action1>({ observer1 }) {
            TestState.State1
        }
        on<TestAction.Action2>({ observer2 }) {
            TestState.State2
        }
    }
}

Observing state as Observable

kaskade.stateObservable()

LiveData

Add the dependency

dependencies {
  implementation 'com.github.gumil.kaskade:kaskade-livedata:0.2.0'
}

Observing state as LiveData

kaskade.stateLiveData()