From 9378ab21920f5622b7b1530f8b528513533679b0 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 17 Dec 2018 17:05:43 -0500 Subject: [PATCH] Add undo/redo buttons to image editor. --- .../translations/en.lproj/Localizable.strings | 6 ++ .../AttachmentApprovalViewController.swift | 5 +- .../Views/ImageEditor/ImageEditorView.swift | 64 +++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index 1804286c9..fbad19f4a 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -311,9 +311,15 @@ /* Label for generic done button. */ "BUTTON_DONE" = "Done"; +/* Label for redo button. */ +"BUTTON_REDO" = "Redo"; + /* Button text to enable batch selection mode */ "BUTTON_SELECT" = "Select"; +/* Label for undo button. */ +"BUTTON_UNDO" = "Undo"; + /* Label for button that lets users call a contact again. */ "CALL_AGAIN_BUTTON_TITLE" = "Call Again"; diff --git a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift index 44a505589..ee866096b 100644 --- a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift +++ b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift @@ -857,8 +857,6 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD private(set) var contentContainer: UIView! private(set) var playVideoButton: UIView? - private var imageEditorView: ImageEditorView? - // MARK: - Initializers init(attachmentItem: SignalAttachmentItem) { @@ -954,7 +952,8 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD imageMediaView.isUserInteractionEnabled = true imageMediaView.addSubview(imageEditorView) imageEditorView.autoPinEdgesToSuperviewEdges() - self.imageEditorView = imageEditorView + + imageEditorView.addControls(to: self.mediaMessageView) } #endif diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift index a6001f1c5..9eeec7745 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift @@ -29,8 +29,70 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { notImplemented() } + // MARK: - Buttons + + private let undoButton = UIButton(type: .custom) + private let redoButton = UIButton(type: .custom) + + @objc + public func addControls(to containerView: UIView) { + configure(button: undoButton, + label: NSLocalizedString("BUTTON_UNDO", comment: "Label for undo button."), + selector: #selector(didTapUndo(sender:))) + + configure(button: redoButton, + label: NSLocalizedString("BUTTON_REDO", comment: "Label for redo button."), + selector: #selector(didTapRedo(sender:))) + + let stackView = UIStackView(arrangedSubviews: [undoButton, redoButton]) + stackView.axis = .vertical + stackView.alignment = .center + stackView.spacing = 10 + + containerView.addSubview(stackView) + stackView.autoAlignAxis(toSuperviewAxis: .horizontal) + stackView.autoPinTrailingToSuperviewMargin(withInset: 10) + + updateButtons() + } + + private func configure(button: UIButton, + label: String, + selector: Selector) { + button.setTitle(label, for: .normal) + button.setTitleColor(.white, + for: .normal) + button.setTitleColor(.gray, + for: .disabled) + button.titleLabel?.font = UIFont.ows_dynamicTypeBody.ows_mediumWeight() + button.addTarget(self, action: selector, for: .touchUpInside) + } + + private func updateButtons() { + undoButton.isEnabled = model.canUndo() + redoButton.isEnabled = model.canRedo() + } + // MARK: - Actions + @objc func didTapUndo(sender: UIButton) { + Logger.verbose("") + guard model.canUndo() else { + owsFailDebug("Can't undo.") + return + } + model.undo() + } + + @objc func didTapRedo(sender: UIButton) { + Logger.verbose("") + guard model.canRedo() else { + owsFailDebug("Can't redo.") + return + } + model.redo() + } + // These properties are non-empty while drawing a stroke. private var currentStroke: ImageEditorStrokeItem? private var currentStrokeSamples = [ImageEditorStrokeItem.StrokeSample]() @@ -103,6 +165,8 @@ public class ImageEditorView: UIView, ImageEditorModelDelegate { // TODO: We eventually want to narrow our change events // to reflect the specific item(s) which changed. updateAllContent() + + updateButtons() } // MARK: - Accessor Overrides