From b396a6d8764110e00491052b302989ae082d2075 Mon Sep 17 00:00:00 2001 From: Jason Barrie Morley Date: Thu, 10 Nov 2022 22:15:19 +0000 Subject: [PATCH 1/2] Support opening files in-place This change provides a mechanism to pass the `asCopy` parameter to the underlying `UIDocumentPickerViewController` to allow opening files in-place, required for opening folders (`UTType.folder`). --- Sources/FilePicker/FilePicker.swift | 6 ++++-- Sources/FilePicker/FilePickerUIRepresentable.swift | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/FilePicker/FilePicker.swift b/Sources/FilePicker/FilePicker.swift index 8febfe1..c3e8267 100644 --- a/Sources/FilePicker/FilePicker.swift +++ b/Sources/FilePicker/FilePicker.swift @@ -34,12 +34,14 @@ public struct FilePicker: View { public let types: [UTType] public let allowMultiple: Bool + public let asCopy: Bool public let pickedCompletionHandler: PickedURLsCompletionHandler public let labelViewContent: LabelViewContent - public init(types: [UTType], allowMultiple: Bool, onPicked completionHandler: @escaping PickedURLsCompletionHandler, @ViewBuilder label labelViewContent: @escaping LabelViewContent) { + public init(types: [UTType], allowMultiple: Bool, asCopy: Bool = true, onPicked completionHandler: @escaping PickedURLsCompletionHandler, @ViewBuilder label labelViewContent: @escaping LabelViewContent) { self.types = types self.allowMultiple = allowMultiple + self.asCopy = asCopy self.pickedCompletionHandler = completionHandler self.labelViewContent = labelViewContent } @@ -61,7 +63,7 @@ public struct FilePicker: View { ) .disabled(isPresented) .sheet(isPresented: $isPresented) { - FilePickerUIRepresentable(types: types, allowMultiple: allowMultiple, onPicked: pickedCompletionHandler) + FilePickerUIRepresentable(types: types, allowMultiple: allowMultiple, asCopy: asCopy, onPicked: pickedCompletionHandler) } } diff --git a/Sources/FilePicker/FilePickerUIRepresentable.swift b/Sources/FilePicker/FilePickerUIRepresentable.swift index f877c31..dace299 100644 --- a/Sources/FilePicker/FilePickerUIRepresentable.swift +++ b/Sources/FilePicker/FilePickerUIRepresentable.swift @@ -35,11 +35,13 @@ public struct FilePickerUIRepresentable: UIViewControllerRepresentable { public let types: [UTType] public let allowMultiple: Bool + public let asCopy: Bool public let pickedCompletionHandler: PickedURLsCompletionHandler - public init(types: [UTType], allowMultiple: Bool, onPicked completionHandler: @escaping PickedURLsCompletionHandler) { + public init(types: [UTType], allowMultiple: Bool, asCopy: Bool, onPicked completionHandler: @escaping PickedURLsCompletionHandler) { self.types = types self.allowMultiple = allowMultiple + self.asCopy = asCopy self.pickedCompletionHandler = completionHandler } @@ -48,7 +50,7 @@ public struct FilePickerUIRepresentable: UIViewControllerRepresentable { } public func makeUIViewController(context: Context) -> UIDocumentPickerViewController { - let picker = UIDocumentPickerViewController(forOpeningContentTypes: types, asCopy: true) + let picker = UIDocumentPickerViewController(forOpeningContentTypes: types, asCopy: asCopy) picker.delegate = context.coordinator picker.allowsMultipleSelection = allowMultiple return picker From ebfcbec2523209604cee1abeed582a06b7104698 Mon Sep 17 00:00:00 2001 From: Jason Barrie Morley Date: Fri, 11 Nov 2022 01:14:50 +0000 Subject: [PATCH 2/2] Add `asCopy` parameter to the convenience initializer. --- Sources/FilePicker/FilePicker.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/FilePicker/FilePicker.swift b/Sources/FilePicker/FilePicker.swift index c3e8267..02d23b5 100644 --- a/Sources/FilePicker/FilePicker.swift +++ b/Sources/FilePicker/FilePicker.swift @@ -46,8 +46,8 @@ public struct FilePicker: View { self.labelViewContent = labelViewContent } - public init(types: [UTType], allowMultiple: Bool, title: String, onPicked completionHandler: @escaping PickedURLsCompletionHandler) where LabelView == Text { - self.init(types: types, allowMultiple: allowMultiple, onPicked: completionHandler) { Text(title) } + public init(types: [UTType], allowMultiple: Bool, title: String, asCopy: Bool = true, onPicked completionHandler: @escaping PickedURLsCompletionHandler) where LabelView == Text { + self.init(types: types, allowMultiple: allowMultiple, asCopy: asCopy, onPicked: completionHandler) { Text(title) } } #if os(iOS)