All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.copperleaf.ballast.undo.BallastUndoInterceptor.kt Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
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)"
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy