67 lines
2.1 KiB
Swift
67 lines
2.1 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
struct FilesPickerView: View {
|
|
@Environment(\.router) var router
|
|
// @EnvironmentObject var attachments: AttachmentsStore
|
|
|
|
var body: some View {
|
|
Text("dumb")
|
|
// DocumentPicker(
|
|
// completion: { dataArray, extensionsArray in
|
|
// attachments.sendDocuments(dataArray, extensionsArray)
|
|
// router.dismissEnvironment()
|
|
// },
|
|
// cancel: {
|
|
// router.dismissEnvironment()
|
|
// }
|
|
// )
|
|
}
|
|
}
|
|
|
|
struct DocumentPicker: UIViewControllerRepresentable {
|
|
let completion: ([Data], [String]) -> 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]) {
|
|
var dataArray = [Data]()
|
|
var extensionArray = [String]()
|
|
for url in didPickDocumentsAt {
|
|
do {
|
|
let data = try Data(contentsOf: url)
|
|
dataArray.append(data)
|
|
extensionArray.append(url.pathExtension)
|
|
} catch {
|
|
print("Unable to load data from \(url): \(error)")
|
|
}
|
|
}
|
|
parent.completion(dataArray, extensionArray)
|
|
}
|
|
|
|
func documentPickerWasCancelled(_: UIDocumentPickerViewController) {
|
|
parent.cancel()
|
|
}
|
|
}
|
|
}
|