Every repository, service, Blazor page/dialog, and test now uses
async/await. Single atomic diff; the codebase does not compile in
intermediate states.
- BaseRepository: LoadListAsync/LoadAsync/SaveAsync return Task<T>;
per-file locks use SemaphoreSlim so waiters can await; Save
serialises to a MemoryStream sync (XmlSerializer has no async
form), then File.WriteAllBytesAsync + sync File.Move.
- RepositoryCache.GetOrLoadAsync takes a Func<Task<List<T>>>.
- All 7 repository interfaces + implementations async.
- All service interfaces + implementations async (except vendor
IFxService and stateless IFxConverter / SettingsService).
- Every Blazor OnInitializedAsync switches to await base.
- Test suite fully async, 42 tests pass.
AccountRepository.EnsureAccountsFile keeps two .GetAwaiter().GetResult()
bridges because it runs from the constructor.
Null-render guard follow-up (folded in):
Blazor now renders the component once with fields at their initial
values while OnInitializedAsync awaits — so fields declared `= null!`
are actually null on that first render and things like
`accounts.GroupBy(...)` throw ArgumentNullException. Fixed across
Transactions, BalanceReport, BalanceSheetReport, ProfitLossReport,
DetailReport, Assets, Spendings, SpendingsOverTime, TransactionDialog,
TransactionSplitDialog, and BookingRuleDialog:
- Collection fields initialise to [] so first-render loops are empty.
- Single-object data fields become nullable; the razor wraps
consumption in `@if (field is null) { <p>Lädt…</p> return; }`.
- <PlotlyChart> guarded behind a null check on config/layout/data so
Plotly.Blazor's @bind doesn't see nulls.
- Header/footer strings initialise to "" instead of null!.
Architectural hygiene on a single-user local Blazor Server app: the
observed win is one File.ReadAllBytesAsync and one
File.WriteAllBytesAsync per Load/Save, and after PR D each file is
loaded at most once per SignalR circuit.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
187 lines
6 KiB
C#
187 lines
6 KiB
C#
using System.IO.Compression;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.FluentUI.AspNetCore.Components;
|
|
using Schaad.Accounting.Datasets;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Models;
|
|
using Schaad.Accounting.UI.Components.Pages.Dialogs;
|
|
|
|
namespace Schaad.Accounting.UI.Components.Pages;
|
|
|
|
public partial class Home : ComponentBase
|
|
{
|
|
[Inject]
|
|
private IViewService viewService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IToastService toastService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IMessageService messageService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IDialogService dialogService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IFileService fileService { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private IBookingTextRepository bookingTextRepository { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private ITransactionRepository transactionRepository { get; set; } = null!;
|
|
|
|
[Inject]
|
|
private NavigationManager Navigation { get; set; } = null!;
|
|
|
|
private List<Transaction>? matchingBankTransactions;
|
|
private IReadOnlyList<Account> accounts = [];
|
|
private IReadOnlyList<string> bookingTexts = [];
|
|
|
|
FluentInputFile? myFileByBuffer;
|
|
int? progressPercent;
|
|
string? progressTitle;
|
|
bool IsCanceled;
|
|
Dictionary<int, string> Files = new();
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
matchingBankTransactions = await viewService.MatchOpenBankTransactionsAsync();
|
|
accounts = await viewService.GetAccountViewListAsync();
|
|
bookingTexts = (await bookingTextRepository.GetBookingTextListAsync()).Select(b => b.Text).ToArray();
|
|
await base.OnInitializedAsync();
|
|
}
|
|
|
|
async Task OnProgressChangeAsync(FluentInputFileEventArgs file)
|
|
{
|
|
progressPercent = file.ProgressPercent;
|
|
progressTitle = file.ProgressTitle;
|
|
|
|
// To cancel?
|
|
file.IsCancelled = IsCanceled;
|
|
|
|
// New file
|
|
if (!Files.ContainsKey(file.Index))
|
|
{
|
|
var localFile = Path.GetTempFileName() + file.Name;
|
|
Files.Add(file.Index, localFile);
|
|
}
|
|
|
|
// Write to the FileStream
|
|
await file.Buffer.AppendToFileAsync(Files[file.Index]);
|
|
}
|
|
|
|
private async Task OnCompletedAsync(IEnumerable<FluentInputFileEventArgs> files)
|
|
{
|
|
progressPercent = myFileByBuffer!.ProgressPercent;
|
|
progressTitle = myFileByBuffer!.ProgressTitle;
|
|
|
|
foreach (var file in Files)
|
|
{
|
|
var extension = Path.GetExtension(file.Value);
|
|
if (extension is ".xml")
|
|
{
|
|
await ImportXmlAndShowResultAsync(file.Value);
|
|
}
|
|
else if (extension is ".zip")
|
|
{
|
|
var unzippedFiles = UnzipAndListFiles(file.Value);
|
|
foreach (var unzippedFile in unzippedFiles)
|
|
{
|
|
await ImportXmlAndShowResultAsync(unzippedFile);
|
|
File.Delete(unzippedFile);
|
|
}
|
|
}
|
|
|
|
File.Delete(file.Value);
|
|
}
|
|
|
|
matchingBankTransactions = await viewService.MatchOpenBankTransactionsAsync();
|
|
}
|
|
|
|
private async Task ImportXmlAndShowResultAsync(string fileName)
|
|
{
|
|
var messages = await fileService.ImportAccountStatementFileAsync(fileName);
|
|
foreach (var message in messages)
|
|
{
|
|
await ShowImportResultAsync(message);
|
|
}
|
|
}
|
|
|
|
private IReadOnlyList<string> UnzipAndListFiles(string zipFilePath)
|
|
{
|
|
var tempExtractPath = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));
|
|
Directory.CreateDirectory(tempExtractPath);
|
|
ZipFile.ExtractToDirectory(zipFilePath, tempExtractPath);
|
|
return new List<string>(Directory.GetFiles(tempExtractPath, "*", SearchOption.AllDirectories));
|
|
}
|
|
|
|
private async Task ShowImportResultAsync(MessageDataset message)
|
|
{
|
|
var joinedDetails = string.Join(", ", message.Lines);
|
|
var text = $"{message.Title}: {joinedDetails}";
|
|
|
|
if (message.Status == MessageStatus.Success)
|
|
{
|
|
ShowToast(text, ToastIntent.Success);
|
|
}
|
|
else if (message.Status == MessageStatus.Info)
|
|
{
|
|
ShowToast(text, ToastIntent.Info);
|
|
}
|
|
else if (message.Status == MessageStatus.Warning)
|
|
{
|
|
await ShowMessageAsync(text, MessageIntent.Warning);
|
|
}
|
|
else if (message.Status == MessageStatus.Error)
|
|
{
|
|
await ShowMessageAsync(text, MessageIntent.Error);
|
|
}
|
|
}
|
|
|
|
private async Task ShowMessageAsync(string message, MessageIntent intent)
|
|
{
|
|
await messageService.ShowMessageBarAsync(message, intent, "MESSAGES_TOP");
|
|
}
|
|
|
|
private void ShowToast(string message, ToastIntent intent)
|
|
{
|
|
toastService.ShowToast(intent, message, 3000);
|
|
}
|
|
|
|
private async Task BookBankTransactionAsync(Transaction transaction)
|
|
{
|
|
if (string.IsNullOrEmpty(transaction.Text) || string.IsNullOrEmpty(transaction.TargetAccountId))
|
|
{
|
|
ShowToast("Transaktion kann wegen fehlenden Daten nicht gespeichert werden!", ToastIntent.Error);
|
|
return;
|
|
}
|
|
await transactionRepository.SaveTransactionAsync(transaction);
|
|
transaction.ReadOnly = true;
|
|
}
|
|
|
|
private async Task SplitBankTransactionAsync(Transaction transaction)
|
|
{
|
|
var dialog = await dialogService.ShowDialogAsync<TransactionSplitDialog>(transaction, new DialogParameters()
|
|
{
|
|
Height = "500px",
|
|
Width = "800px",
|
|
Title = "Split Transaktion",
|
|
PreventDismissOnOverlayClick = true,
|
|
PreventScroll = true,
|
|
});
|
|
|
|
var result = await dialog.Result;
|
|
if (!result.Cancelled && result.Data != null)
|
|
{
|
|
ReloadPage();
|
|
await Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
private void ReloadPage()
|
|
{
|
|
var uri = Navigation.Uri;
|
|
Navigation.NavigateTo(uri, forceLoad: true); // forceLoad = true does a full reload
|
|
}
|
|
}
|