feat: add action for document section

This commit is contained in:
ryanzhao 2022-08-09 12:39:26 +10:00
parent ab3bbab656
commit 402bee2d38
3 changed files with 64 additions and 1 deletions

View File

@ -244,4 +244,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: f0857369c4831b2e5c1946345e76e493f3286805
COCOAPODS: 1.11.2
COCOAPODS: 1.11.3

View File

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

View File

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