gumil / Kaskade

[INACTIVE] Simplifying state management
Apache License 2.0
224 stars 6 forks source link
android coroutines flow kaskade kotlin machine multiplatform mvi state udf unidirectional-data-flow

Kaskade

Build Status Download Android Arsenal codecov Codacy 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

Why Kaskade?

Installation

  1. Add to settings.gradle
    enableFeaturePreview('GRADLE_METADATA')
  2. Add the dependency
    dependencies {
    // core module
    implementation 'dev.gumil.kaskade:core:0.x.y'
    // coroutines module
    implementation 'dev.gumil.kaskade:coroutines:0.x.y'
    // rx module
    implementation 'dev.gumil.kaskade:rx:0.x.y'
    // livedata module
    implementation 'dev.gumil.kaskade:livedata:0.x.y'
    }

    (Please replace x and y with the latest version numbers: Download )

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 Emitter

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

Executing actions

kaskade.dispatch(TestAction.Action1)

Documentation

Check out the wiki for documentation.

Some of the topics covered are:

Sample projects