Controllers are dumb.

This commit is contained in:
Unnoen 2022-01-12 03:26:47 +11:00
parent 4146d0c067
commit d3ded255bf
No known key found for this signature in database
GPG Key ID: 8F8E42252BA20553
9 changed files with 58 additions and 117 deletions

@ -1,12 +1,13 @@
@namespace Wabbajack.App.Blazor.Components
@using Wabbajack.App.Blazor.Store
@using Wabbajack.RateLimiter
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
<div class="item">
<div class="display">
<img src="@Metadata.Links.ImageUri" class="image" alt="@Metadata.Title Image">
<div class="interaction">
@if (_installState.Value.CurrentInstallState != InstallState.InstallStateEnum.Configuration)
@if (_downloadState.Value.CurrentDownloadState == DownloadState.DownloadStateEnum.Downloading)
{
<img src="images/icons/install.svg" class="install hidden" alt="Install">
}
@ -17,9 +18,9 @@
<img src="images/icons/info.svg" class="more" alt="Information">
</div>
</div>
@if (_installState.Value.CurrentInstallState == InstallState.InstallStateEnum.Downloading && _installState.Value.CurrentModlistMetadata == Metadata)
@if (DownloadProgress != Percent.Zero)
{
<Progress Percentage=PercentDownloaded></Progress>
<ProgressBar Percentage=@DownloadProgress></ProgressBar>
}
<div class="info">
<div class="title">@Metadata.Title</div>

@ -1,29 +1,29 @@
using System;
using System.Diagnostics;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using Fluxor;
using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Wabbajack.App.Blazor.Store;
using Wabbajack.Common;
using Wabbajack.DTOs;
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated.Services;
namespace Wabbajack.App.Blazor.Components
{
public partial class ModlistItem
{
[Inject] private ModListDownloadMaintainer _maintainer { get; set; }
[Inject] private IState<DownloadState> _downloadState { get; set; }
[Inject] private IDispatcher _dispatcher { get; set; }
[Inject] private IState<DownloadState> _downloadState { get; set; }
[Inject] private ModListDownloadMaintainer _maintainer { get; set; }
[Inject] private IDispatcher _dispatcher { get; set; }
[Inject] private NavigationManager NavigationManager { get; set; }
[Parameter] public ModlistMetadata Metadata { get; set; }
public double PercentDownloaded { get; set; }
public Percent DownloadProgress { get; set; }
private async Task Download()
{
@ -31,25 +31,27 @@ namespace Wabbajack.App.Blazor.Components
timer.Change(TimeSpan.FromMilliseconds(250), TimeSpan.FromMilliseconds(250));
try
{
UpdateInstallState(InstallState.InstallStateEnum.Downloading, Metadata);
UpdateDownloadState(DownloadState.DownloadStateEnum.Downloading, Metadata);
(IObservable<Percent> progress, Task task) = _maintainer.DownloadModlist(Metadata);
IDisposable dispose = progress.Subscribe(p => { PercentDownloaded = p.Value * 100; });
IDisposable dispose = progress.Subscribe(p => DownloadProgress = p);
await task;
//await _wjClient.SendMetric("downloading", Metadata.Title);
Debug.Print("##### WE DOWNLOADED THE THING!");
UpdateInstallState(InstallState.InstallStateEnum.Configuration);
UpdateDownloadState(DownloadState.DownloadStateEnum.Downloaded, Metadata);
dispose.Dispose();
NavigationManager.NavigateTo($"configure/{Metadata.Links.MachineURL}");
}
catch (Exception e)
{
Debug.Print(e.Message);
UpdateDownloadState(DownloadState.DownloadStateEnum.Failure, Metadata);
}
await timer.DisposeAsync();
}
private void UpdateInstallState(InstallState.InstallStateEnum state, ModlistMetadata? metadata = null) => _dispatcher.Dispatch(new UpdateInstallState(state, metadata));
private void UpdateDownloadState(DownloadState.DownloadStateEnum state, ModlistMetadata metadata) =>
_dispatcher.Dispatch(new UpdateDownloadState(state, metadata));
}
}

@ -49,7 +49,7 @@ $hover-icon-size: 75px;
margin: 0;
transition: all 150ms ease-in-out;
.hidden {
&.hidden {
opacity: 0.25;
}

@ -1,7 +1,8 @@
<progress value="@Percentage" max="100"> </progress>
@using Wabbajack.RateLimiter
<progress value="@Percentage.Value"> </progress>
@code {
[Parameter]
public double Percentage { get; set; }
public Percent Percentage { get; set; }
}

@ -1,51 +0,0 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Fluxor;
using Wabbajack.App.Blazor.Store;
using Wabbajack.DTOs;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated.Services;
namespace Wabbajack.App.Blazor.Controllers;
public class DownloadController
{
private readonly ModListDownloadMaintainer _maintainer;
private readonly IState<DownloadState> _downloadState;
private IDispatcher _dispatcher;
public DownloadController(ModListDownloadMaintainer maintainer, IState<DownloadState> downloadState, IDispatcher dispatcher)
{
_maintainer = maintainer;
_downloadState = downloadState;
_dispatcher = dispatcher;
}
private async Task DownloadModlist(ModlistMetadata metadata)
{
// await using Timer timer = new(_ => InvokeAsync(StateHasChanged));
// timer.Change(TimeSpan.FromMilliseconds(250), TimeSpan.FromMilliseconds(250));
try
{
(IObservable<Percent> progress, Task task) = _maintainer.DownloadModlist(metadata);
IDisposable dispose = progress.Subscribe(p =>
{
UpdateDownloadState(DownloadState.DownloadStateEnum.Downloading, metadata, p);
});
await task;
//await _wjClient.SendMetric("downloading", Metadata.Title);
UpdateDownloadState(DownloadState.DownloadStateEnum.Downloaded, metadata);
dispose.Dispose();
}
catch (Exception e)
{
Debug.Print(e.Message);
}
// await timer.DisposeAsync();
}
private void UpdateDownloadState(DownloadState.DownloadStateEnum state, ModlistMetadata metadata, Percent? progress = null) => _dispatcher.Dispatch(new UpdateDownloadState(state, metadata, progress));
}

@ -1,7 +1,7 @@
@page "/configure/{path}"
@page "/configure/{machineURL}"
@layout Shared.MainLayout
@using System.Diagnostics
@using Wabbajack.Paths
@using Wabbajack.Common
@using Wabbajack.Paths.IO
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@ -32,15 +32,16 @@
@code {
[Parameter]
public string path { get; set; }
public string machineURL { get; set; }
protected override void OnInitialized()
{
var file = path.ToAbsolutePath();
if (file.FileExists())
var Location = KnownFolders.EntryPoint.Combine("downloaded_mod_lists", machineURL).WithExtension(Ext.Wabbajack);
if (Location.FileExists())
{
Debug.Print("IT EXISTS I GUESS");
}
base.OnInitialized();
}
}

@ -1,7 +1,5 @@
@page "/gallery"
@layout Shared.MainLayout
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@using Wabbajack.Networking.WabbajackClientApi;
@using Wabbajack.DTOs
@ -12,4 +10,28 @@
{
<ModlistItem Metadata=@item></ModlistItem>
}
</div>
</div>
@code {
List<ModlistMetadata> _listItems = new();
protected override async Task<Task> OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return base.OnAfterRenderAsync(firstRender);
try
{
ModlistMetadata[] modLists = await _client.LoadLists();
_listItems = modLists.ToList();
StateHasChanged();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return base.OnAfterRenderAsync(firstRender);
}
}

@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wabbajack.DTOs;
namespace Wabbajack.App.Blazor.Pages;
public partial class Gallery
{
List<ModlistMetadata> _listItems = new();
protected override async Task<Task> OnAfterRenderAsync(bool firstRender)
{
if (!firstRender) return base.OnAfterRenderAsync(firstRender);
try
{
ModlistMetadata[] modLists = await _client.LoadLists();
_listItems = modLists.ToList();
StateHasChanged();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return base.OnAfterRenderAsync(firstRender);
}
}

@ -1,6 +1,5 @@
using Fluxor;
using Wabbajack.DTOs;
using Wabbajack.RateLimiter;
namespace Wabbajack.App.Blazor.Store;
@ -9,16 +8,14 @@ public class DownloadState
{
public DownloadStateEnum CurrentDownloadState { get; }
public ModlistMetadata CurrentModlistMetadata { get; }
public Percent CurrentDownloadProgress { get; }
// Required for initial state.
private DownloadState() { }
public DownloadState(DownloadStateEnum newState, ModlistMetadata newModlist, Percent newProgress)
public DownloadState(DownloadStateEnum newState, ModlistMetadata newModlist)
{
CurrentDownloadState = newState;
CurrentModlistMetadata = newModlist;
CurrentDownloadProgress = newProgress;
}
public enum DownloadStateEnum
@ -34,13 +31,11 @@ public class UpdateDownloadState
{
public DownloadState.DownloadStateEnum State { get; }
public ModlistMetadata Modlist { get; }
public Percent DownloadProgress { get; }
public UpdateDownloadState(DownloadState.DownloadStateEnum state, ModlistMetadata modlist, Percent? currentDownloadProgress)
public UpdateDownloadState(DownloadState.DownloadStateEnum state, ModlistMetadata modlist)
{
State = state;
Modlist = modlist;
DownloadProgress = currentDownloadProgress ?? Percent.Zero;
}
}
@ -48,5 +43,5 @@ public static class DownloadStateReducers
{
[ReducerMethod]
public static DownloadState ReduceChangeDownloadState(DownloadState state, UpdateDownloadState action) =>
new(action.State, action.Modlist, action.DownloadProgress);
new(action.State, action.Modlist);
}