package net.metacircular.engpad.undo import net.metacircular.engpad.data.model.Stroke import net.metacircular.engpad.data.repository.PageRepository class AddStrokeAction( private val stroke: Stroke, private val repository: PageRepository, private val onExecute: (Stroke) -> Unit, private val onUndo: (Long) -> Unit, ) : UndoableAction { override val description = "Add stroke" private var insertedId: Long = 0 override suspend fun execute() { insertedId = repository.addStroke(stroke) onExecute(stroke.copy(id = insertedId)) } override suspend fun undo() { repository.deleteStroke(insertedId) onUndo(insertedId) } } class DeleteStrokeAction( private val stroke: Stroke, private val repository: PageRepository, private val onExecute: (Long) -> Unit, private val onUndo: (Stroke) -> Unit, ) : UndoableAction { override val description = "Delete stroke" override suspend fun execute() { repository.deleteStroke(stroke.id) onExecute(stroke.id) } override suspend fun undo() { val id = repository.addStroke(stroke) onUndo(stroke.copy(id = id)) } }