63 lines
1.9 KiB
Swift
63 lines
1.9 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
struct SharingFilesPickerView: View {
|
|
@EnvironmentObject var store: AppStore
|
|
|
|
var body: some View {
|
|
DocumentPicker(
|
|
completion: { dataArray in
|
|
store.dispatch(.sharingAction(.showSharing(false)))
|
|
store.dispatch(.sharingAction(.shareDocuments(dataArray)))
|
|
},
|
|
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]) {
|
|
var dataArray = [Data]()
|
|
for url in didPickDocumentsAt {
|
|
do {
|
|
let data = try Data(contentsOf: url)
|
|
dataArray.append(data)
|
|
} catch {
|
|
print("Unable to load data from \(url): \(error)")
|
|
}
|
|
}
|
|
parent.completion(dataArray)
|
|
}
|
|
|
|
func documentPickerWasCancelled(_: UIDocumentPickerViewController) {
|
|
parent.cancel()
|
|
}
|
|
}
|
|
}
|