import QuickLook import SwiftUI import UIKit // 🤖 Quick Look preview for a downloaded attachment. NativeChatScreen.openFile // decrypts the blob and writes its bytes to a temp URL (cleaned up on dismiss); // this hands that URL to QLPreviewController, which renders PDFs, Office docs, // text, images, … and carries a built-in action button — Save to Files, // AirDrop, open-in — the iOS equivalent of the web file-chip's browser // download. Wrapped in a UINavigationController so a Done button and Quick // Look's own share button share one bar. struct QuickLookPreview: UIViewControllerRepresentable { let url: URL let onDone: () -> Void func makeCoordinator() -> Coordinator { Coordinator(url: url, onDone: onDone) } func makeUIViewController(context: Context) -> UINavigationController { let preview = QLPreviewController() preview.dataSource = context.coordinator // QLPreviewController owns its right-hand share/action button; we add // the Done on the left (a sheet swipe-down also dismisses). preview.navigationItem.leftBarButtonItem = UIBarButtonItem( barButtonSystemItem: .done, target: context.coordinator, action: #selector(Coordinator.done)) return UINavigationController(rootViewController: preview) } func updateUIViewController(_ controller: UINavigationController, context: Context) {} final class Coordinator: NSObject, QLPreviewControllerDataSource { private let item: Item private let onDone: () -> Void init(url: URL, onDone: @escaping () -> Void) { self.item = Item(url: url) self.onDone = onDone } func numberOfPreviewItems(in controller: QLPreviewController) -> Int { 1 } func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { item } @objc func done() { onDone() } } // Quick Look keys its preview + title off the URL's filename/extension — // which AttachmentDownload.safeFilename has already made sound. private final class Item: NSObject, QLPreviewItem { let previewItemURL: URL? init(url: URL) { previewItemURL = url } } }