Skip to content

Commit

Permalink
feat: Allow multiple selection in file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit-shah committed May 18, 2024
1 parent cad5b7a commit e3d6741
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions AetherVoice/Utilities/FilePickerUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import AppKit

class FilePickerUtility {
#if os(macOS)
static func openFilePicker(completion: @escaping (URL?) -> Void) {
static func openFilePicker(completion: @escaping ([URL]?) -> Void) {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.allowsMultipleSelection = true
panel.canChooseDirectories = false
panel.canChooseFiles = true
panel.allowedContentTypes = [.plainText, .pdf, .epub]

if panel.runModal() == .OK {
completion(panel.url)
completion(panel.urls)
} else {
completion(nil)
}
Expand Down
10 changes: 5 additions & 5 deletions AetherVoice/ViewModels/DocumentListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ class DocumentListViewModel: ObservableObject {

#if os(macOS)
func uploadDocument() {
FilePickerUtility.openFilePicker { url in
guard let url = url else { return }

// Call processDocument to handle the file
self.processDocument(at: url)
FilePickerUtility.openFilePicker { urls in
urls?.forEach { url in
// Call processDocument to handle the file
self.processDocument(at: url)
}
}
}
#endif
Expand Down
8 changes: 5 additions & 3 deletions AetherVoice/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ struct ContentView: View {
])
}
.sheet(isPresented: $showingDocumentPicker) {
DocumentPicker(allowedContentTypes: [.plainText, .pdf, .epub]) { url in
// Handle the picked document URL
viewModel.processDocument(at: url)
DocumentPicker(allowedContentTypes: [.plainText, .pdf, .epub]) { urls in
urls?.forEach { url in
// Handle the picked document URL
viewModel.processDocument(at: url)
}
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions AetherVoice/Views/DocumentPickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import UniformTypeIdentifiers
#if os(iOS)
struct DocumentPicker: UIViewControllerRepresentable {
var allowedContentTypes: [UTType]
var onPick: (URL) -> Void
var onPick: ([URL]) -> Void

func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let picker = UIDocumentPickerViewController(forOpeningContentTypes: allowedContentTypes, asCopy: true)
picker.allowsMultipleSelection = false
picker.allowsMultipleSelection = true
picker.delegate = context.coordinator
return picker
}
Expand All @@ -27,8 +27,7 @@ struct DocumentPicker: UIViewControllerRepresentable {
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first else { return }
parent.onPick(url)
parent.onPick(urls)
}
}
}
Expand Down

0 comments on commit e3d6741

Please sign in to comment.