140 lines
5.0 KiB
Plaintext
140 lines
5.0 KiB
Plaintext
@using ErsatzTV.Application.MediaSources
|
|
@implements IDisposable
|
|
@inject IMediator Mediator
|
|
@inject NavigationManager NavigationManager
|
|
@inject ILogger<RemoteMediaSourceLibrariesEditor> Logger
|
|
@inject ISnackbar Snackbar
|
|
@inject IEntityLocker Locker
|
|
|
|
<MudForm Style="max-height: 100%">
|
|
<MudPaper Square="true" Style="display: flex; height: 64px; min-height: 64px; width: 100%; z-index: 100; align-items: center">
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" Class="ml-6" StartIcon="@Icons.Material.Filled.Save" OnClick="@(_ => SaveChanges())">
|
|
Save Changes
|
|
</MudButton>
|
|
</MudPaper>
|
|
<div class="d-flex flex-column" style="height: 100vh; overflow-x: auto">
|
|
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8">
|
|
<MudText Typo="Typo.h5" Class="mb-2"><b>@_source?.Name</b> Libraries</MudText>
|
|
<MudDivider Class="mb-6"/>
|
|
<MudTable Hover="true" Items="_libraries" Dense="true">
|
|
<ColGroup>
|
|
<MudHidden Breakpoint="Breakpoint.Xs">
|
|
<col/>
|
|
<col/>
|
|
<col style="width: 100px;"/>
|
|
</MudHidden>
|
|
</ColGroup>
|
|
<HeaderContent>
|
|
<MudTh>
|
|
<MudTableSortLabel SortBy="new Func<RemoteMediaSourceLibraryEditViewModel, object>(x => x.Name)">
|
|
Name
|
|
</MudTableSortLabel>
|
|
</MudTh>
|
|
<MudTh>
|
|
<MudTableSortLabel SortBy="new Func<RemoteMediaSourceLibraryEditViewModel, object>(x => x.MediaKind)">
|
|
Media Kind
|
|
</MudTableSortLabel>
|
|
</MudTh>
|
|
<MudTh>Synchronize</MudTh>
|
|
</HeaderContent>
|
|
<RowTemplate>
|
|
<MudTd DataLabel="Name">@context.Name</MudTd>
|
|
<MudTd DataLabel="MediaType">@context.MediaKind</MudTd>
|
|
<MudTd DataLabel="Synchronize">
|
|
<MudSwitch T="bool" @bind-Value="@context.ShouldSyncItems" Color="Color.Primary"/>
|
|
</MudTd>
|
|
</RowTemplate>
|
|
</MudTable>
|
|
</MudContainer>
|
|
</div>
|
|
</MudForm>
|
|
|
|
@code {
|
|
private CancellationTokenSource _cts;
|
|
|
|
[Parameter]
|
|
public int Id { get; set; }
|
|
|
|
[Parameter]
|
|
public string Name { get; set; }
|
|
|
|
[Parameter]
|
|
public Func<int, CancellationToken, Task<Option<RemoteMediaSourceViewModel>>> GetMediaSourceById { get; set; }
|
|
|
|
[Parameter]
|
|
public Func<int, CancellationToken, Task<List<RemoteMediaSourceLibraryEditViewModel>>> GetLibrariesBySourceId { get; set; }
|
|
|
|
[Parameter]
|
|
public Func<List<RemoteMediaSourceLibraryEditViewModel>, IRequest<Either<BaseError, Unit>>> GetUpdateLibraryRequest { get; set; }
|
|
|
|
[Parameter]
|
|
public Func<SynchronizeParameters, CancellationToken, Task<Unit>> SynchronizeLibraryByIdIfNeeded { get; set; }
|
|
|
|
private RemoteMediaSourceViewModel _source;
|
|
private List<RemoteMediaSourceLibraryEditViewModel> _libraries;
|
|
|
|
public record SynchronizeParameters(int LibraryId, int MediaSourceId);
|
|
|
|
public void Dispose()
|
|
{
|
|
_cts.Cancel();
|
|
_cts.Dispose();
|
|
}
|
|
|
|
protected override Task OnParametersSetAsync() => LoadData();
|
|
|
|
private async Task LoadData()
|
|
{
|
|
_cts?.Cancel();
|
|
_cts?.Dispose();
|
|
_cts = new CancellationTokenSource();
|
|
var token = _cts.Token;
|
|
|
|
try
|
|
{
|
|
Option<RemoteMediaSourceViewModel> maybeSource = await GetMediaSourceById(Id, token);
|
|
foreach (var source in maybeSource)
|
|
{
|
|
_source = source;
|
|
_libraries = await GetLibrariesBySourceId(Id, token);
|
|
}
|
|
|
|
if (maybeSource.IsNone)
|
|
{
|
|
NavigationManager.NavigateTo("404");
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
// do nothing
|
|
}
|
|
}
|
|
|
|
private async Task SaveChanges()
|
|
{
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
IRequest<Either<BaseError, Unit>> request = GetUpdateLibraryRequest(_libraries);
|
|
Either<BaseError, Unit> result = await Mediator.Send(request, cts.Token);
|
|
foreach (var error in result.LeftToSeq())
|
|
{
|
|
Snackbar.Add($"Unexpected error saving {Name.ToLowerInvariant()} libraries: {error.Value}", Severity.Error);
|
|
Logger.LogError("Unexpected error saving {MediaSource} libraries: {Error}", Name, error.Value);
|
|
}
|
|
|
|
if (result.IsRight)
|
|
{
|
|
foreach (int libraryId in _libraries.Filter(l => l.ShouldSyncItems).Map(l => l.Id))
|
|
{
|
|
if (Locker.LockLibrary(libraryId))
|
|
{
|
|
await SynchronizeLibraryByIdIfNeeded(new SynchronizeParameters(libraryId, Id), cts.Token);
|
|
}
|
|
}
|
|
|
|
NavigationManager.NavigateTo($"media/sources/{Name.ToLowerInvariant()}");
|
|
}
|
|
}
|
|
|
|
}
|