ProgressBar IObservable. Basic Modal. No longer download list if exists.

.Sample() is what .Throttle() sounds like it does.
ProgressBar will be changed again, this implementation feels dirty.
This commit is contained in:
Unnoen 2022-01-27 18:48:32 +11:00
parent 699252f05e
commit d1e64e910c
No known key found for this signature in database
GPG Key ID: 8F8E42252BA20553
3 changed files with 48 additions and 37 deletions

View File

@ -1,18 +1,34 @@
@using Wabbajack.RateLimiter
@using System
@using System.Reactive.Linq
@namespace Wabbajack.App.Blazor.Components
<div id="progress-bar">
<progress value="@Percentage.Value"></progress>
<progress value="@CurrentProgress"></progress>
<span class="text">@Text</span>
</div>
@code {
[Parameter]
public Percent Percentage { get; set; }
private double CurrentProgress { get; set; }
[Parameter]
public string Text { get; set; }
[Parameter] public IObservable<Percent> ProgressObserver { get; set; }
[Parameter] public string Text { get; set; }
protected override Task OnInitializedAsync()
{
var textPercentage = string.IsNullOrEmpty(Text);
ProgressObserver
.Sample(TimeSpan.FromMilliseconds(250))
.DistinctUntilChanged()
.Subscribe(p => {
CurrentProgress = p.Value;
if (textPercentage) Text = p.ToString();
InvokeAsync(StateHasChanged);
});
return base.OnInitializedAsync();
}
}

View File

@ -1,7 +1,5 @@
@page "/gallery"
@using System.Globalization
@namespace Wabbajack.App.Blazor.Pages
<div id="content">
@ -39,7 +37,7 @@
{
<BottomBar Image="@DownloadingMetaData.Links.ImageUri" Title="Downloading..." Subtitle="@DownloadingMetaData.Title">
<div style="height:1.5rem;">
<ProgressBar Percentage="@DownloadProgress" Text="@DownloadProgress.Value.ToString(CultureInfo.InvariantCulture)"/>
<ProgressBar ProgressObserver="@DownloadProgress"/>
</div>
</BottomBar>
}

View File

@ -4,12 +4,13 @@ using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Shell;
using Blazored.Modal;
using Blazored.Modal.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Wabbajack.App.Blazor.Components;
using Wabbajack.App.Blazor.State;
using Wabbajack.Common;
using Wabbajack.DTOs;
using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated.Services;
@ -22,7 +23,8 @@ public partial class Gallery
[Inject] private NavigationManager NavigationManager { get; set; } = default!;
[Inject] private ModListDownloadMaintainer Maintainer { get; set; } = default!;
private Percent DownloadProgress { get; set; } = Percent.Zero;
[CascadingParameter] public IModalService Modal { get; set; }
private IObservable<Percent> DownloadProgress { get; set; }
private ModlistMetadata? DownloadingMetaData { get; set; }
private IEnumerable<ModlistMetadata> Modlists => StateContainer.Modlists;
@ -50,13 +52,18 @@ public partial class Gallery
private async Task OnClickDownload(ModlistMetadata metadata)
{
// GlobalState.NavigationAllowed = !GlobalState.NavigationAllowed;
await Download(metadata);
if (!await Maintainer.HaveModList(metadata)) await Download(metadata);
StateContainer.ModlistPath = Maintainer.ModListPath(metadata);
StateContainer.Modlist = null;
NavigationManager.NavigateTo(Configure.Route);
}
private async Task OnClickInformation(ModlistMetadata metadata)
{
// TODO: [High] Implement information modal.
var parameters = new ModalParameters();
parameters.Add(nameof(InfoModal.Content), metadata.Description);
Modal.Show<InfoModal>("Information", parameters);
}
private async Task Download(ModlistMetadata metadata)
@ -64,35 +71,25 @@ public partial class Gallery
StateContainer.NavigationAllowed = false;
DownloadingMetaData = metadata;
// TODO: download progress should be in ProgressBar component so it can refresh independently
try
{
var (progress, task) = Maintainer.DownloadModlist(metadata);
DownloadProgress = progress;
var dispose = progress
.DistinctUntilChanged(p => p.Value)
.Throttle(TimeSpan.FromMilliseconds(100))
.Subscribe(p =>
{
DownloadProgress = p;
.Sample(TimeSpan.FromMilliseconds(250))
.Subscribe(p => {
StateContainer.TaskBarState = new TaskBarState
{
Description = $"Downloading {metadata.Title}",
State = TaskbarItemProgressState.Indeterminate,
State = TaskbarItemProgressState.Normal,
ProgressValue = p.Value
};
InvokeAsync(StateHasChanged);
// Dispatcher.CreateDefault().InvokeAsync(StateHasChanged);
}, () => { StateContainer.TaskBarState = new TaskBarState(); });
await task;
dispose.Dispose();
var path = KnownFolders.EntryPoint.Combine("downloaded_mod_lists", metadata.Links.MachineURL).WithExtension(Ext.Wabbajack);
StateContainer.ModlistPath = path;
NavigationManager.NavigateTo(Configure.Route);
}
catch (Exception e)
{