| 1 | import QuickLook |
| 2 | import SwiftUI |
| 3 | import UIKit |
| 4 | |
| 5 | // 🤖 Quick Look preview for a downloaded attachment. NativeChatScreen.openFile |
| 6 | // decrypts the blob and writes its bytes to a temp URL (cleaned up on dismiss); |
| 7 | // this hands that URL to QLPreviewController, which renders PDFs, Office docs, |
| 8 | // text, images, … and carries a built-in action button — Save to Files, |
| 9 | // AirDrop, open-in — the iOS equivalent of the web file-chip's browser |
| 10 | // download. Wrapped in a UINavigationController so a Done button and Quick |
| 11 | // Look's own share button share one bar. |
| 12 | struct QuickLookPreview: UIViewControllerRepresentable { |
| 13 | let url: URL |
| 14 | let onDone: () -> Void |
| 15 | |
| 16 | func makeCoordinator() -> Coordinator { Coordinator(url: url, onDone: onDone) } |
| 17 | |
| 18 | func makeUIViewController(context: Context) -> UINavigationController { |
| 19 | let preview = QLPreviewController() |
| 20 | preview.dataSource = context.coordinator |
| 21 | // QLPreviewController owns its right-hand share/action button; we add |
| 22 | // the Done on the left (a sheet swipe-down also dismisses). |
| 23 | preview.navigationItem.leftBarButtonItem = UIBarButtonItem( |
| 24 | barButtonSystemItem: .done, target: context.coordinator, |
| 25 | action: #selector(Coordinator.done)) |
| 26 | return UINavigationController(rootViewController: preview) |
| 27 | } |
| 28 | |
| 29 | func updateUIViewController(_ controller: UINavigationController, context: Context) {} |
| 30 | |
| 31 | final class Coordinator: NSObject, QLPreviewControllerDataSource { |
| 32 | private let item: Item |
| 33 | private let onDone: () -> Void |
| 34 | |
| 35 | init(url: URL, onDone: @escaping () -> Void) { |
| 36 | self.item = Item(url: url) |
| 37 | self.onDone = onDone |
| 38 | } |
| 39 | |
| 40 | func numberOfPreviewItems(in controller: QLPreviewController) -> Int { 1 } |
| 41 | func previewController(_ controller: QLPreviewController, |
| 42 | previewItemAt index: Int) -> QLPreviewItem { item } |
| 43 | |
| 44 | @objc func done() { onDone() } |
| 45 | } |
| 46 | |
| 47 | // Quick Look keys its preview + title off the URL's filename/extension — |
| 48 | // which AttachmentDownload.safeFilename has already made sound. |
| 49 | private final class Item: NSObject, QLPreviewItem { |
| 50 | let previewItemURL: URL? |
| 51 | init(url: URL) { previewItemURL = url } |
| 52 | } |
| 53 | } |
| 54 | |