Skip to content

Commit

Permalink
fix(catalyst): FolderOpenPicker on UIThread
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed Feb 19, 2024
1 parent e1ae085 commit 6d56f3a
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions src/Uno.UWP/Storage/Pickers/FolderPicker.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,56 @@
using UIKit;
using Uno.Helpers.Theming;
using Windows.ApplicationModel.Core;
using Uno.UI.Dispatching;

namespace Windows.Storage.Pickers
{
public partial class FolderPicker
{
private async Task<StorageFolder?> PickSingleFolderTaskAsync(CancellationToken token)
private Task<StorageFolder?> PickSingleFolderTaskAsync(CancellationToken token)
{
var rootController = UIApplication.SharedApplication?.KeyWindow?.RootViewController;
if (rootController == null)
static async Task<StorageFolder?> PickFolderAsync()
{
throw new InvalidOperationException("Root controller not initialized yet. FolderPicker invoked too early.");
}
var rootController = UIApplication.SharedApplication?.KeyWindow?.RootViewController;
if (rootController == null)
{
throw new InvalidOperationException("Root controller not initialized yet. FolderPicker invoked too early.");
}

var documentTypes = new string[] { UTType.Folder };
using var documentPicker = new UIDocumentPickerViewController(documentTypes, UIDocumentPickerMode.Open);
var documentTypes = new string[] { UTType.Folder };
using var documentPicker = new UIDocumentPickerViewController(documentTypes, UIDocumentPickerMode.Open);

var completionSource = new TaskCompletionSource<NSUrl?>();
var completionSource = new TaskCompletionSource<NSUrl?>();

documentPicker.OverrideUserInterfaceStyle = CoreApplication.RequestedTheme == SystemTheme.Light ?
UIUserInterfaceStyle.Light : UIUserInterfaceStyle.Dark;
documentPicker.OverrideUserInterfaceStyle = CoreApplication.RequestedTheme == SystemTheme.Light ?
UIUserInterfaceStyle.Light : UIUserInterfaceStyle.Dark;

documentPicker.Delegate = new FolderPickerDelegate(completionSource);
documentPicker.Delegate = new FolderPickerDelegate(completionSource);

if (documentPicker.PresentationController != null)
{
documentPicker.PresentationController.Delegate = new FolderPickerPresentationControllerDelegate(completionSource);
}
if (documentPicker.PresentationController is not null)
{
documentPicker.PresentationController.Delegate = new FolderPickerPresentationControllerDelegate(completionSource);
}

await rootController.PresentViewControllerAsync(documentPicker, true);
await rootController.PresentViewControllerAsync(documentPicker, true);

var nsUrl = await completionSource.Task;
if (nsUrl == null)
{
return null;
var nsUrl = await completionSource.Task;
if (nsUrl is null)
{
return null;
}

return StorageFolder.GetFromSecurityScopedUrl(nsUrl, null);
}

return StorageFolder.GetFromSecurityScopedUrl(nsUrl, null);
var tcs = new TaskCompletionSource<StorageFolder?>();
NativeDispatcher.Main.Enqueue(async () =>
{
var folder = await PickFolderAsync();
tcs.SetResult(folder);
});

return tcs.Task;
}

private class FolderPickerDelegate : UIDocumentPickerDelegate
Expand Down

0 comments on commit 6d56f3a

Please sign in to comment.