From 2e8241d734ffce04565f42663b9cf6d1a37b5edf Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 20 Mar 2020 22:28:23 -0400 Subject: [PATCH] Move attachment context menu interaction to AttachmentView --- .../AttachmentPreviewViewController.swift | 18 ++---------------- Tusker/Views/Attachments/AttachmentView.swift | 15 +++++++++++++++ .../Views/Status/BaseStatusTableViewCell.swift | 15 --------------- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift b/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift index ac4177e2..64aee4c0 100644 --- a/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift +++ b/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift @@ -13,12 +13,10 @@ import Gifu class AttachmentPreviewViewController: UIViewController { let attachment: Attachment - let createGallery: () -> GalleryViewController - init(attachment: Attachment, createGallery: @escaping () -> GalleryViewController) { + init(attachment: Attachment) { self.attachment = attachment - self.createGallery = createGallery - + super.init(nibName: nil, bundle: nil) } @@ -46,15 +44,3 @@ class AttachmentPreviewViewController: UIViewController { } } } - -extension AttachmentPreviewViewController: TuskerNavigationDelegate { - var apiController: MastodonController { - fatalError("AttachmentPreviewViewController doesn't have a MastodonController") - } -} - -extension AttachmentPreviewViewController: CustomPreviewPresenting { - func presentFromPreview(presenter: UIViewController) { - presenter.present(createGallery(), animated: true) - } -} diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 7fcc790e..29a9c7d7 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -55,6 +55,8 @@ class AttachmentView: UIImageView, GIFAnimatable { addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(imagePressed))) NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) + + addInteraction(UIContextMenuInteraction(delegate: self)) } @objc func preferencesChanged() { @@ -156,3 +158,16 @@ class AttachmentView: UIImageView, GIFAnimatable { } } + +extension AttachmentView: UIContextMenuInteractionDelegate { + func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? { + return UIContextMenuConfiguration(identifier: nil, previewProvider: { () -> UIViewController? in + return AttachmentPreviewViewController(attachment: self.attachment) + }, actionProvider: nil) + } + func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { + animator.addCompletion { + self.delegate?.showAttachmentsGallery(startingAt: self.index) + } + } +} diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index 25aaa299..3cb0d7fd 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -329,21 +329,6 @@ extension BaseStatusTableViewCell: MenuPreviewProvider { content: { ProfileTableViewController(accountID: self.accountID, mastodonController: mastodonController) }, actions: { self.actionsForProfile(accountID: self.accountID, sourceView: self.avatarImageView) } ) - } else if attachmentsView.frame.contains(location) { - let attachmentsViewLocation = attachmentsView.convert(location, from: self) - if let attachmentView = attachmentsView.attachmentViews.allObjects.first(where: { $0.frame.contains(attachmentsViewLocation) }) { - return ( - content: { - let attachments = self.attachmentsView.attachments! - let sourceViews = attachments.map(self.attachmentsView.getAttachmentView(for:)) - let startIndex = sourceViews.firstIndex(of: attachmentView)! - return AttachmentPreviewViewController(attachment: attachments[startIndex]) { () -> GalleryViewController in - return self.delegate!.gallery(attachments: attachments, sourceViews: sourceViews, startIndex: startIndex) - } - }, - actions: { [] } - ) - } } return self.getStatusCellPreviewProviders(for: location, sourceViewController: sourceViewController) }