Move attachment context menu interaction to AttachmentView

This commit is contained in:
Shadowfacts 2020-03-20 22:28:23 -04:00
parent c9c001d403
commit 2e8241d734
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 17 additions and 31 deletions

View File

@ -13,11 +13,9 @@ 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)
}
}

View File

@ -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)
}
}
}

View File

@ -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)
}