Files
ersatztv/ErsatzTV/Pages/PlexLibrariesEditor.razor
T
Jason DoveandGitHub 6b44873474 add library scan progress detail (#133)
* add library scan progress detail

* scan plex libraries on plex thread
2021-04-04 10:44:10 -05:00

111 lines
3.9 KiB
Plaintext

@page "/media/sources/plex/{Id:int}/libraries"
@using ErsatzTV.Application.Plex
@using ErsatzTV.Application.Plex.Commands
@using ErsatzTV.Application.Plex.Queries
@inject IMediator Mediator
@inject NavigationManager NavigationManager
@inject ILogger<PlexLibrariesEditor> Logger
@inject ISnackbar Snackbar
@inject ChannelWriter<IPlexBackgroundServiceRequest> Channel
@inject IEntityLocker Locker
<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="pt-8">
<MudTable Hover="true" Items="_libraries" Dense="true">
<ToolBarContent>
<MudText Typo="Typo.h6"><b>@_source.Name</b> Libraries</MudText>
</ToolBarContent>
<ColGroup>
<col/>
<col/>
<col style="width: 100px;"/>
</ColGroup>
<HeaderContent>
<MudTh>
<MudTableSortLabel SortBy="new Func<PlexMediaSourceLibraryEditViewModel, object>(x => x.Name)">
Name
</MudTableSortLabel>
</MudTh>
<MudTh>
<MudTableSortLabel SortBy="new Func<PlexMediaSourceLibraryEditViewModel, 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-Checked="@context.ShouldSyncItems" Color="Color.Primary"/>
</MudTd>
</RowTemplate>
</MudTable>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(_ => SaveChanges())" Class="mt-4">
Save Changes
</MudButton>
</MudContainer>
@code {
[Parameter]
public int Id { get; set; }
private PlexMediaSourceViewModel _source;
private List<PlexMediaSourceLibraryEditViewModel> _libraries;
protected override Task OnParametersSetAsync() => LoadData();
private async Task LoadData()
{
Option<PlexMediaSourceViewModel> maybeSource = await Mediator.Send(new GetPlexMediaSourceById(Id));
await maybeSource.Match(
async source =>
{
_source = source;
_libraries = await Mediator.Send(new GetPlexLibrariesBySourceId(Id))
.Map(list => list.Map(ProjectToEditViewModel).OrderBy(x => x.MediaKind).ThenBy(x => x.Name).ToList());
},
() =>
{
NavigationManager.NavigateTo("404");
return Task.CompletedTask;
});
}
private PlexMediaSourceLibraryEditViewModel ProjectToEditViewModel(PlexLibraryViewModel library) => new()
{
Id = library.Id,
Name = library.Name,
MediaKind = library.MediaKind,
ShouldSyncItems = library.ShouldSyncItems
};
private async Task SaveChanges()
{
var request = new UpdatePlexLibraryPreferences(
_libraries.Map(l => new PlexLibraryPreference(l.Id, l.ShouldSyncItems)).ToList());
Seq<BaseError> errorMessages = await Mediator.Send(request).Map(e => e.LeftToSeq());
await errorMessages.HeadOrNone().Match(
error =>
{
Snackbar.Add($"Unexpected error saving plex libraries: {error.Value}", Severity.Error);
Logger.LogError("Unexpected error saving plex libraries: {Error}", error.Value);
return Task.CompletedTask;
},
async () =>
{
foreach (int id in _libraries.Filter(l => l.ShouldSyncItems).Map(l => l.Id))
{
if (Locker.LockLibrary(id))
{
await Channel.WriteAsync(new SynchronizePlexLibraryByIdIfNeeded(id));
}
}
NavigationManager.NavigateTo("/media/plex");
});
}
}