From cf1763e79d596371d8883e8786f378d11c9502d7 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 18 Dec 2018 09:16:32 -0500 Subject: [PATCH] Suppress undo during strokes. --- .../Views/ImageEditor/ImageEditorModel.swift | 51 ++----------------- .../Views/ImageEditor/ImageEditorView.swift | 6 +-- 2 files changed, 5 insertions(+), 52 deletions(-) diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift b/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift index 81fdb756e..a79eecee4 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorModel.swift @@ -320,13 +320,6 @@ public class ImageEditorModel: NSObject { private var undoStack = [ImageEditorOperation]() private var redoStack = [ImageEditorOperation]() - // In some cases, we want to suppress changes to undo state. - // e.g. drawing a stroke will modify the model many times (once - // for each touch event/stroke sample), but we only want that - // to add a single undo operation. - private var isUndoSuppressed = false - private var suppressedUndoContents: ImageEditorContents? - // We don't want to allow editing of images if: // // * They are invalid. @@ -372,28 +365,6 @@ public class ImageEditorModel: NSObject { return !undoStack.isEmpty } - @objc - public func setIsUndoSuppressed(isUndoSuppressed: Bool) { - if isUndoSuppressed { - if self.isUndoSuppressed { - owsFailDebug("Undo already suppressed.") - } - if suppressedUndoContents != nil { - owsFailDebug("Unexpected suppressed undo contents.") - } - suppressedUndoContents = contents.clone() - } else { - if self.isUndoSuppressed { - if suppressedUndoContents == nil { - owsFailDebug("Missing suppressed undo contents.") - } - } - suppressedUndoContents = nil - } - - self.isUndoSuppressed = isUndoSuppressed - } - @objc public func canRedo() -> Bool { return !redoStack.isEmpty @@ -438,10 +409,10 @@ public class ImageEditorModel: NSObject { @objc public func replace(item: ImageEditorItem, - shouldRemoveUndoSuppression: Bool = false) { + suppressUndo: Bool = false) { performAction({ (newContents) in newContents.replace(item: item) - }, shouldRemoveUndoSuppression: shouldRemoveUndoSuppression) + }, suppressUndo: suppressUndo) } @objc @@ -452,22 +423,8 @@ public class ImageEditorModel: NSObject { } private func performAction(_ action: (ImageEditorContents) -> Void, - shouldRemoveUndoSuppression: Bool = false) { - if shouldRemoveUndoSuppression { - if !isUndoSuppressed { - owsFailDebug("Can't remove undo suppression, not suppressed.") - } - if let suppressedUndoContents = self.suppressedUndoContents { - let undoOperation = ImageEditorOperation(contents: suppressedUndoContents) - undoStack.append(undoOperation) - redoStack.removeAll() - } else { - owsFailDebug("Missing suppressed undo contents.") - } - self.isUndoSuppressed = false - - setIsUndoSuppressed(isUndoSuppressed: false) - } else if !isUndoSuppressed { + suppressUndo: Bool = false) { + if !suppressUndo { let undoOperation = ImageEditorOperation(contents: contents) undoStack.append(undoOperation) redoStack.removeAll() diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift index d460c8834..1465bc34b 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift @@ -107,7 +107,6 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { if let stroke = self.currentStroke { self.model.remove(item: stroke) } - self.model.setIsUndoSuppressed(isUndoSuppressed: false) self.currentStroke = nil self.currentStrokeSamples.removeAll() } @@ -132,8 +131,6 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { currentStrokeSamples.append(unitSampleForGestureLocation()) - model.setIsUndoSuppressed(isUndoSuppressed: true) - let stroke = ImageEditorStrokeItem(color: strokeColor, unitSamples: currentStrokeSamples, unitStrokeWidth: unitStrokeWidth) model.append(item: stroke) currentStroke = stroke @@ -150,13 +147,12 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { // Model items are immutable; we _replace_ the // stroke item rather than modify it. let stroke = ImageEditorStrokeItem(itemId: lastStroke.itemId, color: strokeColor, unitSamples: currentStrokeSamples, unitStrokeWidth: unitStrokeWidth) + model.replace(item: stroke, suppressUndo: true) if gestureRecognizer.state == .ended { - model.replace(item: stroke, shouldRemoveUndoSuppression: true) currentStroke = nil currentStrokeSamples.removeAll() } else { - model.replace(item: stroke) currentStroke = stroke } default: