mirror of
https://github.com/dscyrescotti/Memola.git
synced 2026-03-12 21:35:25 +01:00
feat: update undo and redo
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -88,6 +88,7 @@ struct MemoView: View {
|
||||
}
|
||||
|
||||
func closeMemo() {
|
||||
history.resetRedo()
|
||||
if managedObjectContext.hasChanges {
|
||||
do {
|
||||
try managedObjectContext.save()
|
||||
|
||||
@@ -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"/>
|
||||
|
||||
Reference in New Issue
Block a user