LANCommander/LANCommander.Launcher/UI/Components/ImportHandler.razor

138 lines
No EOL
4.4 KiB
Text

@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.OnImportStarted.EventRaised -= OnImportStarted;
ImportService.OnImportComplete.EventRaised -= OnImportComplete;
ImportService.OnImportError.EventRaised -= OnImportError;
ImportService.OnImportStatusUpdate.EventRaised -= OnImportStatusUpdate;
}
}
public async Task Import()
{
if (!Importing)
{
Logger?.LogInformation("Starting import process");
// Clear any existing handlers
ImportService.OnImportStarted.EventRaised -= OnImportStarted;
ImportService.OnImportComplete.EventRaised -= OnImportComplete;
ImportService.OnImportError.EventRaised -= OnImportError;
ImportService.OnImportStatusUpdate.EventRaised -= OnImportStatusUpdate;
// Add our handlers
ImportService.OnImportStarted.EventRaised += OnImportStarted;
ImportService.OnImportComplete.EventRaised += OnImportComplete;
ImportService.OnImportError.EventRaised += OnImportError;
ImportService.OnImportStatusUpdate.EventRaised += OnImportStatusUpdate;
// 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 OnImportStarted(ImportStatusUpdate status)
{
await NotifyProgress(ImportProgressState.Importing);
}
private async Task OnImportComplete(ImportStatusUpdate status)
{
Logger?.LogInformation("Import Complete handler called");
Importing = false;
ImportStatusIndex = 0;
ImportStatusTotal = 0;
await NotifyProgress(ImportProgressState.Imported);
MessageService.Success(LocalizationService.GetString("ImportComplete"), 3);
}
private async Task OnImportError(ImportStatusUpdate status)
{
Importing = false;
ImportStatusIndex = 0;
ImportStatusTotal = 0;
await NotifyProgress(ImportProgressState.Failed);
MessageService.Error(LocalizationService.GetString("ImportFailed"), 3);
}
private async Task OnImportStatusUpdate(ImportStatusUpdate? status)
{
Logger?.LogInformation("Import Update handler called: {Index}/{Total}", status.Index, status.Total);
ImportStatusIndex = status.Index;
ImportStatusTotal = status.Total;
await 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;
}
}