Fix previewing video/audio attachments

This commit is contained in:
Shadowfacts 2020-03-20 22:48:28 -04:00
parent bef3388fe8
commit d9517047d7
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 31 additions and 7 deletions

View File

@ -12,7 +12,8 @@ import Gifu
import AVFoundation import AVFoundation
protocol AttachmentViewDelegate: class { protocol AttachmentViewDelegate: class {
func showAttachmentsGallery(startingAt index: Int) func attachmentViewGallery(startingAt index: Int) -> UIViewController
func attachmentViewPresent(_ vc: UIViewController, animated: Bool)
} }
class AttachmentView: UIImageView, GIFAnimatable { class AttachmentView: UIImageView, GIFAnimatable {
@ -153,8 +154,15 @@ class AttachmentView: UIImageView, GIFAnimatable {
updateImageIfNeeded() updateImageIfNeeded()
} }
func showGallery() {
if let delegate = delegate {
let gallery = delegate.attachmentViewGallery(startingAt: index)
delegate.attachmentViewPresent(gallery, animated: true)
}
}
@objc func imagePressed() { @objc func imagePressed() {
delegate?.showAttachmentsGallery(startingAt: index) showGallery()
} }
} }
@ -162,12 +170,21 @@ class AttachmentView: UIImageView, GIFAnimatable {
extension AttachmentView: UIContextMenuInteractionDelegate { extension AttachmentView: UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? { func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: nil, previewProvider: { () -> UIViewController? in return UIContextMenuConfiguration(identifier: nil, previewProvider: { () -> UIViewController? in
if self.attachment.kind == .image {
return AttachmentPreviewViewController(attachment: self.attachment) return AttachmentPreviewViewController(attachment: self.attachment)
} else {
return self.delegate?.attachmentViewGallery(startingAt: self.index)
}
}, actionProvider: nil) }, actionProvider: nil)
} }
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
animator.addCompletion { 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()
}
} }
} }
} }

View File

@ -366,7 +366,10 @@ class AttachmentsContainerView: UIView {
@objc func moreViewTapped() { @objc func moreViewTapped() {
guard attachments.count > 4 else { return } 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 // 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)
}
} }
} }

View File

@ -310,10 +310,14 @@ class BaseStatusTableViewCell: UITableViewCell {
} }
extension BaseStatusTableViewCell: AttachmentViewDelegate { 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!)") } guard let status = mastodonController.cache.status(for: statusID) else { fatalError("Missing cached status \(statusID!)") }
let sourceViews = status.attachments.map(attachmentsView.getAttachmentView(for:)) 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)
} }
} }