@page "/media/libraries" @using MediatR.Courier @using ErsatzTV.Application.Libraries @using ErsatzTV.Application.Libraries.Queries @using ErsatzTV.Application.MediaSources.Commands @using ErsatzTV.Application.Plex.Commands @using ErsatzTV.Core.Metadata @using System.Threading @using ErsatzTV.Application.Jellyfin @using ErsatzTV.Application.Jellyfin.Commands @using ErsatzTV.Application.Emby @using ErsatzTV.Application.Emby.Commands @implements IDisposable @inject IMediator _mediator @inject IEntityLocker _locker @inject ChannelWriter _workerChannel @inject ChannelWriter _plexWorkerChannel @inject ChannelWriter _jellyfinWorkerChannel @inject ChannelWriter _embyWorkerChannel @inject ICourier _courier Libraries Library Kind Name Media Kind @context.LibraryKind @context.Name @context.MediaKind
@if (_locker.IsLibraryLocked(context.Id)) {
@if (_progressByLibrary[context.Id] > 0) { @($"{_progressByLibrary[context.Id]} %") }
} else {
}
@code { private IList _libraries; private Dictionary _progressByLibrary; protected override void OnInitialized() { _locker.OnLibraryChanged += LockChanged; _courier.Subscribe(HandleScanProgress); } protected override async Task OnParametersSetAsync() => await LoadLibraries(); private async Task LoadLibraries() { _libraries = await _mediator.Send(new GetAllLibraries()); _progressByLibrary = _libraries.ToDictionary(vm => vm.Id, _ => 0); } private async Task ScanLibrary(LibraryViewModel library) { if (_locker.LockLibrary(library.Id)) { switch (library) { case LocalLibraryViewModel: await _workerChannel.WriteAsync(new ForceScanLocalLibrary(library.Id)); break; case PlexLibraryViewModel: await _plexWorkerChannel.WriteAsync(new ForceSynchronizePlexLibraryById(library.Id)); break; case JellyfinLibraryViewModel: await _jellyfinWorkerChannel.WriteAsync(new ForceSynchronizeJellyfinLibraryById(library.Id)); break; case EmbyLibraryViewModel: await _embyWorkerChannel.WriteAsync(new ForceSynchronizeEmbyLibraryById(library.Id)); break; } StateHasChanged(); } } private void LockChanged(object sender, EventArgs e) => InvokeAsync(StateHasChanged); private async Task HandleScanProgress(LibraryScanProgress libraryScanProgress, CancellationToken cancellationToken) { try { if (_progressByLibrary != null && _progressByLibrary.ContainsKey(libraryScanProgress.LibraryId)) { _progressByLibrary[libraryScanProgress.LibraryId] = (int) (libraryScanProgress.Progress * 100); await InvokeAsync(StateHasChanged); } } catch (Exception) { // ignore } } void IDisposable.Dispose() { _locker.OnLibraryChanged -= LockChanged; _courier.UnSubscribe(HandleScanProgress); } }