mirror of
https://github.com/claunia/romrepomgr.git
synced 2025-12-16 19:24:51 +00:00
[Blazor] Implement importing DAT folder.
This commit is contained in:
16
RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor
Normal file
16
RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor
Normal file
@@ -0,0 +1,16 @@
|
||||
@implements IDialogContentComponent
|
||||
@inject ILogger<ImportDats> Logger
|
||||
|
||||
<FluentDialog Width="800px" Height="400px" Title="Import DATs" Modal="true" TrapFocus="true">
|
||||
<FluentDialogBody>
|
||||
<p hidden="@IsBusy">DAT files will be imported from @path.</p>
|
||||
<FluentLabel Color="@StatusColor">@StatusMessage</FluentLabel>
|
||||
<FluentProgress Max="@ProgressMax" Min="@ProgressMin" Value="@ProgressValue" Visible="@ProgressVisible"/>
|
||||
</FluentDialogBody>
|
||||
<FluentDialogFooter>
|
||||
<FluentStack Orientation="Orientation.Horizontal">
|
||||
<FluentButton OnClick="@StartAsync" Disabled="@IsBusy">Start</FluentButton>
|
||||
<FluentButton OnClick="@CloseAsync" Disabled="@CannotClose">Close</FluentButton>
|
||||
</FluentStack>
|
||||
</FluentDialogFooter>
|
||||
</FluentDialog>
|
||||
108
RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs
Normal file
108
RomRepoMgr.Blazor/Components/Dialogs/ImportDats.razor.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.FluentUI.AspNetCore.Components;
|
||||
using RomRepoMgr.Core.Workers;
|
||||
using Serilog;
|
||||
using Serilog.Extensions.Logging;
|
||||
|
||||
namespace RomRepoMgr.Blazor.Components.Dialogs;
|
||||
|
||||
public partial class ImportDats : ComponentBase
|
||||
{
|
||||
readonly Stopwatch _stopwatch = new();
|
||||
string[] _datFiles;
|
||||
int _listPosition;
|
||||
int _workers;
|
||||
string path;
|
||||
public string StatusMessage { get; set; }
|
||||
public bool IsBusy { get; set; }
|
||||
[CascadingParameter]
|
||||
public FluentDialog Dialog { get; set; }
|
||||
public int? ProgressMax { get; set; }
|
||||
public int? ProgressMin { get; set; }
|
||||
public int? ProgressValue { get; set; }
|
||||
public bool CannotClose { get; set; }
|
||||
public bool ProgressVisible { get; set; }
|
||||
public Color? StatusColor { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
base.OnInitialized();
|
||||
|
||||
path = Path.Combine(Environment.CurrentDirectory, Consts.IncomingDatFolder);
|
||||
StatusMessage = string.Empty;
|
||||
IsBusy = false;
|
||||
CannotClose = false;
|
||||
ProgressVisible = false;
|
||||
}
|
||||
|
||||
Task StartAsync()
|
||||
{
|
||||
IsBusy = true;
|
||||
CannotClose = true;
|
||||
ProgressVisible = true;
|
||||
ProgressValue = null;
|
||||
StatusMessage = "Searching for files...";
|
||||
|
||||
_stopwatch.Restart();
|
||||
string[] dats = Directory.GetFiles(path, "*.dat", SearchOption.AllDirectories);
|
||||
|
||||
string[] xmls = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
|
||||
|
||||
_datFiles = dats.Concat(xmls).Order().ToArray();
|
||||
_stopwatch.Stop();
|
||||
|
||||
Logger.LogDebug("Took {TotalSeconds} to find {Length} DAT files",
|
||||
_stopwatch.Elapsed.TotalSeconds,
|
||||
_datFiles.Length);
|
||||
|
||||
StatusMessage = string.Format("Found {0} files...", _datFiles.Length);
|
||||
|
||||
ProgressMin = 0;
|
||||
ProgressMax = _datFiles.Length;
|
||||
ProgressValue = 0;
|
||||
_listPosition = 0;
|
||||
_workers = 0;
|
||||
StateHasChanged();
|
||||
|
||||
return ImportAsync();
|
||||
}
|
||||
|
||||
async Task ImportAsync()
|
||||
{
|
||||
_stopwatch.Restart();
|
||||
Logger.LogDebug("Starting to import DAT files...");
|
||||
|
||||
Parallel.ForEach(_datFiles,
|
||||
datFile =>
|
||||
{
|
||||
_ = InvokeAsync(() =>
|
||||
{
|
||||
StatusMessage = string.Format("Importing {0}...", Path.GetFileName(datFile));
|
||||
|
||||
ProgressValue = _listPosition;
|
||||
StateHasChanged();
|
||||
});
|
||||
|
||||
var worker = new DatImporter(datFile, null, new SerilogLoggerFactory(Log.Logger));
|
||||
|
||||
worker.Import();
|
||||
|
||||
Interlocked.Increment(ref _listPosition);
|
||||
});
|
||||
|
||||
ProgressVisible = false;
|
||||
StatusMessage = "Finished";
|
||||
CannotClose = false;
|
||||
_stopwatch.Stop();
|
||||
|
||||
Logger.LogDebug("Took {TotalSeconds} seconds to import {Length} DAT files",
|
||||
_stopwatch.Elapsed.TotalSeconds,
|
||||
_datFiles.Length);
|
||||
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
Task CloseAsync() => Dialog.CloseAsync();
|
||||
}
|
||||
@@ -16,10 +16,11 @@
|
||||
<FluentSpacer/>
|
||||
<a href="https://learn.microsoft.com/en-us/aspnet/core/blazor" target="_blank">About Blazor</a>
|
||||
</FluentFooter>
|
||||
<FluentDialogProvider/>
|
||||
</FluentLayout>
|
||||
|
||||
<div data-nosnippet id="blazor-error-ui">
|
||||
An unhandled error has occurred.
|
||||
<a class="reload" href=".">Reload</a>
|
||||
<span class="dismiss">🗙</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
@page "/"
|
||||
@rendermode InteractiveServer
|
||||
@inject IDialogService DialogService
|
||||
|
||||
<PageTitle>ROM Repository Manager</PageTitle>
|
||||
|
||||
<FluentToolbar>
|
||||
<FluentButton Disabled="true">Import DATs</FluentButton>
|
||||
<FluentButton OnClick="@ImportDatsAsync">Import DATs</FluentButton>
|
||||
<FluentButton Disabled="true">Export DAT</FluentButton>
|
||||
<FluentButton Disabled="true">Remove DAT</FluentButton>
|
||||
<FluentButton Disabled="true">Import ROMs</FluentButton>
|
||||
|
||||
13
RomRepoMgr.Blazor/Components/Pages/Home.razor.cs
Normal file
13
RomRepoMgr.Blazor/Components/Pages/Home.razor.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.FluentUI.AspNetCore.Components;
|
||||
using RomRepoMgr.Blazor.Components.Dialogs;
|
||||
|
||||
namespace RomRepoMgr.Blazor.Components.Pages;
|
||||
|
||||
public partial class Home : ComponentBase
|
||||
{
|
||||
async Task ImportDatsAsync()
|
||||
{
|
||||
IDialogReference dialog = await DialogService.ShowDialogAsync<ImportDats>(new DialogParameters());
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\RomRepoMgr.Core\RomRepoMgr.Core.csproj"/>
|
||||
<ProjectReference Include="..\RomRepoMgr.Database\RomRepoMgr.Database.csproj"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user