-
Notifications
You must be signed in to change notification settings - Fork 6
State
A state is a representation of your client. It's the model of how the client should look like after performing operations in Kaskade.
Kaskade only output states with the type State
which is an empty interface for typing. This means that all the states that you have for a Kaskade must inherit from a class with the type of State
which sealed classes does very well.
With the todo example we represent the states as follows:
sealed class TodoState : State {
data class OnLoaded(val list: List<TodoItem>) : TodoState()
data class OnDeleted(val position: Int) : TodoState()
data class OnAdded(val item: TodoItem) : TodoState()
data class OnUpdated(val position: Int, val item: TodoItem) : TodoState()
}
Modelling states is dependent on the client. There might be some behaviors that needs to be considered when modelling. In the todo example it assumes that the pieces of the screen is independent from each other.
An alternative in modelling state would be making one data class, for example, a screen state which represents one screen in Android.
data class ScreenState(
val todoItems: List<TodoItem>,
val errorMessage: String,
val isLoading: Boolean
) : State
This example assumes that a screen contains a list of items, an area in the screen to shows an error message, and a loading spinner.
Kaskade requires an initial state when creating using the Kaskade DSL. This initial state is immediately queued and emitted once onStateChanged
is instantiated.
Kaskade.create<Action, State>(InitalState()) {
. . .
}
This is important in handling process death in Android.
Kaskade exposes onStateChanged
function that can be used to listen to states every time a state is emitted by a reducer.
kaskade.onStateChanged = {
render(it)
}
While onStateChanged
is not yet assigned, Kaskade enqueues states and dequeues them after assigning onStateChanged
.
To make listening states easier there are extension functions to listen to states using Android LiveData, RxJava Observables, and Flow.
There's a type in Kaskade to indicate that a state should only happen once and should not be considered in Reducers
. A common example is an Error
state where you only show a Toast
or Snackbar
.
data class Error(val errorMessage: String): State, SingleEvent