LANCommander/LANCommander.Launcher/UI/Depot/Index.razor

134 lines
4.1 KiB
Text

@page "/Depot"
@page "/Depot/{id:guid}"
@using LANCommander.Launcher.Models
@using LANCommander.Launcher.UI.Depot.Components
@using LANCommander.SDK.Exceptions
@implements IDisposable
@inject NavigationManager NavigationManager
@inject DepotService DepotService
@inject LibraryService LibraryService
@inject KeepAliveService KeepAliveService
@inject MessageService MessageService
@inject ILogger<Index> Logger
<Spin Spinning="Disabled">
<Layout>
<Content Class="depot">
<DepotFilter />
<DepotList OnItemSelected="@((item) => NavigationManager.NavigateTo($"/Depot/{item.Key}"))" />
</Content>
</Layout>
<DepotGameDetails @ref="DepotGameDetails" ItemId="@(SelectedItem?.Key ?? Guid.Empty)" OnClose="@(() => NavigationManager.NavigateTo("/Depot"))" />
</Spin>
<div class="logo">
<img src="assets/logo-cut.svg" />
</div>
<LANCommander.Launcher.UI.Components.Footer />
@code {
[Parameter] public Guid Id { get; set; }
DepotGameDetails DepotGameDetails;
IEnumerable<Models.ListItem> ListItems = new List<Models.ListItem>();
Models.ListItem SelectedItem { get; set; }
Settings Settings = SettingService.GetSettings();
bool Disabled = false;
protected override async Task OnInitializedAsync()
{
DepotService.OnItemsFiltered += LoadFilteredItems;
LibraryService.OnLibraryItemsUpdated += OnLibraryItemsUpdated;
KeepAliveService.ConnectionSevered += KeepAliveServiceOnConnectionSevered;
KeepAliveService.ConnectionEstablished += KeepAliveServiceOnConnectionEstablished;
KeepAliveService.ConnectionLostPermanently += KeepAliveServiceOnConnectionLostPermanently;
try
{
await DepotService.RefreshItemsAsync();
}
catch (DepotNoResultsException ex)
{
MessageService.Error("Depot results could not be fetched");
Logger?.LogError(ex, "The depot returned no results");
}
catch (Exception ex)
{
MessageService.Error("Unexpected error fetching depot results");
Logger?.LogError(ex, "Unexpected error fetching depot results");
}
}
private void KeepAliveServiceOnConnectionLostPermanently(object? sender, EventArgs e)
{
NavigationManager.NavigateTo("/");
}
private async void KeepAliveServiceOnConnectionEstablished(object? sender, EventArgs e)
{
Disabled = false;
await InvokeAsync(StateHasChanged);
}
private async void KeepAliveServiceOnConnectionSevered(object? sender, EventArgs e)
{
Disabled = true;
await InvokeAsync(StateHasChanged);
}
protected override async Task OnParametersSetAsync()
{
SelectedItem = DepotService.Items.FirstOrDefault(i => i.Key == Id);
if (SelectedItem != null && Id != Guid.Empty)
{
await DepotGameDetails.Show();
}
}
async Task LoadFilteredItems(IEnumerable<Models.ListItem> listItems)
{
ListItems = listItems;
await InvokeAsync(StateHasChanged);
}
protected Task OnLibraryItemsUpdated(IEnumerable<Models.ListItem> itemsUpdatedOrAdded, IEnumerable<Models.ListItem> itemsRemoved)
{
foreach (var item in itemsUpdatedOrAdded ?? [])
{
if (ListItems?.FirstOrDefault()?.DataItem is SDK.Models.DepotGame depotItem)
{
depotItem.InLibrary = true;
}
}
foreach (var item in itemsRemoved ?? [])
{
if (ListItems?.FirstOrDefault()?.DataItem is SDK.Models.DepotGame depotItem)
{
depotItem.InLibrary = false;
}
}
return Task.CompletedTask;
}
public void Dispose()
{
DepotService.OnItemsFiltered -= LoadFilteredItems;
LibraryService.OnLibraryItemsUpdated -= OnLibraryItemsUpdated;
KeepAliveService.ConnectionEstablished -= KeepAliveServiceOnConnectionEstablished;
KeepAliveService.ConnectionSevered -= KeepAliveServiceOnConnectionSevered;
KeepAliveService.ConnectionLostPermanently -= KeepAliveServiceOnConnectionLostPermanently;
}
}