From 402bee2d3816e8bab31dbfdea866bfa8e99623f3 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Tue, 9 Aug 2022 12:39:26 +1000 Subject: [PATCH] feat: add action for document section --- Podfile.lock | 2 +- .../AllMediaViewController.swift | 34 +++++++++++++++++++ .../DocumentTitleViewController.swift | 29 ++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Podfile.lock b/Podfile.lock index 5e750d933..37f4ac9c5 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -244,4 +244,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: f0857369c4831b2e5c1946345e76e493f3286805 -COCOAPODS: 1.11.2 +COCOAPODS: 1.11.3 diff --git a/Session/Media Viewing & Editing/AllMediaViewController.swift b/Session/Media Viewing & Editing/AllMediaViewController.swift index 121db8b43..aa7b7a072 100644 --- a/Session/Media Viewing & Editing/AllMediaViewController.swift +++ b/Session/Media Viewing & Editing/AllMediaViewController.swift @@ -34,6 +34,7 @@ public class AllMediaViewController: UIViewController, UIPageViewControllerDataS self.mediaTitleViewController = mediaTitleViewController self.documentTitleViewController = documentTitleViewController super.init(nibName: nil, bundle: nil) + self.documentTitleViewController.delegate = self } required init?(coder: NSCoder) { @@ -98,3 +99,36 @@ public class AllMediaViewController: UIViewController, UIPageViewControllerDataS dismiss(animated: true, completion: nil) } } + +// MARK: - UIDocumentInteractionControllerDelegate + +extension AllMediaViewController: UIDocumentInteractionControllerDelegate { + public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController { + return self + } +} + +// MARK: - DocumentTitleViewControllerDelegate + +extension AllMediaViewController: DocumentTileViewControllerDelegate { + public func share(fileUrl: URL) { + let shareVC = UIActivityViewController(activityItems: [ fileUrl ], applicationActivities: nil) + + if UIDevice.current.isIPad { + shareVC.excludedActivityTypes = [] + shareVC.popoverPresentationController?.permittedArrowDirections = [] + shareVC.popoverPresentationController?.sourceView = self.view + shareVC.popoverPresentationController?.sourceRect = self.view.bounds + } + + navigationController?.present(shareVC, animated: true, completion: nil) + } + + public func preview(fileUrl: URL) { + let interactionController: UIDocumentInteractionController = UIDocumentInteractionController(url: fileUrl) + interactionController.delegate = self + interactionController.presentPreview(animated: true) + } +} + + diff --git a/Session/Media Viewing & Editing/DocumentTitleViewController.swift b/Session/Media Viewing & Editing/DocumentTitleViewController.swift index 9e937ab4b..af790fb89 100644 --- a/Session/Media Viewing & Editing/DocumentTitleViewController.swift +++ b/Session/Media Viewing & Editing/DocumentTitleViewController.swift @@ -23,6 +23,8 @@ public class DocumentTileViewController: UIViewController, UITableViewDelegate, private var isAutoLoadingNextPage: Bool = false private var currentTargetOffset: CGPoint? + public var delegate: DocumentTileViewControllerDelegate? + // MARK: - Initialization init(viewModel: MediaGalleryViewModel) { @@ -313,9 +315,29 @@ public class DocumentTileViewController: UIViewController, UITableViewDelegate, public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: false) + let attachment: Attachment = self.viewModel.galleryData[indexPath.section].elements[indexPath.row].attachment + guard let originalFilePath: String = attachment.originalFilePath else { return } + + let fileUrl: URL = URL(fileURLWithPath: originalFilePath) + + // Open a preview of the document for text, pdf or microsoft files + if + attachment.isText || + attachment.isMicrosoftDoc || + attachment.contentType == OWSMimeTypeApplicationPdf + { + + delegate?.preview(fileUrl: fileUrl) + return + } + + // Otherwise share the file + delegate?.share(fileUrl: fileUrl) } } +// MARK: - View + class DocumentCell: UITableViewCell { // MARK: - Initialization @@ -472,3 +494,10 @@ class DocumentStaticHeaderView: UIView { self.label.text = title } } + +// MARK: - DocumentTitleViewControllerDelegate + +public protocol DocumentTileViewControllerDelegate: AnyObject { + func share(fileUrl: URL) + func preview(fileUrl: URL) +}