[#43] App: Use FileSystemWatcher to renew contexts on demand
This commit is contained in:
@ -60,6 +60,10 @@ namespace Elwig {
|
||||
public static ClientParameters Client { get; set; }
|
||||
|
||||
public static Dispatcher MainDispatcher { get; private set; }
|
||||
private readonly FileSystemWatcher Watcher = new(Path.GetDirectoryName(Config.DatabaseFile) ?? "C:\\") {
|
||||
NotifyFilter = NotifyFilters.LastWrite,
|
||||
Filter = "*.sqlite3",
|
||||
};
|
||||
|
||||
public App() : base() {
|
||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||
@ -69,6 +73,12 @@ namespace Elwig {
|
||||
Scales = [];
|
||||
CurrentApp = this;
|
||||
OverrideCulture();
|
||||
Watcher.Changed += new FileSystemEventHandler(OnChanged);
|
||||
Watcher.EnableRaisingEvents = true;
|
||||
}
|
||||
|
||||
private void OnChanged(object source, FileSystemEventArgs e) {
|
||||
MainDispatcher.BeginInvoke(async () => await HintContextChange());
|
||||
}
|
||||
|
||||
private static void OverrideCulture() {
|
||||
|
@ -171,7 +171,6 @@ namespace Elwig.Helpers {
|
||||
}
|
||||
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
await App.HintContextChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ namespace Elwig.Windows {
|
||||
|
||||
abstract protected void UpdateButtons();
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
for (int i = 0; i < PlzInputs.Length; i++)
|
||||
await UpdatePlz(PlzInputs[i], PlzOrtInputs[i]);
|
||||
}
|
||||
|
@ -189,9 +189,8 @@ namespace Elwig.Windows {
|
||||
ValidateRequiredInputs();
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
await base.OnRenewContext();
|
||||
using var ctx = new AppDbContext();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
await base.OnRenewContext(ctx);
|
||||
ControlUtils.RenewItemsSource(KgInput, await ctx.WbKgs
|
||||
.Include(k => k.AtKg.WbKg!.Rds)
|
||||
.Select(k => k.AtKg)
|
||||
|
@ -131,9 +131,8 @@ namespace Elwig.Windows {
|
||||
LockInputs();
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
await base.OnRenewContext();
|
||||
using var ctx = new AppDbContext();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
await base.OnRenewContext(ctx);
|
||||
FillInputs(App.Client);
|
||||
ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons
|
||||
.OrderByDescending(s => s.Year)
|
||||
|
@ -100,8 +100,7 @@ namespace Elwig.Windows {
|
||||
SaveButton.IsEnabled = hasChanged;
|
||||
}
|
||||
|
||||
private async Task RefreshGraphList() {
|
||||
using var ctx = new AppDbContext();
|
||||
private async Task RefreshGraphList(AppDbContext ctx) {
|
||||
PaymentVar = await ctx.PaymentVariants.FindAsync(Year, AvNr) ?? throw new ArgumentException("PaymentVar not found");
|
||||
Season = await ctx.Seasons.FindAsync(Year) ?? throw new ArgumentException("Season not found");
|
||||
|
||||
@ -201,8 +200,8 @@ namespace Elwig.Windows {
|
||||
FillingInputs = false;
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
await RefreshGraphList();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
await RefreshGraphList(ctx);
|
||||
}
|
||||
|
||||
private void InitPlot() {
|
||||
|
@ -2,25 +2,14 @@ using Elwig.Helpers;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Elwig.Windows {
|
||||
public abstract class ContextWindow : Window {
|
||||
|
||||
public static readonly int RenewSec = 10;
|
||||
|
||||
protected AppDbContext Context { get; private set; }
|
||||
protected bool LockContext { get; set; } = false;
|
||||
|
||||
private readonly DispatcherTimer _timer;
|
||||
private bool _renewPending = false;
|
||||
|
||||
public ContextWindow() : base() {
|
||||
_timer = new DispatcherTimer();
|
||||
_timer.Tick += new EventHandler(OnShouldRenewContext);
|
||||
_timer.Interval = new TimeSpan(0, 0, RenewSec);
|
||||
_timer.Start();
|
||||
Context = new();
|
||||
Loaded += OnLoaded;
|
||||
}
|
||||
|
||||
@ -30,22 +19,18 @@ namespace Elwig.Windows {
|
||||
await RenewContext();
|
||||
}
|
||||
|
||||
private async void OnShouldRenewContext(object? sender, EventArgs? evt) {
|
||||
if (!Context.HasBackendChanged) return;
|
||||
await HintContextChange();
|
||||
}
|
||||
|
||||
protected async void OnLoaded(object? sender, RoutedEventArgs? evt) {
|
||||
await OnRenewContext();
|
||||
using var ctx = new AppDbContext();
|
||||
await OnRenewContext(ctx);
|
||||
}
|
||||
|
||||
protected async Task RenewContext() {
|
||||
if (!_renewPending) return;
|
||||
Context = new();
|
||||
await OnRenewContext();
|
||||
using var ctx = new AppDbContext();
|
||||
await OnRenewContext(ctx);
|
||||
_renewPending = false;
|
||||
}
|
||||
|
||||
abstract protected Task OnRenewContext();
|
||||
abstract protected Task OnRenewContext(AppDbContext ctx);
|
||||
}
|
||||
}
|
||||
|
@ -795,9 +795,8 @@ namespace Elwig.Windows {
|
||||
StatusVarieties.ToolTip = StatusVarieties.Text;
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
await base.OnRenewContext();
|
||||
using var ctx = new AppDbContext();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
await base.OnRenewContext(ctx);
|
||||
|
||||
if (Member != null) {
|
||||
if (await GetMemberAsync(Member.MgNr) is not Member m) {
|
||||
|
@ -127,8 +127,7 @@ namespace Elwig.Windows {
|
||||
EmailBodyInput.Text = App.Client.TextEmailBody ?? "Sehr geehrtes Mitglied,\n\nim Anhang finden Sie das aktuelle Rundschreiben.\n\nIhre Winzergenossenschaft\n";
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
using var ctx = new AppDbContext();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
var season = await ctx.Seasons.FindAsync(Year);
|
||||
var l = new List<string> {
|
||||
MemberDataSheet.Name
|
||||
|
@ -264,9 +264,8 @@ namespace Elwig.Windows {
|
||||
ValidateRequiredInputs();
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
await base.OnRenewContext();
|
||||
using var ctx = new AppDbContext();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
await base.OnRenewContext(ctx);
|
||||
ControlUtils.RenewItemsSource(BranchInput, await ctx.Branches.OrderBy(b => b.Name).ToListAsync());
|
||||
ControlUtils.RenewItemsSource(DefaultKgInput, await ctx.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync());
|
||||
await RefreshMemberList();
|
||||
|
@ -20,8 +20,7 @@ namespace Elwig.Windows {
|
||||
DeactivateKgButton.IsEnabled = false;
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
using (var ctx = new AppDbContext()) {
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
var origins = (await ctx.WineOrigins
|
||||
.Include("Gems.AtGem.Kgs.WbKg.Gl")
|
||||
.AsSplitQuery()
|
||||
@ -44,7 +43,6 @@ namespace Elwig.Windows {
|
||||
.AsSplitQuery()
|
||||
.ToListAsync();
|
||||
ControlUtils.RenewItemsSource(WbGls, gls, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First);
|
||||
}
|
||||
UpdateWbGems();
|
||||
UpdateWbKgs();
|
||||
UpdateWbGlKgs();
|
||||
|
@ -36,8 +36,7 @@ namespace Elwig.Windows {
|
||||
}
|
||||
}
|
||||
|
||||
protected override async Task OnRenewContext() {
|
||||
using var ctx = new AppDbContext();
|
||||
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||
ControlUtils.RenewItemsSource(PaymentVariantList, await ctx.PaymentVariants
|
||||
.Where(v => v.Year == Year)
|
||||
.OrderBy(v => v.AvNr)
|
||||
|
@ -19,7 +19,7 @@ namespace Elwig.Windows {
|
||||
SeasonInput.Value = Utils.CurrentLastSeason;
|
||||
}
|
||||
|
||||
protected override Task OnRenewContext() {
|
||||
protected override Task OnRenewContext(AppDbContext ctx) {
|
||||
SeasonInput_ValueChanged(null, null);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
Reference in New Issue
Block a user