@using LANCommander.Launcher.Services.Import @implements IDisposable @inject ImportManagerService ImportManagerService @inject ImportService ImportService @inject IMessageService MessageService @inject ILogger Logger @inject LocalizationService LocalizationService @code { [Parameter] public EventCallback 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 -= ImportStarted; ImportService.OnImportComplete -= OnImportComplete; ImportService.OnImportError -= OnImportFailed; ImportService.OnImportStatusUpdate -= OnImportStatusUpdate; } } public async Task Import() { if (!Importing) { Logger?.LogInformation("Starting import process"); // Clear any existing handlers ImportService.OnImportStarted -= ImportStarted; ImportService.OnImportComplete -= OnImportComplete; ImportService.OnImportError -= OnImportFailed; ImportService.OnImportStatusUpdate -= OnImportStatusUpdate; // Add our handlers ImportService.OnImportStarted += ImportStarted; ImportService.OnImportComplete += OnImportComplete; ImportService.OnImportError += OnImportFailed; ImportService.OnImportStatusUpdate += 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 void ImportStarted(object? sender, EventArgs e) { NotifyProgress(ImportProgressState.Importing); } private async void OnImportComplete(object? sender, EventArgs e) { Logger?.LogInformation("Import Complete handler called"); Importing = false; ImportStatusIndex = 0; ImportStatusTotal = 0; NotifyProgress(ImportProgressState.Imported); MessageService.Success(LocalizationService.GetString("ImportComplete"), 3); } private async void OnImportFailed(object? sender, Exception? ex) { Importing = false; ImportStatusIndex = 0; ImportStatusTotal = 0; NotifyProgress(ImportProgressState.Failed); MessageService.Error(LocalizationService.GetString("ImportFailed"), 3); } private async void OnImportStatusUpdate(object? sender, 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; } }