import SwiftUI import UIKit struct AttachmentFilesPickerView: View { @EnvironmentObject var store: AppStore var body: some View { DocumentPicker( completion: { arr in let sharedFiles = arr.map { ShareItem( id: UUID().uuidString, type: .file, data: $0, thumbnail: Data(), string: "" ) } store.dispatch(.conversationAction(.sendAttachment(sharedFiles))) store.dispatch(.conversationAction(.showAttachmentPicker(false))) }, cancel: { store.dispatch(.conversationAction(.showAttachmentPicker(false))) } ) } } struct DocumentPicker: UIViewControllerRepresentable { let completion: ([Data]) -> Void let cancel: () -> Void func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIDocumentPickerViewController { let picker: UIDocumentPickerViewController picker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true) picker.delegate = context.coordinator picker.allowsMultipleSelection = true return picker } func updateUIViewController(_: UIDocumentPickerViewController, context _: UIViewControllerRepresentableContext) {} func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, UIDocumentPickerDelegate { var parent: DocumentPicker init(_ parent: DocumentPicker) { self.parent = parent } func documentPicker(_: UIDocumentPickerViewController, didPickDocumentsAt _: [URL]) { // TODO: Send documents // Handle the selected files } func documentPickerWasCancelled(_: UIDocumentPickerViewController) { parent.cancel() } } }