From 74750b1dc910dbdd3ada999d8a6db0c4f0d9e428 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Sun, 5 Feb 2023 02:30:38 -0600 Subject: [PATCH] Semi-functioning file selector component --- LANCommander/Components/ArchiveBrowser.razor | 227 ++++++++---------- .../Components/ArchiveFileSelector.razor | 41 ++++ LANCommander/Pages/Games/Edit.razor | 15 +- 3 files changed, 155 insertions(+), 128 deletions(-) create mode 100644 LANCommander/Components/ArchiveFileSelector.razor diff --git a/LANCommander/Components/ArchiveBrowser.razor b/LANCommander/Components/ArchiveBrowser.razor index e1d1e8a..1c59f23 100644 --- a/LANCommander/Components/ArchiveBrowser.razor +++ b/LANCommander/Components/ArchiveBrowser.razor @@ -3,162 +3,107 @@ @using System.IO.Compression; @inject ArchiveService ArchiveService; -
-
-
- -
-
-
- -
- - - - - - - - - - - @if (CurrentPath != "") - { - - - - - } - - @foreach (var entry in CurrentPathEntries.OrderBy(e => !e.FullName.EndsWith('/')).ThenBy(e => e.FullName)) - { - @if (entry.FullName.EndsWith('/')) + + + + + Name + Size + Modified + @if (OnFileSelected.HasDelegate) { - - - - - - + } - else + + + + @GetFileName(context) + @ByteSize.FromBytes(context.Length) + @context.LastWriteTime + @if (OnFileSelected.HasDelegate) { - - - - - - + Select } - } - -
NameSizeModified
..
@entry.FullName.Remove(0, CurrentPath.Length)@entry.LastWriteTime
@entry.Name@ByteSize.FromBytes(entry.Length)@entry.LastWriteTime
-
- - + + + + -@code { + @code { [Parameter] public Guid ArchiveId { get; set; } + [Parameter] public Guid Archive { get; set; } + [Parameter] public EventCallback OnFileSelected { get; set; } + private IEnumerable Entries { get; set; } private IEnumerable CurrentPathEntries { get; set; } private string CurrentPath { get; set; } - private string[] BreadCrumbs { get { return CurrentPath.TrimEnd('/').Split('/'); } } + private ArchiveDirectory Root { get; set; } + private ArchiveDirectory SelectedDirectory { get; set; } protected override async Task OnInitializedAsync() { Entries = await ArchiveService.GetContents(ArchiveId); - GoToRoot(); - } - - private void GoToRoot() - { - CurrentPath = ""; - CurrentPathEntries = Entries.Where(e => e.FullName.TrimEnd('/').Split('/').Length == 1); - } - - private void GoUpLevel() - { - var parts = CurrentPath.TrimEnd('/').Split('/'); - - if (parts.Length == 1) - GoToRoot(); - else + Root = new ArchiveDirectory() { - GoToPath(String.Join('/', parts.Take(parts.Length - 1)) + "/"); + Name = "/", + FullName = "" + }; + + Root.PopulateChildren(Entries); + } + + private void ChangeDirectory(ArchiveDirectory selectedDirectory) + { + if (SelectedDirectory == null) + SelectedDirectory = selectedDirectory; + + CurrentPathEntries = Entries.Where(e => e.FullName.StartsWith(SelectedDirectory.FullName) && e.FullName != SelectedDirectory.FullName); + } + + private string GetFileName(ZipArchiveEntry entry) + { + if (String.IsNullOrWhiteSpace(entry.Name) && entry.Length == 0) + { + return entry.FullName.TrimEnd('/').Split('/').Last(); } + else + return entry.Name; } - private void GoToPath(string path) + private string GetIcon(ZipArchiveEntry entry) { - CurrentPath = path; - CurrentPathEntries = Entries.Where(e => e.FullName.StartsWith(CurrentPath) && e.FullName != CurrentPath && e.FullName.Remove(0, path.Length).TrimEnd('/').Split('/').Length == 1); - } - - private string GetIcon(string path) - { - switch (Path.GetExtension(path)) + switch (Path.GetExtension(entry.FullName)) { case "": - return "folder"; + return Icons.Material.Filled.Folder; case ".exe": - return "terminal-2"; + return Icons.Material.Filled.Terminal; case ".zip": case ".rar": case ".7z": case ".gz": case ".tar": - return "file-zip"; + return Icons.Material.Filled.FolderZip; case ".wad": case ".pk3": case ".pak": case ".cab": - return "archive"; + return Icons.Material.Filled.Token; case ".txt": case ".cfg": @@ -169,12 +114,12 @@ case ".log": case ".doc": case ".nfo": - return "file-text"; + return Icons.Custom.FileFormats.FileDocument; case ".bat": case ".ps1": case ".json": - return "file-code"; + return Icons.Custom.FileFormats.FileCode; case ".bik": case ".avi": @@ -186,26 +131,54 @@ case ".mpg": case ".mpeg": case ".flv": - return "movie"; + return Icons.Custom.FileFormats.FileVideo; case ".dll": - return "package"; + return Icons.Material.Filled.SettingsApplications; case ".scm": - return "map"; + return Icons.Material.Filled.Map; case ".hlp": - return "help"; + return Icons.Material.Filled.Help; case ".png": case ".bmp": case ".jpeg": case ".jpg": case ".gif": - return "photo"; + return Icons.Custom.FileFormats.FileImage; default: - return "file"; + return Icons.Material.Filled.InsertDriveFile; + } + } + + public class ArchiveDirectory + { + public string Name { get; set; } + public string FullName { get; set; } + public bool IsExpanded { get; set; } = false; + public bool HasChildren => Children != null && Children.Count > 0; + public HashSet Children { get; set; } = new HashSet(); + + public void PopulateChildren(IEnumerable entries) + { + var childPaths = entries.Where(e => e.FullName.StartsWith(FullName) && e.FullName.EndsWith('/')); + var directChildren = childPaths.Where(p => p.FullName != FullName && p.FullName.Substring(FullName.Length + 1).TrimEnd('/').Split('/').Length == 1); + + foreach (var directChild in directChildren) + { + var child = new ArchiveDirectory() + { + FullName = directChild.FullName, + Name = directChild.FullName.Substring(FullName.Length).TrimEnd('/') + }; + + child.PopulateChildren(entries); + + Children.Add(child); + } } } } diff --git a/LANCommander/Components/ArchiveFileSelector.razor b/LANCommander/Components/ArchiveFileSelector.razor new file mode 100644 index 0000000..e46c106 --- /dev/null +++ b/LANCommander/Components/ArchiveFileSelector.razor @@ -0,0 +1,41 @@ + + + + Select a File + + + + + + + + + + + Cancel + + + +@code { + [CascadingParameter] MudDialogInstance MudDialog { get; set; } + [Parameter] public Guid ArchiveId { get; set; } + + protected override async Task OnInitializedAsync() + { + MudDialog.Options.MaxWidth = MaxWidth.Large; + MudDialog.Options.FullWidth = true; + MudDialog.Options.FullScreen = true; + + MudDialog.SetOptions(MudDialog.Options); + } + + private void Cancel() + { + MudDialog.Cancel(); + } + + private void FileSelected(string fileName) + { + MudDialog.Close(DialogResult.Ok(fileName)); + } +} \ No newline at end of file diff --git a/LANCommander/Pages/Games/Edit.razor b/LANCommander/Pages/Games/Edit.razor index ea138e7..ca3bf8c 100644 --- a/LANCommander/Pages/Games/Edit.razor +++ b/LANCommander/Pages/Games/Edit.razor @@ -68,9 +68,22 @@ } - private void BrowseForIcon() + private async void BrowseForIcon() { + var parameters = new DialogParameters + { + ["ArchiveId"] = Game.Archives.OrderByDescending(a => a.CreatedOn).First().Id + }; + var dialog = await DialogService.ShowAsync("File Selector", parameters); + var result = await dialog.Result; + + if (!result.Canceled) + { + Game.Icon = result.Data as string; + + StateHasChanged(); + } } private async void LookupGameMetadata()