commonMain.com.copperleaf.ballast.undo.BallastUndoInterceptor.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
import com.copperleaf.ballast.BallastInterceptor
import com.copperleaf.ballast.BallastInterceptorScope
import com.copperleaf.ballast.BallastNotification
import kotlinx.coroutines.flow.Flow
/**
* Adds undo/redo functionality to a Ballast ViewModel. This interceptor should be added to a ViewModel's configuration,
* and only the [controller] needs to be accessed from the UI which is handling the undo/redo actions. An
* [UndoController] should be created and managed separately from the ViewModel it is associated with, and should only
* be associated with a single ViewModel.
*/
public class BallastUndoInterceptor(
private val controller: UndoController
) : BallastInterceptor, UndoController by controller {
public object Key : BallastInterceptor.Key>
override val key: BallastInterceptor.Key> = BallastUndoInterceptor.Key
override fun BallastInterceptorScope.start(notifications: Flow>) {
val scope = UndoScopeImpl(this)
with(controller) {
with(scope) {
connectViewModel(
notifications = notifications,
)
}
}
}
override fun toString(): String {
return "BallastUndoInterceptor(controller=$controller)"
}
}