@typeparam TViewModel
@typeparam TSecrets
@implements IDisposable
@using ErsatzTV.Core.Interfaces.MediaSources
@typeparam TMediaSource
@inject IMediator Mediator
@inject IDialogService Dialog
@inject IEntityLocker Locker
@if (_mediaSources.Any())
{
Disconnect @Name
}
else
{
Connect @Name
}
@if (_mediaSources.Any())
{
@if (CanEdit)
{
Edit @Name Connection
}
else if (!_isAuthorized)
{
Fix @Name Connection
}
}
@Name Media Sources
Name
Address
@context.Name
@context.Address
@code {
private CancellationTokenSource _cts;
[Parameter]
public string Name { get; set; }
[Parameter]
public IRequest> GetAllMediaSourcesCommand { get; set; }
[Parameter]
public IRequest> DisconnectCommand { get; set; }
[Parameter]
public Func RefreshLibrariesCommand { get; set; }
[Parameter]
public IRemoteMediaSourceSecretStore SecretStore { get; set; }
[Parameter]
public bool CanEdit { get; set; }
private List _mediaSources = [];
private bool _isAuthorized;
public void Dispose()
{
_cts?.Cancel();
_cts?.Dispose();
}
protected override async Task OnParametersSetAsync()
{
_cts?.Cancel();
_cts?.Dispose();
_cts = new CancellationTokenSource();
var token = _cts.Token;
try
{
await LoadMediaSources(token);
}
catch (OperationCanceledException)
{
// do nothing
}
}
private async Task LoadMediaSources(CancellationToken cancellationToken)
{
_isAuthorized = await SecretStore.ReadSecrets()
.Map(secrets => !string.IsNullOrWhiteSpace(secrets.Address) && !string.IsNullOrWhiteSpace(secrets.ApiKey));
_mediaSources = await Mediator.Send(GetAllMediaSourcesCommand, cancellationToken);
}
private async Task Disconnect()
{
var parameters = new DialogParameters { { "Name", Name } };
var options = new DialogOptions { CloseButton = true, MaxWidth = MaxWidth.Small };
IDialogReference dialog = await Dialog.ShowAsync($"Disconnect {Name}", parameters, options);
DialogResult result = await dialog.Result;
if (result is { Canceled: false })
{
if (Locker.LockRemoteMediaSource())
{
await Mediator.Send(DisconnectCommand, _cts.Token);
await LoadMediaSources(_cts.Token);
}
}
}
private async Task RefreshLibraries(int mediaSourceId, CancellationToken cancellationToken) =>
await RefreshLibrariesCommand(mediaSourceId, cancellationToken);
}