@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 Logger @inject LocalizationService LocalizationService @code { [Parameter] public Guid Id { get; set; } DepotGameDetails DepotGameDetails; IEnumerable ListItems = new List(); 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(LocalizationService.GetString("DepotResultsCouldNotBeFetched")); Logger?.LogError(ex, "The depot returned no results"); } catch (Exception ex) { MessageService.Error(LocalizationService.GetString("UnexpectedErrorFetchingDepotResults")); 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 listItems) { ListItems = listItems; await InvokeAsync(StateHasChanged); } protected Task OnLibraryItemsUpdated(IEnumerable itemsUpdatedOrAdded, IEnumerable 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; } }