commonMain.com.copperleaf.ballast.undo.state.stateBasedUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ballast-undo-jvm Show documentation
Show all versions of ballast-undo-jvm Show documentation
Adds undo/redo functionality to your ViewModel state.
package com.copperleaf.ballast.undo.state
import com.copperleaf.ballast.BallastViewModelConfiguration
import com.copperleaf.ballast.core.FifoInputStrategy
import com.copperleaf.ballast.withViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.sample
import kotlin.time.Duration.Companion.seconds
public fun BallastViewModelConfiguration.Builder.withStateBasedUndoController(
bufferStates: (Flow) -> Flow = { it.sample(5.seconds) },
historyDepth: Int = 10,
): BallastViewModelConfiguration.TypedBuilder<
StateBasedUndoControllerContract.Inputs,
StateBasedUndoControllerContract.Events,
StateBasedUndoControllerContract.State> {
return this
.withViewModel(
initialState = StateBasedUndoControllerContract.State(),
inputHandler = StateBasedUndoControllerInputHandler(bufferStates, historyDepth),
name = "UndoController",
)
.apply {
this.inputStrategy = FifoInputStrategy.typed()
}
}