LANCommander/LANCommander.Client/UI/Library/Index.razor

190 lines
6.7 KiB
Text

@page "/"
@using LANCommander.Client.Models
@using System.Diagnostics
@using LANCommander.SDK.Helpers
@inject SDK.Client Client
@inject NavigationManager NavigationManager
@inject LibraryService LibraryService
@inject DownloadService DownloadService
@inject GameService GameService
@inject ModalService ModalService
@inject ImportService ImportService
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Download" OnClick="() => Import()">Import</Button>
<SplitPane Class="library">
<Pane Size="25%" Class="library-list">
<Collapse>
@foreach (var collection in LibraryItems.Where(i => i != null && i.DataItem is Data.Models.Collection))
{
<Panel Header="@collection.Name" Key="@collection.Key.ToString()">
<AntList DataSource="@collection.Children" TItem="LibraryItem" Size="small">
<ListItem OnClick="() => OnLibraryItemSelected(context)">
@context.Name
</ListItem>
</AntList>
</Panel>
}
</Collapse>
</Pane>
<Pane Class="game-details">
@if (SelectedGame != null)
{
<div class="game-hero">
<h1>@SelectedGame.Name</h1>
</div>
<div class="game-infobar">
@if (SelectedGame.State == LibraryItemState.NotInstalled)
{
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Download" OnClick="() => Install(SelectedGame)">Install</Button>
}
else if (SelectedGame.State == LibraryItemState.Installed)
{
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.CaretRight" OnClick="() => Run(SelectedGame)">Play</Button>
<DropdownButton Icon="@IconType.Outline.Ellipsis">
<Overlay>
<Menu>
@if (SelectedGameActions.Count() > 0)
{
@foreach (var action in SelectedGameActions)
{
<MenuItem OnClick="() => LibraryService.Run(SelectedGame, action)">
@action.Name
</MenuItem>
}
<MenuDivider />
}
<MenuItem OnClick="() => Uninstall(SelectedGame)">
Uninstall
</MenuItem>
</Menu>
</Overlay>
<ChildContent>
More
</ChildContent>
</DropdownButton>
}
else if (SelectedGame.State == LibraryItemState.Queued)
{
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Bars" Disabled="true">Queued</Button>
}
else if (SelectedGame.State == LibraryItemState.UpdateAvailable)
{
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Download">Update</Button>
}
else if (SelectedGame.State == LibraryItemState.Installing)
{
<Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Loading">Installing</Button>
}
</div>
}
</Pane>
</SplitPane>
@code {
LibraryItem SelectedGame { get; set; }
IEnumerable<SDK.Models.Action> SelectedGameActions { get; set; }
IEnumerable<LibraryItem> LibraryItems { get; set; } = new List<LibraryItem>();
protected override async Task OnInitializedAsync()
{
LibraryService.OnLibraryChanged += LoadData;
LoadData();
}
async void LoadData()
{
LibraryItems = await LibraryService.GetLibraryItemsAsync();
if (SelectedGame != null)
{
var selectedGame = LibraryItems.SelectMany(i => i.Children).FirstOrDefault(i => i.Key == SelectedGame.Key);
await OnLibraryItemSelected(selectedGame);
}
await InvokeAsync(StateHasChanged);
}
async Task OnLibraryItemSelected(LibraryItem item)
{
if (item.DataItem is Data.Models.Game)
{
SelectedGame = item;
if (SelectedGame.State == LibraryItemState.Installed)
SelectedGameActions = (await GameService.GetActionsAsync(SelectedGame.DataItem as Data.Models.Game)).Where(a => !a.IsPrimaryAction);
}
}
async void Import()
{
await ImportService.ImportAsync();
}
async Task Install(LibraryItem item)
{
await LibraryService.Install(item);
}
async Task Uninstall(LibraryItem item)
{
var game = item.DataItem as Data.Models.Game;
var manifest = ManifestHelper.Read(game.InstallDirectory, game.Id);
var message = $"Would you like to uninstall {game.Title}?";
if (manifest.SavePaths != null && manifest.SavePaths.Any())
message += " Your save files have been uploaded to the server.";
else
message += " Your save files may be unrecoverable if they are contained within the game's install directory.";
var confirmed = await ModalService.ConfirmAsync(new ConfirmOptions
{
OkText = "Uninstall",
CancelText = "Cancel",
Title = "Uninstall",
Content = message,
OkType = "danger",
Icon = @<Icon Type="@IconType.Outline.Delete" />
});
if (confirmed)
await LibraryService.Uninstall(item);
}
async Task Run(LibraryItem item)
{
var game = item.DataItem as Data.Models.Game;
var actions = await GameService.GetActionsAsync(game);
var primaryActions = actions.Where(a => a.IsPrimaryAction);
if (primaryActions.Count() == 1)
LibraryService.Run(item, primaryActions.First());
else
{
var modalOptions = new ModalOptions()
{
Title = item.Name,
Maximizable = false,
DefaultMaximized = false,
Closable = true,
};
var model = new ActionSelectorDialogOptions
{
Actions = actions.Where(a => a.IsPrimaryAction),
LibraryItem = item
};
var modalRef = ModalService.CreateModal<ActionSelectorDialog, ActionSelectorDialogOptions, Process>(modalOptions, model);
}
}
}