LANCommander/LANCommander.Launcher/UI/Components/ImportHandler.razor
Pat Hartl 6dfc0906ac Refactor launcher library importing, manifests
Switched the library importing in the launcher to work based on a queue. As a new game is added to the queue, it should find any related data and also add it to the queue. This should result in an easier to maintain importer while reducing the load on the launcher's DAL. This may result in more RAM usage while importing. Local manifests have also been refactored to match manifests in import/export LCX files, removing the old manifest format. This is a large breaking change that will probably break existing game installations. A migration will probably have to be created.
2025-11-29 18:24:27 -06:00

130 lines
No EOL
4 KiB
Text

@using LANCommander.Launcher.Models
@using LANCommander.Launcher.Services.Import
@implements IDisposable
@inject ImportManagerService ImportManagerService
@inject ImportService ImportService
@inject IMessageService MessageService
@inject ILogger<ImportHandler> Logger
@inject LocalizationService LocalizationService
@code
{
[Parameter] public EventCallback<ImportProgressEventArgs> Progress { get; set; }
public bool Importing;
int ImportStatusIndex = 0;
int ImportStatusTotal = 0;
protected override void OnInitialized()
{
ImportManagerService.OnImportRequested += Import;
}
public void Dispose()
{
if (ImportManagerService != null)
{
ImportManagerService.OnImportRequested -= Import;
}
if (ImportService != null)
{
// Clear any existing handlers
//ImportService.OnImportComplete -= OnImportCompleteHandler;
//ImportService.OnImportFailed -= OnImportFailedHandler;
//ImportService.OnImportUpdated -= OnImportUpdatedHandler;
}
}
public async Task Import()
{
if (!Importing)
{
Logger?.LogInformation("Starting import process");
// Clear any existing handlers
//ImportService.OnImportComplete -= OnImportCompleteHandler;
//ImportService.OnImportFailed -= OnImportFailedHandler;
//ImportService.OnImportUpdated -= OnImportUpdatedHandler;
// Add our handlers
//ImportService.OnImportComplete += OnImportCompleteHandler;
//ImportService.OnImportFailed += OnImportFailedHandler;
//ImportService.OnImportUpdated += OnImportUpdatedHandler;
// Now start the import process
Importing = true;
ImportStatusIndex = 0;
ImportStatusTotal = 0;
await InvokeAsync(() => NotifyProgress(ImportProgressState.Importing));
await InvokeAsync(StateHasChanged);
MessageService.Info(LocalizationService.GetString("ImportStarted"), 2.5);
Logger?.LogInformation("Starting import");
ImportService.ImportLibraryAsync(); // Call directly instead of using JS
}
}
private async Task NotifyProgress(ImportProgressState state)
{
if (!Progress.HasDelegate)
return;
await Progress.InvokeAsync(new ImportProgressEventArgs
{
State = state,
Index = ImportStatusIndex,
Total = ImportStatusTotal,
});
}
private async Task OnImportCompleteHandler()
{
Logger?.LogInformation("Import Complete handler called");
Importing = false;
ImportStatusIndex = 0;
ImportStatusTotal = 0;
NotifyProgress(ImportProgressState.Imported);
MessageService.Success(LocalizationService.GetString("ImportComplete"), 3);
}
private async Task OnImportFailedHandler(Exception ex)
{
Importing = false;
ImportStatusIndex = 0;
ImportStatusTotal = 0;
NotifyProgress(ImportProgressState.Failed);
MessageService.Error(LocalizationService.GetString("ImportFailed"), 3);
}
private async Task OnImportUpdatedHandler(ImportStatusUpdate status)
{
Logger?.LogInformation("Import Update handler called: {Index}/{Total}", status.Index, status.Total);
ImportStatusIndex = status.Index;
ImportStatusTotal = status.Total;
NotifyProgress(ImportProgressState.Updated);
}
public enum ImportProgressState
{
None = 0,
Importing,
Updated,
Imported,
Failed,
}
public class ImportProgressEventArgs
{
public int Index { get; set; } = 0;
public int Total { get; set; } = 0;
public ImportProgressState State { get; set; } = ImportProgressState.None;
public bool IsActive => State > ImportProgressState.None && State < ImportProgressState.Imported;
}
}