From d9517047d731249919c61fee7b41607ca5fb254b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 20 Mar 2020 22:48:28 -0400 Subject: [PATCH] Fix previewing video/audio attachments --- Tusker/Views/Attachments/AttachmentView.swift | 25 ++++++++++++++++--- .../AttachmentsContainerView.swift | 5 +++- .../Status/BaseStatusTableViewCell.swift | 8 ++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 29a9c7d72d..8438600072 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -12,7 +12,8 @@ import Gifu import AVFoundation protocol AttachmentViewDelegate: class { - func showAttachmentsGallery(startingAt index: Int) + func attachmentViewGallery(startingAt index: Int) -> UIViewController + func attachmentViewPresent(_ vc: UIViewController, animated: Bool) } class AttachmentView: UIImageView, GIFAnimatable { @@ -153,8 +154,15 @@ class AttachmentView: UIImageView, GIFAnimatable { updateImageIfNeeded() } + func showGallery() { + if let delegate = delegate { + let gallery = delegate.attachmentViewGallery(startingAt: index) + delegate.attachmentViewPresent(gallery, animated: true) + } + } + @objc func imagePressed() { - delegate?.showAttachmentsGallery(startingAt: index) + showGallery() } } @@ -162,12 +170,21 @@ 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) + if self.attachment.kind == .image { + return AttachmentPreviewViewController(attachment: self.attachment) + } else { + return self.delegate?.attachmentViewGallery(startingAt: self.index) + } }, actionProvider: nil) } func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { animator.addCompletion { - self.delegate?.showAttachmentsGallery(startingAt: self.index) + animator.preferredCommitStyle = .pop + if let gallery = animator.previewViewController as? GalleryViewController { + self.delegate?.attachmentViewPresent(gallery, animated: true) + } else { + self.showGallery() + } } } } diff --git a/Tusker/Views/Attachments/AttachmentsContainerView.swift b/Tusker/Views/Attachments/AttachmentsContainerView.swift index da54fd9654..99f372b9dd 100644 --- a/Tusker/Views/Attachments/AttachmentsContainerView.swift +++ b/Tusker/Views/Attachments/AttachmentsContainerView.swift @@ -366,7 +366,10 @@ class AttachmentsContainerView: UIView { @objc func moreViewTapped() { guard attachments.count > 4 else { return } // the more view shows up in place of the fourth attachemtn view, show tapping it should start at the fourth attachment - delegate?.showAttachmentsGallery(startingAt: 3) + if let delegate = delegate { + let gallery = delegate.attachmentViewGallery(startingAt: 3) + delegate.attachmentViewPresent(gallery, animated: true) + } } } diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index 267ad90477..663d581d22 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -310,10 +310,14 @@ class BaseStatusTableViewCell: UITableViewCell { } extension BaseStatusTableViewCell: AttachmentViewDelegate { - func showAttachmentsGallery(startingAt index: Int) { + func attachmentViewGallery(startingAt index: Int) -> UIViewController { guard let status = mastodonController.cache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") } let sourceViews = status.attachments.map(attachmentsView.getAttachmentView(for:)) - delegate?.showGallery(attachments: status.attachments, sourceViews: sourceViews, startIndex: index) + return delegate!.gallery(attachments: status.attachments, sourceViews: sourceViews, startIndex: index) + } + + func attachmentViewPresent(_ vc: UIViewController, animated: Bool) { + delegate?.show(vc) } }