FlowReduxStateMachineFactory

abstract class FlowReduxStateMachineFactory<S : Any, A : Any>

Used to define a state machine using initializeWith and spec. It can then be started with methods like launchIn or shareIn.

Example:

init {
initializeWith { Loading }

spec {
inState<Loading> {
onEnter {
when(val result = loadData() {
is Success -> override { Content(result.data) }
is Error -> override { Error(result.errorMessage) }
}
}
}

inState<Content> {
// ...
}

inState<Error> {
on<RetryClicked> {
override { Loading }
}
}
}
}

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
fun <S : Parcelable> FlowReduxStateMachineFactory<S, *>.initializeWith(savedStateHandle: SavedStateHandle, initialState: () -> S)

Sets the initial state for the state machine with a Parcelable state class.

fun <S : Any> FlowReduxStateMachineFactory<S, *>.initializeWith(reuseLastEmittedStateOnLaunch: Boolean = true, initialState: () -> S)

Sets the initial state for the state machine.

fun <S : SaveableState> FlowReduxStateMachineFactory<S, *>.initializeWith(savedStateHandle: SavedStateHandle, initialState: (SavedStateHandle) -> S)

Sets the initial state for the state machine with a state class that implements SaveableState. The implementation can save values of the state into the SavedStateHandle. The SavedStateHandle is passed to initialState on each state machine launch so that previously saved values, if present, can be used to construct the initial state.

inline fun <S : Any> FlowReduxStateMachineFactory<S, *>.initializeWith(savedStateHandle: SavedStateHandle, noinline initialState: () -> S)

Sets the initial state for the state machine with a kotlinx.serialization.Serializable state class.

Link copied to clipboard
fun installLogger(logger: Logger, name: String = this::class.simpleName!!)

Install a logger to observe actions in a FlowReduxStateMachine produced by this factory.

Link copied to clipboard
fun launchIn(scope: CoroutineScope): FlowReduxStateMachine<StateFlow<S>, A>

Create and start running a FlowReduxStateMachine that exposes a StateFlow. The state machine will stay active as long as the given scope is not cancelled.

Link copied to clipboard

Create and start running a FlowReduxStateMachine that exposes a compose State. The state machine will stay active as long as this method stays in the current composition.

Link copied to clipboard
fun shareIn(scope: CoroutineScope): FlowReduxStateMachine<SharedFlow<S>, A>

Create and start running a FlowReduxStateMachine that exposes a SharedFlow. The state machine will stay active as long as the given scope is not cancelled.