From a31769647eb46ef8ed3a8c7b80ad90a9154e3257 Mon Sep 17 00:00:00 2001 From: Matteo Pagani Date: Fri, 18 Jun 2021 13:41:45 +0200 Subject: [PATCH 1/4] Added pickDirectory implementation --- .../RCTDocumentPickerModule.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs b/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs index 9115c45d..5a8148dc 100644 --- a/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs +++ b/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs @@ -102,6 +102,24 @@ public async Task> Pick(JSValue options) return result; } + [ReactMethod("pickDirectory")] + public async Task PickDirectory() + { + var openFolderPicker = new FolderPicker(); + + openFolderPicker.ViewMode = PickerViewMode.List; + openFolderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; + openFolderPicker.FileTypeFilter.Add("*"); + + var result = await openFolderPicker.PickSingleFolderAsync(); + JSValueObject obj = new JSValueObject + { + { "uri", result.Path } + }; + + return obj; + } + private async Task PrepareFile(StorageFile file, bool cache, bool readContent) { string base64Content = null; From 29bf489e5a1cf142f45158680a22400b7c32f8d6 Mon Sep 17 00:00:00 2001 From: Matteo Pagani Date: Fri, 18 Jun 2021 14:39:22 +0200 Subject: [PATCH 2/4] Fixed issue with Dispatcher --- .../RCTDocumentPickerModule.cs | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs b/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs index 5a8148dc..85be0214 100644 --- a/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs +++ b/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs @@ -105,19 +105,27 @@ public async Task> Pick(JSValue options) [ReactMethod("pickDirectory")] public async Task PickDirectory() { - var openFolderPicker = new FolderPicker(); - - openFolderPicker.ViewMode = PickerViewMode.List; - openFolderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; - openFolderPicker.FileTypeFilter.Add("*"); + TaskCompletionSource tcs = new TaskCompletionSource(); - var result = await openFolderPicker.PickSingleFolderAsync(); - JSValueObject obj = new JSValueObject + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { - { "uri", result.Path } - }; + var openFolderPicker = new FolderPicker(); + + openFolderPicker.ViewMode = PickerViewMode.List; + openFolderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; + openFolderPicker.FileTypeFilter.Add("*"); - return obj; + var folder = await openFolderPicker.PickSingleFolderAsync(); + JSValueObject obj = new JSValueObject + { + { "uri", folder.Path } + }; + + tcs.SetResult(obj); + }); + + var result = await tcs.Task; + return result; } private async Task PrepareFile(StorageFile file, bool cache, bool readContent) From bc7b7e43463defd1436a14b8b56f4a82fa4ad385 Mon Sep 17 00:00:00 2001 From: Matteo Pagani Date: Fri, 18 Jun 2021 14:40:34 +0200 Subject: [PATCH 3/4] Added windows as supported platform for pickDirectory --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 2328684e..0a8ebde0 100644 --- a/index.js +++ b/index.js @@ -191,7 +191,7 @@ export default class DocumentPicker { } static pickDirectory() { - if (Platform.OS === 'android') { + if (Platform.OS === 'android' || Platform.OS === 'windows') { return RNDocumentPicker.pickDirectory(); } else { return Promise.resolve(null); From 82f7d5892a3c4d64484ff60537b3da3d1b98ccc0 Mon Sep 17 00:00:00 2001 From: Matteo Pagani Date: Fri, 18 Jun 2021 15:08:04 +0200 Subject: [PATCH 4/4] Added pickDirectory support for Windows in README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 10a4b649..2a2f7008 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ See [this](./install-old.md) ## API -#### [Android only] `DocumentPicker.pickDirectory()` +#### [Android and Windows only] `DocumentPicker.pickDirectory()` Open a system directory picker. Returns a promise that resolves to (`{ uri: string }`) of the directory selected by user. @@ -66,7 +66,7 @@ If specified, the picked file is copied to `NSCachesDirectory` / `NSDocumentDire This should help if you need to work with the file(s) later on, because by default, [the picked documents are temporary files. They remain available only until your application terminates](https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/2902364-documentpicker). This may impact performance for large files, so keep this in mind if you expect users to pick particularly large files and your app does not need immediate read access. -##### [UWP only] `readContent`:`boolean` +##### [Windows only] `readContent`:`boolean` Defaults to `false`. If `readContent` is set to true the content of the picked file/files will be read and supplied in the result object. @@ -107,7 +107,7 @@ The display name of the file. _This is normally the filename of the file, but An The file size of the document. _On Android some DocumentProviders may not provide this information for a document._ -##### [UWP only] `content`: +##### [Windows only] `content`: The base64 encoded content of the picked file if the option `readContent` was set to `true`.