[#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 ClientParameters Client { get; set; }
|
||||||
|
|
||||||
public static Dispatcher MainDispatcher { get; private 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() {
|
public App() : base() {
|
||||||
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
||||||
@ -69,6 +73,12 @@ namespace Elwig {
|
|||||||
Scales = [];
|
Scales = [];
|
||||||
CurrentApp = this;
|
CurrentApp = this;
|
||||||
OverrideCulture();
|
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() {
|
private static void OverrideCulture() {
|
||||||
|
@ -171,7 +171,6 @@ namespace Elwig.Helpers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await cmd.ExecuteNonQueryAsync();
|
await cmd.ExecuteNonQueryAsync();
|
||||||
await App.HintContextChange();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,7 @@ namespace Elwig.Windows {
|
|||||||
|
|
||||||
abstract protected void UpdateButtons();
|
abstract protected void UpdateButtons();
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
for (int i = 0; i < PlzInputs.Length; i++)
|
for (int i = 0; i < PlzInputs.Length; i++)
|
||||||
await UpdatePlz(PlzInputs[i], PlzOrtInputs[i]);
|
await UpdatePlz(PlzInputs[i], PlzOrtInputs[i]);
|
||||||
}
|
}
|
||||||
|
@ -189,9 +189,8 @@ namespace Elwig.Windows {
|
|||||||
ValidateRequiredInputs();
|
ValidateRequiredInputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
await base.OnRenewContext();
|
await base.OnRenewContext(ctx);
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
ControlUtils.RenewItemsSource(KgInput, await ctx.WbKgs
|
ControlUtils.RenewItemsSource(KgInput, await ctx.WbKgs
|
||||||
.Include(k => k.AtKg.WbKg!.Rds)
|
.Include(k => k.AtKg.WbKg!.Rds)
|
||||||
.Select(k => k.AtKg)
|
.Select(k => k.AtKg)
|
||||||
|
@ -131,9 +131,8 @@ namespace Elwig.Windows {
|
|||||||
LockInputs();
|
LockInputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
await base.OnRenewContext();
|
await base.OnRenewContext(ctx);
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
FillInputs(App.Client);
|
FillInputs(App.Client);
|
||||||
ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons
|
ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons
|
||||||
.OrderByDescending(s => s.Year)
|
.OrderByDescending(s => s.Year)
|
||||||
|
@ -100,8 +100,7 @@ namespace Elwig.Windows {
|
|||||||
SaveButton.IsEnabled = hasChanged;
|
SaveButton.IsEnabled = hasChanged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task RefreshGraphList() {
|
private async Task RefreshGraphList(AppDbContext ctx) {
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
PaymentVar = await ctx.PaymentVariants.FindAsync(Year, AvNr) ?? throw new ArgumentException("PaymentVar not found");
|
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");
|
Season = await ctx.Seasons.FindAsync(Year) ?? throw new ArgumentException("Season not found");
|
||||||
|
|
||||||
@ -201,8 +200,8 @@ namespace Elwig.Windows {
|
|||||||
FillingInputs = false;
|
FillingInputs = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
await RefreshGraphList();
|
await RefreshGraphList(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitPlot() {
|
private void InitPlot() {
|
||||||
|
@ -2,25 +2,14 @@ using Elwig.Helpers;
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Threading;
|
|
||||||
|
|
||||||
namespace Elwig.Windows {
|
namespace Elwig.Windows {
|
||||||
public abstract class ContextWindow : Window {
|
public abstract class ContextWindow : Window {
|
||||||
|
|
||||||
public static readonly int RenewSec = 10;
|
|
||||||
|
|
||||||
protected AppDbContext Context { get; private set; }
|
|
||||||
protected bool LockContext { get; set; } = false;
|
protected bool LockContext { get; set; } = false;
|
||||||
|
|
||||||
private readonly DispatcherTimer _timer;
|
|
||||||
private bool _renewPending = false;
|
private bool _renewPending = false;
|
||||||
|
|
||||||
public ContextWindow() : base() {
|
public ContextWindow() : base() {
|
||||||
_timer = new DispatcherTimer();
|
|
||||||
_timer.Tick += new EventHandler(OnShouldRenewContext);
|
|
||||||
_timer.Interval = new TimeSpan(0, 0, RenewSec);
|
|
||||||
_timer.Start();
|
|
||||||
Context = new();
|
|
||||||
Loaded += OnLoaded;
|
Loaded += OnLoaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,22 +19,18 @@ namespace Elwig.Windows {
|
|||||||
await RenewContext();
|
await RenewContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void OnShouldRenewContext(object? sender, EventArgs? evt) {
|
|
||||||
if (!Context.HasBackendChanged) return;
|
|
||||||
await HintContextChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async void OnLoaded(object? sender, RoutedEventArgs? evt) {
|
protected async void OnLoaded(object? sender, RoutedEventArgs? evt) {
|
||||||
await OnRenewContext();
|
using var ctx = new AppDbContext();
|
||||||
|
await OnRenewContext(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task RenewContext() {
|
protected async Task RenewContext() {
|
||||||
if (!_renewPending) return;
|
if (!_renewPending) return;
|
||||||
Context = new();
|
using var ctx = new AppDbContext();
|
||||||
await OnRenewContext();
|
await OnRenewContext(ctx);
|
||||||
_renewPending = false;
|
_renewPending = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract protected Task OnRenewContext();
|
abstract protected Task OnRenewContext(AppDbContext ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -795,9 +795,8 @@ namespace Elwig.Windows {
|
|||||||
StatusVarieties.ToolTip = StatusVarieties.Text;
|
StatusVarieties.ToolTip = StatusVarieties.Text;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
await base.OnRenewContext();
|
await base.OnRenewContext(ctx);
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
|
|
||||||
if (Member != null) {
|
if (Member != null) {
|
||||||
if (await GetMemberAsync(Member.MgNr) is not Member m) {
|
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";
|
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() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
var season = await ctx.Seasons.FindAsync(Year);
|
var season = await ctx.Seasons.FindAsync(Year);
|
||||||
var l = new List<string> {
|
var l = new List<string> {
|
||||||
MemberDataSheet.Name
|
MemberDataSheet.Name
|
||||||
|
@ -264,9 +264,8 @@ namespace Elwig.Windows {
|
|||||||
ValidateRequiredInputs();
|
ValidateRequiredInputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
await base.OnRenewContext();
|
await base.OnRenewContext(ctx);
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
ControlUtils.RenewItemsSource(BranchInput, await ctx.Branches.OrderBy(b => b.Name).ToListAsync());
|
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());
|
ControlUtils.RenewItemsSource(DefaultKgInput, await ctx.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync());
|
||||||
await RefreshMemberList();
|
await RefreshMemberList();
|
||||||
|
@ -20,31 +20,29 @@ namespace Elwig.Windows {
|
|||||||
DeactivateKgButton.IsEnabled = false;
|
DeactivateKgButton.IsEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
using (var ctx = new AppDbContext()) {
|
var origins = (await ctx.WineOrigins
|
||||||
var origins = (await ctx.WineOrigins
|
.Include("Gems.AtGem.Kgs.WbKg.Gl")
|
||||||
.Include("Gems.AtGem.Kgs.WbKg.Gl")
|
|
||||||
.AsSplitQuery()
|
|
||||||
.ToListAsync())
|
|
||||||
.OrderByDescending(o => o.SortKey)
|
|
||||||
.ThenBy(o => o.HkId);
|
|
||||||
ControlUtils.RenewItemsSource(WineOrigins, origins, WineOrigins_SelectionChanged);
|
|
||||||
if (WineOrigins.SelectedItem == null) {
|
|
||||||
var hkid = await ctx.WbKgs
|
|
||||||
.GroupBy(k => k.AtKg.Gem.WbGem!.HkId)
|
|
||||||
.Where(g => g.Count() > 0)
|
|
||||||
.OrderByDescending(g => g.Count())
|
|
||||||
.Select(g => g.Key)
|
|
||||||
.FirstOrDefaultAsync();
|
|
||||||
ControlUtils.SelectItemWithPk(WineOrigins, hkid);
|
|
||||||
}
|
|
||||||
var gls = await ctx.WbGls
|
|
||||||
.OrderBy(g => g.GlNr)
|
|
||||||
.Include("Kgs.Rds")
|
|
||||||
.AsSplitQuery()
|
.AsSplitQuery()
|
||||||
.ToListAsync();
|
.ToListAsync())
|
||||||
ControlUtils.RenewItemsSource(WbGls, gls, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First);
|
.OrderByDescending(o => o.SortKey)
|
||||||
|
.ThenBy(o => o.HkId);
|
||||||
|
ControlUtils.RenewItemsSource(WineOrigins, origins, WineOrigins_SelectionChanged);
|
||||||
|
if (WineOrigins.SelectedItem == null) {
|
||||||
|
var hkid = await ctx.WbKgs
|
||||||
|
.GroupBy(k => k.AtKg.Gem.WbGem!.HkId)
|
||||||
|
.Where(g => g.Count() > 0)
|
||||||
|
.OrderByDescending(g => g.Count())
|
||||||
|
.Select(g => g.Key)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
ControlUtils.SelectItemWithPk(WineOrigins, hkid);
|
||||||
}
|
}
|
||||||
|
var gls = await ctx.WbGls
|
||||||
|
.OrderBy(g => g.GlNr)
|
||||||
|
.Include("Kgs.Rds")
|
||||||
|
.AsSplitQuery()
|
||||||
|
.ToListAsync();
|
||||||
|
ControlUtils.RenewItemsSource(WbGls, gls, WbGls_SelectionChanged, ControlUtils.RenewSourceDefault.First);
|
||||||
UpdateWbGems();
|
UpdateWbGems();
|
||||||
UpdateWbKgs();
|
UpdateWbKgs();
|
||||||
UpdateWbGlKgs();
|
UpdateWbGlKgs();
|
||||||
|
@ -36,8 +36,7 @@ namespace Elwig.Windows {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task OnRenewContext() {
|
protected override async Task OnRenewContext(AppDbContext ctx) {
|
||||||
using var ctx = new AppDbContext();
|
|
||||||
ControlUtils.RenewItemsSource(PaymentVariantList, await ctx.PaymentVariants
|
ControlUtils.RenewItemsSource(PaymentVariantList, await ctx.PaymentVariants
|
||||||
.Where(v => v.Year == Year)
|
.Where(v => v.Year == Year)
|
||||||
.OrderBy(v => v.AvNr)
|
.OrderBy(v => v.AvNr)
|
||||||
|
@ -19,7 +19,7 @@ namespace Elwig.Windows {
|
|||||||
SeasonInput.Value = Utils.CurrentLastSeason;
|
SeasonInput.Value = Utils.CurrentLastSeason;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Task OnRenewContext() {
|
protected override Task OnRenewContext(AppDbContext ctx) {
|
||||||
SeasonInput_ValueChanged(null, null);
|
SeasonInput_ValueChanged(null, null);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user