From c9c001d403a169813dd5dd55185ffbfc2d7693d1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 20 Mar 2020 22:13:04 -0400 Subject: [PATCH] Improve attachment previewing - Set correct preview size - Don't show controls --- Tusker.xcodeproj/project.pbxproj | 4 ++ .../AttachmentPreviewViewController.swift | 60 +++++++++++++++++++ .../EnhancedTableViewController.swift | 4 +- Tusker/Screens/Utilities/Previewing.swift | 22 +++++++ .../Status/BaseStatusTableViewCell.swift | 4 +- 5 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index eedafb4b..2d9893c7 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ D646C956213B365700269FB5 /* LargeImageExpandAnimationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C955213B365700269FB5 /* LargeImageExpandAnimationController.swift */; }; D646C958213B367000269FB5 /* LargeImageShrinkAnimationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C957213B367000269FB5 /* LargeImageShrinkAnimationController.swift */; }; D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C959213B5D0500269FB5 /* LargeImageInteractionController.swift */; }; + D647D92824257BEB0005044F /* AttachmentPreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D647D92724257BEB0005044F /* AttachmentPreviewViewController.swift */; }; D64BC18623C1253A000D0238 /* AssetPreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64BC18523C1253A000D0238 /* AssetPreviewViewController.swift */; }; D64BC18823C1640A000D0238 /* PinStatusActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64BC18723C1640A000D0238 /* PinStatusActivity.swift */; }; D64BC18A23C16487000D0238 /* UnpinStatusActivity.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64BC18923C16487000D0238 /* UnpinStatusActivity.swift */; }; @@ -398,6 +399,7 @@ D646C955213B365700269FB5 /* LargeImageExpandAnimationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageExpandAnimationController.swift; sourceTree = ""; }; D646C957213B367000269FB5 /* LargeImageShrinkAnimationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageShrinkAnimationController.swift; sourceTree = ""; }; D646C959213B5D0500269FB5 /* LargeImageInteractionController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageInteractionController.swift; sourceTree = ""; }; + D647D92724257BEB0005044F /* AttachmentPreviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentPreviewViewController.swift; sourceTree = ""; }; D64BC18523C1253A000D0238 /* AssetPreviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetPreviewViewController.swift; sourceTree = ""; }; D64BC18723C1640A000D0238 /* PinStatusActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PinStatusActivity.swift; sourceTree = ""; }; D64BC18923C16487000D0238 /* UnpinStatusActivity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnpinStatusActivity.swift; sourceTree = ""; }; @@ -570,6 +572,7 @@ isa = PBXGroup; children = ( 04D14BAE22B34A2800642648 /* GalleryViewController.swift */, + D647D92724257BEB0005044F /* AttachmentPreviewViewController.swift */, ); path = "Attachment Gallery"; sourceTree = ""; @@ -1698,6 +1701,7 @@ 04586B4322B301470021BD04 /* AppearancePrefsView.swift in Sources */, D67C57AF21E28EAD00C3118B /* Array+Uniques.swift in Sources */, D6945C3223AC4D36005C403C /* HashtagTimelineViewController.swift in Sources */, + D647D92824257BEB0005044F /* AttachmentPreviewViewController.swift in Sources */, D66362752137068A00C9CBA2 /* Visibility+Helpers.swift in Sources */, D6C693FC2162FE6F007D6A6D /* LoadingViewController.swift in Sources */, D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */, diff --git a/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift b/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift new file mode 100644 index 00000000..ac4177e2 --- /dev/null +++ b/Tusker/Screens/Attachment Gallery/AttachmentPreviewViewController.swift @@ -0,0 +1,60 @@ +// +// AttachmentPreviewViewController.swift +// Tusker +// +// Created by Shadowfacts on 3/20/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import UIKit +import Pachyderm +import Gifu + +class AttachmentPreviewViewController: UIViewController { + + let attachment: Attachment + let createGallery: () -> GalleryViewController + + init(attachment: Attachment, createGallery: @escaping () -> GalleryViewController) { + self.attachment = attachment + self.createGallery = createGallery + + super.init(nibName: nil, bundle: nil) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func loadView() { + if let data = ImageCache.attachments.get(attachment.url), + let image = UIImage(data: data) { + let imageView: UIImageView + if attachment.url.pathExtension == "gif" { + let gifView = GIFImageView(image: image) + gifView.animate(withGIFData: data) + imageView = gifView + } else { + imageView = UIImageView(image: image) + } + imageView.contentMode = .scaleAspectFit + imageView.backgroundColor = .black + view = imageView + preferredContentSize = image.size + } else { + view = UIActivityIndicatorView(style: .large) + } + } +} + +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/Screens/Utilities/EnhancedTableViewController.swift b/Tusker/Screens/Utilities/EnhancedTableViewController.swift index 56f54a9c..0c66bd54 100644 --- a/Tusker/Screens/Utilities/EnhancedTableViewController.swift +++ b/Tusker/Screens/Utilities/EnhancedTableViewController.swift @@ -72,8 +72,8 @@ extension EnhancedTableViewController { if let viewController = animator.previewViewController { animator.preferredCommitStyle = .pop animator.addCompletion { - if viewController is LargeImageViewController || viewController is GalleryViewController || viewController is SFSafariViewController { - self.present(viewController, animated: true) + if let customPresenting = viewController as? CustomPreviewPresenting { + customPresenting.presentFromPreview(presenter: self) } else { self.show(viewController, sender: nil) } diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 8619f971..3b85f264 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -20,6 +20,10 @@ protocol MenuPreviewProvider { } +protocol CustomPreviewPresenting { + func presentFromPreview(presenter: UIViewController) +} + extension MenuPreviewProvider { private var mastodonController: MastodonController? { navigationDelegate?.apiController } @@ -76,3 +80,21 @@ extension MenuPreviewProvider { } } + +extension LargeImageViewController: CustomPreviewPresenting { + func presentFromPreview(presenter: UIViewController) { + presenter.present(self, animated: true) + } +} + +extension GalleryViewController: CustomPreviewPresenting { + func presentFromPreview(presenter: UIViewController) { + presenter.present(self, animated: true) + } +} + +extension SFSafariViewController: CustomPreviewPresenting { + func presentFromPreview(presenter: UIViewController) { + presenter.present(self, animated: true) + } +} diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index 9c55bd0b..25aaa299 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -337,7 +337,9 @@ extension BaseStatusTableViewCell: MenuPreviewProvider { let attachments = self.attachmentsView.attachments! let sourceViews = attachments.map(self.attachmentsView.getAttachmentView(for:)) let startIndex = sourceViews.firstIndex(of: attachmentView)! - return self.delegate?.gallery(attachments: attachments, sourceViews: sourceViews, startIndex: startIndex) + return AttachmentPreviewViewController(attachment: attachments[startIndex]) { () -> GalleryViewController in + return self.delegate!.gallery(attachments: attachments, sourceViews: sourceViews, startIndex: startIndex) + } }, actions: { [] } )