ContextWindow: Change renew context event handling

This commit is contained in:
2023-09-18 21:46:14 +02:00
parent a6fef7fd9b
commit 3a73265a75
7 changed files with 44 additions and 32 deletions

View File

@ -16,20 +16,20 @@ namespace Elwig.Windows {
protected Control[] ExemptInputs { private get; set; } protected Control[] ExemptInputs { private get; set; }
protected Control[] RequiredInputs { private get; set; } protected Control[] RequiredInputs { private get; set; }
private bool _IsEditing; private bool _isEditing;
private bool _IsCreating; private bool _isCreating;
protected bool IsEditing { protected bool IsEditing {
get { return _IsEditing; } get { return _isEditing; }
set { set {
_IsEditing = value; _isEditing = value;
LockContext = IsEditing; LockContext = IsEditing || IsCreating;
} }
} }
protected bool IsCreating { protected bool IsCreating {
get { return _IsCreating; } get { return _isCreating; }
set { set {
_IsCreating = value; _isCreating = value;
LockContext = IsEditing; LockContext = IsEditing || IsCreating;
} }
} }
protected bool DoShowWarningWindows = true; protected bool DoShowWarningWindows = true;
@ -99,7 +99,7 @@ namespace Elwig.Windows {
abstract protected void UpdateButtons(); abstract protected void UpdateButtons();
protected override async Task RenewContext() { protected override async Task OnRenewContext() {
for (int i = 0; i < PlzInputs.Length; i++) for (int i = 0; i < PlzInputs.Length; i++)
UpdatePlz(PlzInputs[i], PlzOrtInputs[i]); UpdatePlz(PlzInputs[i], PlzOrtInputs[i]);
} }

View File

@ -95,8 +95,8 @@ namespace Elwig.Windows {
ValidateRequiredInputs(); ValidateRequiredInputs();
} }
protected override async Task RenewContext() { protected override async Task OnRenewContext() {
await base.RenewContext(); await base.OnRenewContext();
ControlUtils.RenewItemsSource(KgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr); ControlUtils.RenewItemsSource(KgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
ControlUtils.RenewItemsSource(AreaComTypeInput, await Context.AreaCommitmentTypes.OrderBy(v => v.VtrgId).ToListAsync(), i => (i as AreaComType)?.VtrgId); ControlUtils.RenewItemsSource(AreaComTypeInput, await Context.AreaCommitmentTypes.OrderBy(v => v.VtrgId).ToListAsync(), i => (i as AreaComType)?.VtrgId);
ControlUtils.RenewItemsSource(WineCultivationInput, await Context.WineCultivations.OrderBy(c => c.Name).ToListAsync(), i => (i as WineCult)?.CultId); ControlUtils.RenewItemsSource(WineCultivationInput, await Context.WineCultivations.OrderBy(c => c.Name).ToListAsync(), i => (i as WineCult)?.CultId);

View File

@ -52,8 +52,8 @@ namespace Elwig.Windows {
FillInputs(App.Client); FillInputs(App.Client);
} }
protected override async Task RenewContext() { protected override async Task OnRenewContext() {
await base.RenewContext(); await base.OnRenewContext();
ControlUtils.RenewItemsSource(SeasonList, await Context.Seasons.OrderByDescending(s => s.Year).ToListAsync(), s => (s as Season)?.Year, null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(SeasonList, await Context.Seasons.OrderByDescending(s => s.Year).ToListAsync(), s => (s as Season)?.Year, null, ControlUtils.RenewSourceDefault.First);
var year = (SeasonList.SelectedItem as Season)?.Year; var year = (SeasonList.SelectedItem as Season)?.Year;
ControlUtils.RenewItemsSource(SeasonModifierList, await Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync(), m => (m as Modifier)?.ModId); ControlUtils.RenewItemsSource(SeasonModifierList, await Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync(), m => (m as Modifier)?.ModId);

View File

@ -214,8 +214,8 @@ namespace Elwig.Windows {
FinishInputFilling(); FinishInputFilling();
} }
protected override async Task RenewContext() { protected override async Task OnRenewContext() {
await base.RenewContext(); await base.OnRenewContext();
await RefreshGraphList(); await RefreshGraphList();
} }

View File

@ -7,30 +7,32 @@ 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 AppDbContext Context { get; private set; }
protected bool LockContext { get; set; } = false; protected bool LockContext { get; set; } = false;
private readonly DispatcherTimer ContextRenewTimer; private readonly DispatcherTimer _timer;
private static readonly int ContextRenewSec = 10; private bool _renewPending = false;
public ContextWindow() : base() { public ContextWindow() : base() {
ContextRenewTimer = new DispatcherTimer(); _timer = new DispatcherTimer();
ContextRenewTimer.Tick += new EventHandler(OnRenewContext); _timer.Tick += new EventHandler(OnShouldRenewContext);
ContextRenewTimer.Interval = new TimeSpan(0, 0, ContextRenewSec); _timer.Interval = new TimeSpan(0, 0, RenewSec);
ContextRenewTimer.Start(); _timer.Start();
Context = new(); Context = new();
Loaded += OnLoaded; Loaded += OnLoaded;
} }
private void OnRenewContext(object? sender, EventArgs evt) { private async void OnShouldRenewContext(object? sender, EventArgs evt) {
if (LockContext || !Context.HasBackendChanged) return; if (!Context.HasBackendChanged) return;
Context.Dispose(); _renewPending = true;
Context = new(); if (LockContext) return;
RenewContext().GetAwaiter().GetResult(); await RenewContext();
} }
private void OnLoaded(object sender, RoutedEventArgs evt) { private void OnLoaded(object sender, RoutedEventArgs evt) {
RenewContext().GetAwaiter().GetResult(); OnRenewContext().GetAwaiter().GetResult();
} }
protected override void OnClosed(EventArgs evt) { protected override void OnClosed(EventArgs evt) {
@ -38,6 +40,14 @@ namespace Elwig.Windows {
Context.Dispose(); Context.Dispose();
} }
abstract protected Task RenewContext(); protected async Task RenewContext() {
if (!_renewPending) return;
Context.Dispose();
Context = new();
await OnRenewContext();
_renewPending = false;
}
abstract protected Task OnRenewContext();
} }
} }

View File

@ -415,8 +415,8 @@ namespace Elwig.Windows {
} }
} }
protected override async Task RenewContext() { protected override async Task OnRenewContext() {
await base.RenewContext(); await base.OnRenewContext();
if (Member != null) { if (Member != null) {
if (Context.Members.Find(Member.MgNr) is not Member m) { if (Context.Members.Find(Member.MgNr) is not Member m) {
@ -798,6 +798,7 @@ namespace Elwig.Windows {
} }
FinishButton.Cursor = null; FinishButton.Cursor = null;
DeliveryList.SelectedItem = null; DeliveryList.SelectedItem = null;
await RenewContext();
RefreshInputs(); RefreshInputs();
InitInputs(); InitInputs();
} }
@ -820,6 +821,7 @@ namespace Elwig.Windows {
DisableWeighingButtons(); DisableWeighingButtons();
HideFinishNewPartDeliveryCancelButtons(); HideFinishNewPartDeliveryCancelButtons();
ShowNewEditDeleteButtons(); ShowNewEditDeleteButtons();
await RenewContext();
RefreshInputs(); RefreshInputs();
ClearInputStates(); ClearInputStates();
LockInputs(); LockInputs();

View File

@ -131,8 +131,8 @@ namespace Elwig.Windows {
ValidateRequiredInputs(); ValidateRequiredInputs();
} }
protected override async Task RenewContext() { protected override async Task OnRenewContext() {
await base.RenewContext(); await base.OnRenewContext();
ControlUtils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId); ControlUtils.RenewItemsSource(BranchInput, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId);
ControlUtils.RenewItemsSource(DefaultKgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr); ControlUtils.RenewItemsSource(DefaultKgInput, await Context.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
await RefreshMemberList(); await RefreshMemberList();