wabbajack/Wabbajack.App.Blazor/Pages/Gallery.razor.cs

106 lines
3.4 KiB
C#
Raw Normal View History

2022-01-20 08:34:38 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2022-01-21 13:41:37 +00:00
using System.Reactive.Linq;
2022-01-20 08:34:38 +00:00
using System.Threading.Tasks;
2022-01-21 04:57:59 +00:00
using System.Windows.Shell;
using Blazored.Modal;
using Blazored.Modal.Services;
2022-01-20 08:34:38 +00:00
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Wabbajack.App.Blazor.Components;
2022-01-20 08:34:38 +00:00
using Wabbajack.App.Blazor.State;
using Wabbajack.DTOs;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated.Services;
namespace Wabbajack.App.Blazor.Pages;
public partial class Gallery
{
2022-01-21 13:41:37 +00:00
[Inject] private ILogger<Gallery> Logger { get; set; } = default!;
[Inject] private IStateContainer StateContainer { get; set; } = default!;
[Inject] private NavigationManager NavigationManager { get; set; } = default!;
[Inject] private ModListDownloadMaintainer Maintainer { get; set; } = default!;
2022-01-20 08:34:38 +00:00
2022-01-28 05:05:30 +00:00
[Inject] private IModalService Modal { get; set; } = default!;
private IObservable<Percent> DownloadProgress { get; set; }
2022-01-21 15:11:44 +00:00
private ModlistMetadata? DownloadingMetaData { get; set; }
2022-01-20 08:34:38 +00:00
2022-01-21 13:41:37 +00:00
private IEnumerable<ModlistMetadata> Modlists => StateContainer.Modlists;
private bool _errorLoadingModlists;
2022-01-21 13:41:37 +00:00
private bool _shouldRender;
protected override bool ShouldRender() => _shouldRender;
2022-01-20 08:34:38 +00:00
protected override async Task OnInitializedAsync()
{
2022-01-21 13:41:37 +00:00
if (!StateContainer.Modlists.Any())
2022-01-20 08:34:38 +00:00
{
2022-01-21 13:41:37 +00:00
var res = await StateContainer.LoadModlistMetadata();
if (!res)
{
_errorLoadingModlists = true;
_shouldRender = true;
return;
}
2022-01-20 08:34:38 +00:00
}
2022-01-21 13:41:37 +00:00
_shouldRender = true;
2022-01-20 08:34:38 +00:00
}
2022-01-21 15:11:44 +00:00
private async Task OnClickDownload(ModlistMetadata metadata)
2022-01-20 08:34:38 +00:00
{
if (!await Maintainer.HaveModList(metadata)) await Download(metadata);
StateContainer.ModlistPath = Maintainer.ModListPath(metadata);
StateContainer.Modlist = null;
NavigationManager.NavigateTo(Configure.Route);
2022-01-20 08:34:38 +00:00
}
2022-01-21 15:11:44 +00:00
private async Task OnClickInformation(ModlistMetadata metadata)
2022-01-20 08:34:38 +00:00
{
// TODO: [High] Implement information modal.
var parameters = new ModalParameters();
parameters.Add(nameof(InfoModal.Content), metadata.Description);
Modal.Show<InfoModal>("Information", parameters);
2022-01-20 08:34:38 +00:00
}
private async Task Download(ModlistMetadata metadata)
{
2022-01-21 13:41:37 +00:00
StateContainer.NavigationAllowed = false;
2022-01-21 15:11:44 +00:00
DownloadingMetaData = metadata;
2022-01-21 13:41:37 +00:00
2022-01-20 08:34:38 +00:00
try
{
2022-01-21 13:41:37 +00:00
var (progress, task) = Maintainer.DownloadModlist(metadata);
2022-01-21 15:11:44 +00:00
DownloadProgress = progress;
var dispose = progress
.Sample(TimeSpan.FromMilliseconds(250))
2022-01-28 05:05:30 +00:00
.DistinctUntilChanged(p => p.Value)
.Subscribe(p => {
StateContainer.TaskBarState = new TaskBarState
{
Description = $"Downloading {metadata.Title}",
State = TaskbarItemProgressState.Normal,
ProgressValue = p.Value
};
}, () => { StateContainer.TaskBarState = new TaskBarState(); });
2022-01-20 08:34:38 +00:00
await task;
dispose.Dispose();
}
catch (Exception e)
{
2022-01-21 13:41:37 +00:00
Logger.LogError(e, "Exception downloading Modlist {Name}", metadata.Title);
}
finally
{
StateContainer.TaskBarState = new TaskBarState();
StateContainer.NavigationAllowed = true;
2022-01-20 08:34:38 +00:00
}
}
}