PulseCanvas / app / visionOS / VisionVideoSavePicker.swift
| 1 | import SwiftUI |
| 2 | import UIKit |
| 3 | |
| 4 | struct VisionVideoSaveRequest: Identifiable { |
| 5 | let id = UUID() |
| 6 | let url: URL |
| 7 | init(url: URL) throws { try VisionVideoFile.validate(url); self.url = url } |
| 8 | } |
| 9 | |
| 10 | /// Export the existing on-disk movie directly. Avoid SwiftUI's Transferable-to- |
| 11 | /// NSFileWrapper conversion, which aborts in setPreferredFilename on xrOS 27. |
| 12 | struct VisionVideoSavePicker: UIViewControllerRepresentable { |
| 13 | let request: VisionVideoSaveRequest |
| 14 | let completion: (Result<URL, Error>?) -> Void |
| 15 | func makeCoordinator() -> Coordinator { Coordinator(completion: completion) } |
| 16 | func makeUIViewController(context: Context) -> UIDocumentPickerViewController { |
| 17 | let picker = UIDocumentPickerViewController(forExporting: [request.url], asCopy: true) |
| 18 | picker.delegate = context.coordinator |
| 19 | picker.shouldShowFileExtensions = true |
| 20 | return picker |
| 21 | } |
| 22 | func updateUIViewController(_ controller: UIDocumentPickerViewController, context: Context) {} |
| 23 | final class Coordinator: NSObject, UIDocumentPickerDelegate { |
| 24 | let completion: (Result<URL, Error>?) -> Void |
| 25 | init(completion: @escaping (Result<URL, Error>?) -> Void) { self.completion = completion } |
| 26 | func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { |
| 27 | guard let url = urls.first else { completion(.failure(CocoaError(.fileWriteUnknown))); return } |
| 28 | completion(.success(url)) |
| 29 | } |
| 30 | func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { completion(nil) } |
| 31 | } |
| 32 | } |