another.im-ios/ConversationsClassic/View/Screens/Sharing/SharingFilesPickerView.swift

65 lines
2.1 KiB
Swift
Raw Normal View History

2024-07-02 09:56:27 +00:00
import SwiftUI
import UIKit
2024-07-13 13:58:38 +00:00
struct SharingFilesPickerView: View {
2024-07-10 13:00:54 +00:00
@EnvironmentObject var store: AppStore
2024-07-02 09:56:27 +00:00
var body: some View {
2024-07-10 13:00:54 +00:00
DocumentPicker(
2024-07-16 18:05:15 +00:00
completion: { dataArray, extensionsArray in
2024-07-10 14:13:47 +00:00
store.dispatch(.sharingAction(.showSharing(false)))
2024-07-16 18:05:15 +00:00
store.dispatch(.sharingAction(.shareDocuments(dataArray, extensionsArray)))
2024-07-10 13:00:54 +00:00
},
cancel: {
2024-07-10 14:13:47 +00:00
store.dispatch(.sharingAction(.showSharing(false)))
2024-07-10 13:00:54 +00:00
}
)
2024-07-02 09:56:27 +00:00
}
}
struct DocumentPicker: UIViewControllerRepresentable {
2024-07-16 18:05:15 +00:00
let completion: ([Data], [String]) -> Void
2024-07-10 13:00:54 +00:00
let cancel: () -> Void
2024-07-02 09:56:27 +00:00
func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController {
2024-07-04 11:45:39 +00:00
let picker: UIDocumentPickerViewController
picker = UIDocumentPickerViewController(forOpeningContentTypes: [.item], asCopy: true)
2024-07-02 09:56:27 +00:00
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
}
2024-07-10 14:48:18 +00:00
func documentPicker(_: UIDocumentPickerViewController, didPickDocumentsAt: [URL]) {
var dataArray = [Data]()
2024-07-16 18:05:15 +00:00
var extensionArray = [String]()
2024-07-10 14:48:18 +00:00
for url in didPickDocumentsAt {
do {
let data = try Data(contentsOf: url)
dataArray.append(data)
2024-07-16 18:05:15 +00:00
extensionArray.append(url.pathExtension)
2024-07-10 14:48:18 +00:00
} catch {
print("Unable to load data from \(url): \(error)")
}
}
2024-07-16 18:05:15 +00:00
parent.completion(dataArray, extensionArray)
2024-07-02 09:56:27 +00:00
}
func documentPickerWasCancelled(_: UIDocumentPickerViewController) {
2024-07-10 13:00:54 +00:00
parent.cancel()
2024-07-02 09:56:27 +00:00
}
}
}