From 9abf32b2881117bc66175d71281883e8f1cd0d3f Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Sat, 30 Mar 2024 23:47:59 +0100 Subject: [PATCH] Added select folder dialog. Added item moving as a module --- .../FileManager/FileManagerFeature.cs | 2 + .../Implementations/DeleteSelectionAction.cs | 3 +- .../Implementations/MoveContextAction.cs | 26 ++++ .../Implementations/MoveSelectionAction.cs | 33 +++++ .../UI/NewFileManager/FileManager.razor | 140 ++++++------------ 5 files changed, 111 insertions(+), 93 deletions(-) create mode 100644 Moonlight/Features/FileManager/Implementations/MoveContextAction.cs create mode 100644 Moonlight/Features/FileManager/Implementations/MoveSelectionAction.cs diff --git a/Moonlight/Features/FileManager/FileManagerFeature.cs b/Moonlight/Features/FileManager/FileManagerFeature.cs index b97cec7..482c6ca 100644 --- a/Moonlight/Features/FileManager/FileManagerFeature.cs +++ b/Moonlight/Features/FileManager/FileManagerFeature.cs @@ -53,9 +53,11 @@ public class FileManagerFeature : MoonlightFeature var pluginService = context.Application.Services.GetRequiredService(); await pluginService.RegisterImplementation(new RenameContextAction()); + await pluginService.RegisterImplementation(new MoveContextAction()); await pluginService.RegisterImplementation(new DownloadContextAction()); await pluginService.RegisterImplementation(new DeleteContextAction()); + await pluginService.RegisterImplementation(new MoveSelectionAction()); await pluginService.RegisterImplementation(new DeleteSelectionAction()); await pluginService.RegisterImplementation(new CreateFileAction()); diff --git a/Moonlight/Features/FileManager/Implementations/DeleteSelectionAction.cs b/Moonlight/Features/FileManager/Implementations/DeleteSelectionAction.cs index 0426e31..d4574ae 100644 --- a/Moonlight/Features/FileManager/Implementations/DeleteSelectionAction.cs +++ b/Moonlight/Features/FileManager/Implementations/DeleteSelectionAction.cs @@ -29,6 +29,7 @@ public class DeleteSelectionAction : IFileManagerSelectionAction await toastService.RemoveProgress("fileManagerSelectionDelete"); - await toastService.Success($"Successfully deleted selection"); + await toastService.Success("Successfully deleted selection"); + await fileManager.View.Refresh(); } } \ No newline at end of file diff --git a/Moonlight/Features/FileManager/Implementations/MoveContextAction.cs b/Moonlight/Features/FileManager/Implementations/MoveContextAction.cs new file mode 100644 index 0000000..bd19bce --- /dev/null +++ b/Moonlight/Features/FileManager/Implementations/MoveContextAction.cs @@ -0,0 +1,26 @@ +using MoonCoreUI.Services; +using Moonlight.Features.FileManager.Interfaces; +using Moonlight.Features.FileManager.Models.Abstractions.FileAccess; + +namespace Moonlight.Features.FileManager.Implementations; + +public class MoveContextAction : IFileManagerContextAction +{ + public string Name => "Move"; + public string Icon => "bx-move"; + public string Color => "info"; + public Func Filter => _ => true; + + public async Task Execute(BaseFileAccess access, UI.NewFileManager.FileManager fileManager, FileEntry entry, IServiceProvider provider) + { + await fileManager.OpenFolderSelect("Select the location to move the item to", async path => + { + var toastService = provider.GetRequiredService(); + + await access.Move(entry, path + entry.Name); + + await toastService.Success("Successfully moved item"); + await fileManager.View.Refresh(); + }); + } +} \ No newline at end of file diff --git a/Moonlight/Features/FileManager/Implementations/MoveSelectionAction.cs b/Moonlight/Features/FileManager/Implementations/MoveSelectionAction.cs new file mode 100644 index 0000000..e78fa91 --- /dev/null +++ b/Moonlight/Features/FileManager/Implementations/MoveSelectionAction.cs @@ -0,0 +1,33 @@ +using MoonCoreUI.Services; +using Moonlight.Features.FileManager.Interfaces; +using Moonlight.Features.FileManager.Models.Abstractions.FileAccess; + +namespace Moonlight.Features.FileManager.Implementations; + +public class MoveSelectionAction : IFileManagerSelectionAction +{ + public string Name => "Move"; + public string Color => "primary"; + + public async Task Execute(BaseFileAccess access, UI.NewFileManager.FileManager fileManager, FileEntry[] entries, IServiceProvider provider) + { + await fileManager.OpenFolderSelect("Select the location to move the items to", async path => + { + var toastService = provider.GetRequiredService(); + + await toastService.CreateProgress("fileManagerSelectionMove", "Moving items"); + + foreach (var entry in entries) + { + await toastService.ModifyProgress("fileManagerSelectionMove", $"Moving '{entry.Name}'"); + + await access.Move(entry, path + entry.Name); + } + + await toastService.RemoveProgress("fileManagerSelectionMove"); + + await toastService.Success("Successfully moved selection"); + await fileManager.View.Refresh(); + }); + } +} \ No newline at end of file diff --git a/Moonlight/Features/FileManager/UI/NewFileManager/FileManager.razor b/Moonlight/Features/FileManager/UI/NewFileManager/FileManager.razor index bac3e03..580996e 100644 --- a/Moonlight/Features/FileManager/UI/NewFileManager/FileManager.razor +++ b/Moonlight/Features/FileManager/UI/NewFileManager/FileManager.razor @@ -50,8 +50,8 @@ foreach (var action in SelectionActions) { var cssClass = $"btn btn-{action.Color} mx-2"; - - + + } } else @@ -100,42 +100,37 @@ else @foreach (var action in ContextActions) { - if(!action.Filter.Invoke(context)) + if (!action.Filter.Invoke(context)) continue; - + @action.Name } - - - - Move - - + } @@ -156,15 +151,14 @@ else private FileEntry FileToEdit; private bool ShowEditor = false; - // Move - private SmartModal MoveModal; - private BaseFileAccess MoveAccess; - private FileView MoveView; - private bool InMoveState = false; - private Func FolderOnlyFilter = entry => entry.IsDirectory; - private Func OnFolderClicked; - private Func OnMoveUpClicked; - private List FilesToMove = new(); + // Folder select dialog + private bool FolderSelectIsOpen = false; + private SmartModal FolderSelectModal; + private BaseFileAccess FolderSelectFileAccess; + private string FolderSelectTitle; + private Func FolderSelectResult; + private FileView FolderSelectView; + private Func FolderSelectFilter => entry => entry.IsDirectory; private Timer? UploadTokenTimer; @@ -174,17 +168,6 @@ else ContextActions = await PluginService.GetImplementations(); SelectionActions = await PluginService.GetImplementations(); CreateActions = await PluginService.GetImplementations(); - - OnFolderClicked = async entry => - { - await MoveAccess.ChangeDirectory(entry.Name); - await MoveView.Refresh(); - }; - OnMoveUpClicked = async () => - { - await MoveAccess.ChangeDirectory(".."); - await MoveView.Refresh(); - }; } protected override async Task OnAfterRenderAsync(bool firstRender) @@ -250,7 +233,7 @@ else private async Task InvokeSelectionAction(IFileManagerSelectionAction action) { await action.Execute(FileAccess, this, View.Selection, ServiceProvider); - + // Refresh resets the selection await View.Refresh(); } @@ -259,7 +242,7 @@ else { await action.Execute(FileAccess, this, ServiceProvider); } - + private async Task OnSelectionChanged(FileEntry[] _) => await InvokeAsync(StateHasChanged); #region Navigation & Refreshing @@ -306,7 +289,7 @@ else } #endregion - + #region File Editor public async Task OpenEditor(FileEntry entry) @@ -325,75 +308,48 @@ else #endregion - #region Move + #region Selects - private async Task Move(FileEntry entry) + public async Task OpenFolderSelect(string title, Func onResult) { - await View.HideContextMenu(); + if (FolderSelectIsOpen) + await HideFolderSelect(); - FilesToMove.Clear(); + FolderSelectResult = onResult; + FolderSelectTitle = title; - FilesToMove.Add(entry); + FolderSelectFileAccess = FileAccess.Clone(); + await FolderSelectFileAccess.SetDirectory("/"); - await StartMove(); + await FolderSelectModal.Show(); } - private async Task MoveSelection() + public async Task HideFolderSelect() { - FilesToMove.Clear(); - - FilesToMove.AddRange(View.Selection); - - await StartMove(); + await FolderSelectModal.Hide(); + FolderSelectIsOpen = false; + FolderSelectFileAccess.Dispose(); } - private async Task StartMove() + private async Task SubmitFolderSelect() { - // Cleanup if modal was removed in any other way - if (InMoveState) - await HideMove(); + var path = await FolderSelectFileAccess.GetCurrentDirectory(); - // Prepare file access and show modal - InMoveState = true; - await InvokeAsync(StateHasChanged); + await HideFolderSelect(); - MoveAccess = FileAccess.Clone(); - await MoveAccess.SetDirectory("/"); - - await MoveModal.Show(); + await FolderSelectResult.Invoke(path); } - private async Task HideMove() + private async Task NavigateUpFolderSelect() { - await MoveModal.Hide(); - MoveAccess.Dispose(); - - InMoveState = false; - await InvokeAsync(StateHasChanged); + await FolderSelectFileAccess.ChangeDirectory(".."); + await FolderSelectView.Refresh(); } - private async Task FinishMove() + private async Task EntryClickFolderSelect(FileEntry entry) { - var target = await MoveAccess.GetCurrentDirectory(); - - await HideMove(); - - await ToastService.CreateProgress("fileManagerMoveFile", "Moving items"); - - var i = 1; - foreach (var entry in FilesToMove) - { - await ToastService.ModifyProgress("fileManagerMoveFile", $"[{i}/{FilesToMove.Count}] Moving items"); - await FileAccess.Move(entry, target + entry.Name); - - i++; - } - - await ToastService.RemoveProgress("fileManagerMoveFile"); - - await ToastService.Success($"Successfully moved {FilesToMove.Count} items"); - - await View.Refresh(); + await FolderSelectFileAccess.ChangeDirectory(entry.Name); + await FolderSelectView.Refresh(); } #endregion