diff --git a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift index 7909d9421..6cebb6f58 100644 --- a/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift +++ b/SignalMessaging/ViewControllers/AttachmentApprovalViewController.swift @@ -49,7 +49,7 @@ class AttachmentItemCollection { attachmentItems = attachmentItems.filter { $0 != item } } - func count() -> Int { + var count: Int { return attachmentItems.count } } @@ -624,7 +624,7 @@ extension AttachmentApprovalViewController: AttachmentPrepViewControllerDelegate } func prepViewControllerAttachmentCount() -> Int { - return attachmentItemCollection.count() + return attachmentItemCollection.count } } @@ -893,24 +893,52 @@ public class AttachmentPrepViewController: OWSViewController, PlayerProgressBarD // MARK: - Navigation Bar public func navigationBarItems() -> [UIView] { + let captionButton = navigationBarButton(imageName: "image_editor_caption", + selector: #selector(didTapCaption(sender:))) + guard let imageEditorView = imageEditorView else { // Show the "add caption" button for non-image attachments if // there is more than one attachment. if let prepDelegate = prepDelegate, prepDelegate.prepViewControllerAttachmentCount() > 1 { - let captionButton = navigationBarButton(imageName: "image_editor_caption", - selector: #selector(didTapCaption(sender:))) return [captionButton] } return [] } - return imageEditorView.navigationBarItems() + var navigationBarItems = imageEditorView.navigationBarItems() + + // Show the caption UI if there's more than one attachment + // OR if the attachment already has a caption. + var shouldShowCaptionUI = attachmentCount() > 0 + if let captionText = attachmentItem.captionText, captionText.count > 0 { + shouldShowCaptionUI = true + } + if shouldShowCaptionUI { + navigationBarItems.append(captionButton) + } + + return navigationBarItems + } + + private func attachmentCount() -> Int { + guard let prepDelegate = prepDelegate else { + owsFailDebug("Missing prepDelegate.") + return 0 + } + return prepDelegate.prepViewControllerAttachmentCount() } @objc func didTapCaption(sender: UIButton) { Logger.verbose("") - imageEditorPresentCaptionView() + presentCaptionView() + } + + private func presentCaptionView() { + let view = AttachmentCaptionViewController(delegate: self, attachmentItem: attachmentItem) + self.imageEditor(presentFullScreenView: view, isTransparent: true) + + isShowingCaptionView = true } // MARK: - Event Handlers @@ -1175,24 +1203,9 @@ extension AttachmentPrepViewController: ImageEditorViewDelegate { } } - public func imageEditorPresentCaptionView() { - let view = AttachmentCaptionViewController(delegate: self, attachmentItem: attachmentItem) - self.imageEditor(presentFullScreenView: view, isTransparent: true) - - isShowingCaptionView = true - } - public func imageEditorUpdateNavigationBar() { prepDelegate?.prepViewControllerUpdateNavigationBar() } - - public func imageEditorAttachmentCount() -> Int { - guard let prepDelegate = prepDelegate else { - owsFailDebug("Missing prepDelegate.") - return 0 - } - return prepDelegate.prepViewControllerAttachmentCount() - } } // MARK: - diff --git a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift index d72d507a4..146c0a2de 100644 --- a/SignalMessaging/Views/ImageEditor/ImageEditorView.swift +++ b/SignalMessaging/Views/ImageEditor/ImageEditorView.swift @@ -8,9 +8,7 @@ import UIKit public protocol ImageEditorViewDelegate: class { func imageEditor(presentFullScreenView viewController: UIViewController, isTransparent: Bool) - func imageEditorPresentCaptionView() func imageEditorUpdateNavigationBar() - func imageEditorAttachmentCount() -> Int } // MARK: - @@ -100,8 +98,6 @@ public class ImageEditorView: UIView { selector: #selector(didTapCrop(sender:))) let newTextButton = navigationBarButton(imageName: "image_editor_text", selector: #selector(didTapNewText(sender:))) - let captionButton = navigationBarButton(imageName: "image_editor_caption", - selector: #selector(didTapCaption(sender:))) var buttons: [UIView] if model.canUndo() { @@ -110,12 +106,6 @@ public class ImageEditorView: UIView { buttons = [newTextButton, brushButton, cropButton] } - // Show the "add caption" button for non-image attachments if - // there is more than one attachment. - if let delegate = delegate, - delegate.imageEditorAttachmentCount() > 1 { - buttons.append(captionButton) - } return buttons } @@ -162,12 +152,6 @@ public class ImageEditorView: UIView { edit(textItem: textItem) } - @objc func didTapCaption(sender: UIButton) { - Logger.verbose("") - - delegate?.imageEditorPresentCaptionView() - } - @objc func didTapDone(sender: UIButton) { Logger.verbose("") }