Skip to content

Commit

Permalink
misc: chore: Multi/Single file/folder picker extensions (for convenie…
Browse files Browse the repository at this point in the history
…nce)

The result of these extensions is an empty Optional when the user hits Cancel on the shown file picker.
  • Loading branch information
GreemDev committed Jan 22, 2025
1 parent fe9d8d0 commit 9089c4f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/Ryujinx/Common/ApplicationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Avalonia.Controls.Notifications;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using Gommon;
using LibHac;
using LibHac.Account;
using LibHac.Common;
Expand All @@ -15,6 +15,7 @@
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Ava.UI.Controls;
using Ryujinx.Ava.UI.Helpers;
using Ryujinx.Ava.Utilities;
using Ryujinx.Ava.Utilities.Configuration;
using Ryujinx.Common.Helper;
using Ryujinx.Common.Logging;
Expand Down Expand Up @@ -418,35 +419,27 @@ public static void ExtractAoc(string destination, string updateFilePath, string

public static async Task ExtractAoc(IStorageProvider storageProvider, string updateFilePath, string updateName)
{
var result = await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
Optional<IStorageFolder> result = await storageProvider.OpenSingleFolderPickerAsync(new FolderPickerOpenOptions
{
Title = LocaleManager.Instance[LocaleKeys.FolderDialogExtractTitle],
AllowMultiple = false,
Title = LocaleManager.Instance[LocaleKeys.FolderDialogExtractTitle]
});

if (result.Count == 0)
{
return;
}

ExtractAoc(result[0].Path.LocalPath, updateFilePath, updateName);
if (!result.HasValue) return;

ExtractAoc(result.Value.Path.LocalPath, updateFilePath, updateName);
}


public static async Task ExtractSection(IStorageProvider storageProvider, NcaSectionType ncaSectionType, string titleFilePath, string titleName, int programIndex = 0)
{
var result = await storageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
Optional<IStorageFolder> result = await storageProvider.OpenSingleFolderPickerAsync(new FolderPickerOpenOptions
{
Title = LocaleManager.Instance[LocaleKeys.FolderDialogExtractTitle],
AllowMultiple = false,
Title = LocaleManager.Instance[LocaleKeys.FolderDialogExtractTitle]
});

if (result.Count == 0)
{
return;
}
if (!result.HasValue) return;

ExtractSection(result[0].Path.LocalPath, ncaSectionType, titleFilePath, titleName, programIndex);
ExtractSection(result.Value.Path.LocalPath, ncaSectionType, titleFilePath, titleName, programIndex);
}

public static (Result? result, bool canceled) CopyDirectory(FileSystemClient fs, string sourcePath, string destPath, CancellationToken token)
Expand Down
47 changes: 47 additions & 0 deletions src/Ryujinx/Utilities/StorageProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Avalonia.Platform.Storage;
using Gommon;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ryujinx.Ava.Utilities
{
public static class StorageProviderExtensions
{
public static async Task<Optional<IStorageFolder>> OpenSingleFolderPickerAsync(this IStorageProvider storageProvider, FolderPickerOpenOptions openOptions = null) =>
await storageProvider.OpenFolderPickerAsync(FixOpenOptions(openOptions, false))
.Then(folders => folders.FindFirst());

public static async Task<Optional<IStorageFile>> OpenSingleFilePickerAsync(this IStorageProvider storageProvider, FilePickerOpenOptions openOptions = null) =>
await storageProvider.OpenFilePickerAsync(FixOpenOptions(openOptions, false))
.Then(files => files.FindFirst());

public static async Task<Optional<IReadOnlyList<IStorageFolder>>> OpenMultiFolderPickerAsync(this IStorageProvider storageProvider, FolderPickerOpenOptions openOptions = null) =>
await storageProvider.OpenFolderPickerAsync(FixOpenOptions(openOptions, true))
.Then(folders => folders.Count > 0 ? Optional.Of(folders) : default);

public static async Task<Optional<IReadOnlyList<IStorageFile>>> OpenMultiFilePickerAsync(this IStorageProvider storageProvider, FilePickerOpenOptions openOptions = null) =>
await storageProvider.OpenFilePickerAsync(FixOpenOptions(openOptions, true))
.Then(files => files.Count > 0 ? Optional.Of(files) : default);

private static FilePickerOpenOptions FixOpenOptions(this FilePickerOpenOptions openOptions, bool allowMultiple)
{
if (openOptions is null)
return new FilePickerOpenOptions { AllowMultiple = allowMultiple };

openOptions.AllowMultiple = allowMultiple;

return openOptions;
}

private static FolderPickerOpenOptions FixOpenOptions(this FolderPickerOpenOptions openOptions, bool allowMultiple)
{
if (openOptions is null)
return new FolderPickerOpenOptions { AllowMultiple = allowMultiple };

openOptions.AllowMultiple = allowMultiple;

return openOptions;
}
}
}

0 comments on commit 9089c4f

Please sign in to comment.