Add FolderProvider

This commit is contained in:
TheXamlGuy
2024-09-28 21:55:08 +01:00
parent 928969f39f
commit aa539c83e6
9 changed files with 84 additions and 26 deletions
+6 -6
View File
@@ -11,21 +11,21 @@ public class FileProvider(ITopLevelProvider topLevelProvider) :
{
if (topLevelProvider.Get() is TopLevel topLevel)
{
IReadOnlyList<IStorageFile> storageFiles = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
IReadOnlyList<IStorageFile> files = await topLevel.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
{
AllowMultiple = filter.AllowMultiple,
FileTypeFilter = new List<FilePickerFileType>
{
FileTypeFilter =
[
new(filter.Name)
{
Patterns = filter.Extensions is { Count: > 0 } ? filter.Extensions.Select(x => $"*.{x}").ToList() : ["*.*"]
}
}
]
});
return storageFiles.Select(file => file.Path.LocalPath).ToList();
return files.Select(x => x.Path.LocalPath).ToList();
}
return Array.Empty<string>();
return [];
}
}
+25
View File
@@ -0,0 +1,25 @@
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Toolkit.Foundation;
namespace Toolkit.Avalonia;
public class FolderProvider(ITopLevelProvider topLevelProvider) :
IFolderProvider
{
public async Task<IReadOnlyCollection<string>> SelectFolders(FolderFilter filter)
{
if (topLevelProvider.Get() is TopLevel topLevel)
{
IReadOnlyList<IStorageFolder> folders = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions()
{
AllowMultiple = filter.AllowMultiple
});
return folders.Select(x => x.Path.LocalPath).ToList();
}
return [];
}
}
@@ -126,6 +126,7 @@ public static class IServiceCollectionExtensions
{
services.AddTransient<ITopLevelProvider, TopLevelProvider>();
services.AddTransient<IFileProvider, FileProvider>();
services.AddTransient<IFolderProvider, FolderProvider>();
services.AddTransient<IClipboardWriter, ClipboardWriter>();
@@ -139,6 +140,7 @@ public static class IServiceCollectionExtensions
services.AddTransient<INavigationRegion, NavigationRegion>();
services.AddHandler<WriteClipboardHandler>();
services.AddHandler<SelectFoldersHandler>();
services.AddHandler<ClassicDesktopStyleApplicationHandler>(nameof(IClassicDesktopStyleApplicationLifetime));
services.AddHandler<SingleViewApplicationHandler>(nameof(ISingleViewApplicationLifetime));
@@ -157,6 +159,7 @@ public static class IServiceCollectionExtensions
{
services.AddTransient<ITopLevelProvider, TopLevelProvider>();
services.AddTransient<IFileProvider, FileProvider>();
services.AddTransient<IFolderProvider, FolderProvider>();
services.AddTransient<IClipboardWriter, ClipboardWriter>();
@@ -170,6 +173,7 @@ public static class IServiceCollectionExtensions
services.AddTransient<INavigationRegion, NavigationRegion>();
services.AddHandler<WriteClipboardHandler>();
services.AddHandler<SelectFoldersHandler>();
services.AddHandler<ContentControlHandler>(nameof(ContentControl));
services.AddHandler<FrameHandler>(nameof(Frame));
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Toolkit.Foundation;
public record FileFilter(string Name, List<string> Extensions, bool AllowMultiple = false);
public record FileFilter(string Name, List<string> Extensions, bool AllowMultiple = false);
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record FolderFilter(bool AllowMultiple = false);
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IFolderProvider
{
Task<IReadOnlyCollection<string>> SelectFolders(FolderFilter filter);
}
+19
View File
@@ -12,3 +12,22 @@ public record Result<TValue> :
public static implicit operator Result<TValue>(TValue? value) => Create(value);
}
public record Result(bool IsSuccess, Error Error)
{
public bool IsFailure => !IsSuccess;
public static Result Success() => new(true, Error.None);
public static Result<TValue> Success<TValue>(TValue value) => new(value, true, Error.None);
public static Result Failure(Error error) => new(false, error);
public static Result<TValue> Failure<TValue>(Error error) => new(default, false, error);
public static Result Create(bool condition) => condition ? Success() : Failure(Error.ConditionNotMet);
public static Result<TValue> Create<TValue>(TValue? value) => value is not null ? Success(value) : Failure<TValue>(Error.Null);
}
-19
View File
@@ -1,19 +0,0 @@
namespace Toolkit.Foundation;
public record Result(bool IsSuccess, Error Error)
{
public bool IsFailure => !IsSuccess;
public static Result Success() => new(true, Error.None);
public static Result<TValue> Success<TValue>(TValue value) => new(value, true, Error.None);
public static Result Failure(Error error) => new(false, error);
public static Result<TValue> Failure<TValue>(Error error) => new(default, false, error);
public static Result Create(bool condition) => condition ? Success() : Failure(Error.ConditionNotMet);
public static Result<TValue> Create<TValue>(TValue? value) => value is not null ? Success(value) : Failure<TValue>(Error.Null);
}
@@ -0,0 +1,20 @@
namespace Toolkit.Foundation;
public class SelectFoldersHandler(IFolderProvider folderProvider) :
IHandler<SelectionEventArgs<FolderFilter>, IReadOnlyCollection<string>?>
{
public async Task<IReadOnlyCollection<string>?> Handle(SelectionEventArgs<FolderFilter> args,
CancellationToken cancellationToken)
{
if (args.Sender is FolderFilter filter)
{
if (await folderProvider.SelectFolders(filter)
is { Count: > 0 } folders)
{
return folders;
}
}
return default;
}
}