conversations-classic-ios/ConversationsClassic/View/Screens/Attachments/AttachmentFilesPickerView.swift
2024-07-10 16:13:47 +02:00

65 lines
2 KiB
Swift

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: ""
)
}
// TODO: Send files
// store.dispatch(.conversationAction(.sendAttachment(sharedFiles)))
store.dispatch(.sharingAction(.showSharing(false)))
},
cancel: {
store.dispatch(.sharingAction(.showSharing(false)))
}
)
}
}
struct DocumentPicker: UIViewControllerRepresentable {
let completion: ([Data]) -> Void
let cancel: () -> Void
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController {
let picker: UIDocumentPickerViewController
picker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true)
picker.delegate = context.coordinator
picker.allowsMultipleSelection = true
return picker
}
func updateUIViewController(_: UIDocumentPickerViewController, context _: UIViewControllerRepresentableContext<DocumentPicker>) {}
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()
}
}
}