54 lines
2.1 KiB
Plaintext
54 lines
2.1 KiB
Plaintext
@page "/media/sources/plex/{Id:int}/libraries"
|
|
@using ErsatzTV.Application.Plex
|
|
@using ErsatzTV.Application.MediaSources
|
|
@implements IDisposable
|
|
@inject IMediator _mediator
|
|
@inject ChannelWriter<IScannerBackgroundServiceRequest> _scannerWorkerChannel
|
|
|
|
<RemoteMediaSourceLibrariesEditor
|
|
Id="@Id"
|
|
Name="Plex"
|
|
GetUpdateLibraryRequest="GetUpdateLibraryRequest"
|
|
GetLibrariesBySourceId="GetLibrariesBySourceId"
|
|
GetMediaSourceById="GetMediaSourceById"
|
|
SynchronizeLibraryByIdIfNeeded="SynchronizeLibraryByIdIfNeeded"/>
|
|
|
|
@code {
|
|
private readonly CancellationTokenSource _cts = new();
|
|
|
|
[Parameter]
|
|
public int Id { get; set; }
|
|
|
|
private IRequest<Either<BaseError, Unit>> GetUpdateLibraryRequest(List<RemoteMediaSourceLibraryEditViewModel> libraries) =>
|
|
new UpdatePlexLibraryPreferences(
|
|
libraries.Map(l => new PlexLibraryPreference(l.Id, l.ShouldSyncItems)).ToList());
|
|
|
|
private async Task<List<RemoteMediaSourceLibraryEditViewModel>> GetLibrariesBySourceId(int mediaSourceId) =>
|
|
await _mediator.Send(new GetPlexLibrariesBySourceId(Id), _cts.Token)
|
|
.Map(list => list.Map(ProjectToEditViewModel).OrderBy(x => x.MediaKind).ThenBy(x => x.Name).ToList());
|
|
|
|
private async Task<Option<RemoteMediaSourceViewModel>> GetMediaSourceById(int mediaSourceId) =>
|
|
await _mediator.Send(new GetPlexMediaSourceById(Id), _cts.Token)
|
|
.MapT(vm => new RemoteMediaSourceViewModel(vm.Id, vm.Name, vm.Address));
|
|
|
|
public void Dispose()
|
|
{
|
|
_cts.Cancel();
|
|
_cts.Dispose();
|
|
}
|
|
|
|
private RemoteMediaSourceLibraryEditViewModel ProjectToEditViewModel(PlexLibraryViewModel library) => new()
|
|
{
|
|
Id = library.Id,
|
|
Name = library.Name,
|
|
MediaKind = library.MediaKind,
|
|
ShouldSyncItems = library.ShouldSyncItems
|
|
};
|
|
|
|
private async Task<Unit> SynchronizeLibraryByIdIfNeeded(RemoteMediaSourceLibrariesEditor.SynchronizeParameters parameters)
|
|
{
|
|
await _scannerWorkerChannel.WriteAsync(new SynchronizePlexLibraryByIdIfNeeded(parameters.LibraryId), _cts.Token);
|
|
return Unit.Default;
|
|
}
|
|
|
|
} |