From 98bfe90605e31de1ca80abe64ae1501a42d4eceb Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Thu, 6 Feb 2025 20:54:03 -0600 Subject: [PATCH] Show import progress in popover --- .../ImportProgress.cs | 9 ++ .../Extensions/ServiceCollectionExtensions.cs | 1 + .../ImportManagerService.cs | 22 +++ .../ImportService.cs | 27 +++- LANCommander.Launcher/Styles/_import.scss | 36 +++++ LANCommander.Launcher/Styles/app.scss | 3 +- .../Authenticate/Components/LoginForm.razor | 7 +- .../Components/RegistrationForm.razor | 5 +- .../UI/Components/ImportProgressDisplay.razor | 37 +++++ LANCommander.Launcher/UI/MainLayout.razor | 129 +++++++++++++----- 10 files changed, 227 insertions(+), 49 deletions(-) create mode 100644 LANCommander.Launcher.Models/ImportProgress.cs create mode 100644 LANCommander.Launcher.Services/ImportManagerService.cs create mode 100644 LANCommander.Launcher/Styles/_import.scss create mode 100644 LANCommander.Launcher/UI/Components/ImportProgressDisplay.razor diff --git a/LANCommander.Launcher.Models/ImportProgress.cs b/LANCommander.Launcher.Models/ImportProgress.cs new file mode 100644 index 00000000..9af3c187 --- /dev/null +++ b/LANCommander.Launcher.Models/ImportProgress.cs @@ -0,0 +1,9 @@ +namespace LANCommander.Launcher.Models; + +public class ImportProgress +{ + public int Index { get; set; } + public int Total { get; set; } + public ImportItem CurrentItem { get; set; } + public bool IsImporting { get; set; } +} \ No newline at end of file diff --git a/LANCommander.Launcher.Services/Extensions/ServiceCollectionExtensions.cs b/LANCommander.Launcher.Services/Extensions/ServiceCollectionExtensions.cs index 541bc4b7..04cd0b56 100644 --- a/LANCommander.Launcher.Services/Extensions/ServiceCollectionExtensions.cs +++ b/LANCommander.Launcher.Services/Extensions/ServiceCollectionExtensions.cs @@ -59,6 +59,7 @@ namespace LANCommander.Launcher.Services.Extensions services.AddSingleton(); #endregion + services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddScoped(); diff --git a/LANCommander.Launcher.Services/ImportManagerService.cs b/LANCommander.Launcher.Services/ImportManagerService.cs new file mode 100644 index 00000000..befa4c5b --- /dev/null +++ b/LANCommander.Launcher.Services/ImportManagerService.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; + +namespace LANCommander.Launcher.Services; + +public class ImportManagerService +{ + private readonly ImportService ImportService; + + public ImportManagerService(ImportService importService) + { + ImportService = importService; + } + + public delegate Task OnImportRequestedHandler(); + public event OnImportRequestedHandler OnImportRequested; + + public async Task RequestImport() + { + if (OnImportRequested != null) + await OnImportRequested.Invoke(); + } +} \ No newline at end of file diff --git a/LANCommander.Launcher.Services/ImportService.cs b/LANCommander.Launcher.Services/ImportService.cs index 3aef5a3e..39a259f8 100644 --- a/LANCommander.Launcher.Services/ImportService.cs +++ b/LANCommander.Launcher.Services/ImportService.cs @@ -40,6 +40,9 @@ namespace LANCommander.Launcher.Services private readonly Settings Settings; private readonly DatabaseContext DatabaseContext; + private ImportProgress _importProgress = new(); + public ImportProgress Progress => _importProgress; + public delegate Task OnImportUpdatedHandler(ImportStatusUpdate update); public event OnImportUpdatedHandler OnImportUpdated; @@ -369,9 +372,11 @@ namespace LANCommander.Launcher.Services { var library = await LibraryService.GetByUserAsync(AuthenticationService.GetUserId()); - Logger?.LogInformation("Importing games"); + Logger?.LogInformation("Starting library import"); - int i = 1; + _importProgress.IsImporting = true; + _importProgress.Index = 1; + _importProgress.Total = games.Count(); foreach (var game in games) { @@ -379,13 +384,17 @@ namespace LANCommander.Launcher.Services { var remoteGame = await Client.Games.GetAsync(game.Id); + _importProgress.CurrentItem = new ImportItem(game.Id, game.Name); + if (OnImportUpdated != null) - await OnImportUpdated(new ImportStatusUpdate + { + await OnImportUpdated.Invoke(new ImportStatusUpdate { - CurrentItem = new ImportItem(game.Id, game.Name), - Index = i, - Total = games.Count() + Index = _importProgress.Index, + Total = _importProgress.Total, + CurrentItem = _importProgress.CurrentItem }); + } if (game != null && remoteGame != null) { @@ -402,9 +411,13 @@ namespace LANCommander.Launcher.Services } finally { - i++; + _importProgress.Index++; } } + + _importProgress.IsImporting = false; + if (OnImportComplete != null) + await OnImportComplete.Invoke(); Logger?.LogInformation("Importing games completed"); } diff --git a/LANCommander.Launcher/Styles/_import.scss b/LANCommander.Launcher/Styles/_import.scss new file mode 100644 index 00000000..ca0cc24e --- /dev/null +++ b/LANCommander.Launcher/Styles/_import.scss @@ -0,0 +1,36 @@ +.import-progress { + width: 300px; + + &-info { + display: flex; + flex-direction: column; + } + + &-status { + position: relative; + white-space: nowrap; + overflow: hidden; + max-width: 100%; + font-size: 14px; + + &::after { + content: ''; + position: absolute; + right: 0; + top: 0; + height: 100%; + width: 32px; + background: linear-gradient(to right, rgba(31, 31, 31, 0), rgb(31, 31, 31, 1) 90%); + pointer-events: none; + } + } + + &-progress-bar { + margin: 4px 0; + } + + &-footer { + text-align: center; + font-size: 12px; + } +} \ No newline at end of file diff --git a/LANCommander.Launcher/Styles/app.scss b/LANCommander.Launcher/Styles/app.scss index 976cd6cd..2648e392 100644 --- a/LANCommander.Launcher/Styles/app.scss +++ b/LANCommander.Launcher/Styles/app.scss @@ -23,4 +23,5 @@ @import '_download-queue'; @import '_game-actions-dialog'; @import '_install-dialog'; -@import '_checkbox-button-group'; \ No newline at end of file +@import '_checkbox-button-group'; +@import '_import'; \ No newline at end of file diff --git a/LANCommander.Launcher/UI/Authenticate/Components/LoginForm.razor b/LANCommander.Launcher/UI/Authenticate/Components/LoginForm.razor index 6ebc4f38..ce3f9beb 100644 --- a/LANCommander.Launcher/UI/Authenticate/Components/LoginForm.razor +++ b/LANCommander.Launcher/UI/Authenticate/Components/LoginForm.razor @@ -4,6 +4,7 @@ @inject NavigationManager NavigationManager @inject IMessageService MessageService @inject ILogger Logger +@inject ImportManagerService ImportManagerService @@ -100,9 +101,11 @@ { await AuthenticationService.Login(Client.GetServerAddress(), Model.UserName, Model.Password); - MainLayout.Import(); + await ImportManagerService.RequestImport(); NavigationManager.NavigateTo("/"); + + MessageService.Success("Welcome back!"); } catch (Exception ex) { @@ -128,7 +131,7 @@ { await AuthenticationService.Login(ServerAddress, token); - MainLayout.Import(); + await ImportManagerService.RequestImport(); NavigationManager.NavigateTo("/"); } diff --git a/LANCommander.Launcher/UI/Authenticate/Components/RegistrationForm.razor b/LANCommander.Launcher/UI/Authenticate/Components/RegistrationForm.razor index e3c468fa..38cb65b2 100644 --- a/LANCommander.Launcher/UI/Authenticate/Components/RegistrationForm.razor +++ b/LANCommander.Launcher/UI/Authenticate/Components/RegistrationForm.razor @@ -4,6 +4,7 @@ @inject NavigationManager NavigationManager @inject IMessageService MessageService @inject ILogger Logger +@inject ImportManagerService ImportManagerService @@ -54,7 +55,7 @@ { await AuthenticationService.Register(Client.GetServerAddress(), Model.UserName, Model.Password, Model.PasswordConfirmation); - MainLayout.Import(); + await ImportManagerService.RequestImport(); NavigationManager.NavigateTo("/"); } @@ -75,7 +76,7 @@ { await AuthenticationService.Login(ServerAddress, token); - MainLayout.Import(); + await ImportManagerService.RequestImport(); NavigationManager.NavigateTo("/"); } diff --git a/LANCommander.Launcher/UI/Components/ImportProgressDisplay.razor b/LANCommander.Launcher/UI/Components/ImportProgressDisplay.razor new file mode 100644 index 00000000..e9282ffe --- /dev/null +++ b/LANCommander.Launcher/UI/Components/ImportProgressDisplay.razor @@ -0,0 +1,37 @@ +@using LANCommander.Launcher.Models +@inject ImportService ImportService + +
+
+ +
+
+ @if (Total > 0) + { + Importing: @(CurrentItem?.Name ?? "") + } + else + { + Starting import... + } +
+
+ +
+ +
+
+
+
+ +@code { + [Parameter] public int Index { get; set; } + [Parameter] public int Total { get; set; } + [Parameter] public ImportItem CurrentItem { get; set; } +} \ No newline at end of file diff --git a/LANCommander.Launcher/UI/MainLayout.razor b/LANCommander.Launcher/UI/MainLayout.razor index 963ceeed..3b0e9046 100644 --- a/LANCommander.Launcher/UI/MainLayout.razor +++ b/LANCommander.Launcher/UI/MainLayout.razor @@ -12,6 +12,8 @@ @inject ModalService ModalService @inject LANCommander.SDK.Client LANCommander @inject IJSRuntime JS +@inject ILogger Logger +@inject ImportManagerService ImportManagerService @@ -19,14 +21,25 @@ - - -