feat: update undo and redo

This commit is contained in:
dscyrescotti
2024-05-08 23:18:31 +07:00
parent 1f9c176eb0
commit 0fc277ca5b
3 changed files with 23 additions and 3 deletions

View File

@@ -42,15 +42,27 @@ class GraphicContext: NSManagedObject {
func undoGraphic() {
guard let stroke = strokes.lastObject as? Stroke else { return }
strokes.remove(stroke)
stroke.graphicContext = nil
previousStroke = nil
do {
try Persistence.shared.viewContext.save()
} catch {
NSLog("[Memola] - \(error.localizedDescription)")
}
}
func redoGraphic(for event: HistoryEvent) {
switch event {
case .stroke(let stroke):
strokes.add(stroke)
stroke.graphicContext = self
previousStroke = nil
}
do {
try Persistence.shared.viewContext.save()
} catch {
NSLog("[Memola] - \(error.localizedDescription)")
}
}
}
@@ -109,8 +121,15 @@ extension GraphicContext {
}
func cancelStroke() {
if let stroke = strokes.lastObject {
strokes.remove(stroke)
if let stroke = strokes.lastObject as? Stroke {
do {
let viewContext = Persistence.shared.viewContext
strokes.remove(stroke)
viewContext.delete(stroke)
try viewContext.save()
} catch {
NSLog("[Memola] - \(error.localizedDescription)")
}
}
currentStroke = nil
currentPoint = nil

View File

@@ -88,6 +88,7 @@ struct MemoView: View {
}
func closeMemo() {
history.resetRedo()
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()

View File

@@ -23,7 +23,7 @@
<attribute name="color" attributeType="Transformable" valueTransformerName="NSSecureUnarchiveFromDataTransformer" customClassName="[CGFloat]"/>
<attribute name="createdAt" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="id" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="strokeQuads" optional="YES" attributeType="Transformable" valueTransformerName="QuadValueTransformer" customClassName="[StrokeQuad]"/>
<attribute name="strokeQuads" attributeType="Transformable" valueTransformerName="QuadValueTransformer" customClassName="[StrokeQuad]"/>
<attribute name="style" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
<attribute name="thickness" attributeType="Double" defaultValueString="0.0" usesScalarValueType="YES"/>
<relationship name="graphicContext" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="GraphicContext" inverseName="strokes" inverseEntity="GraphicContext"/>