From 28bb8f2d3fc13f32a53542c9d01451f547792a81 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 24 Aug 2023 15:45:48 +0200 Subject: [PATCH] Fix Original/Default input detection in DeliveryAdminWindow --- Elwig/Helpers/ControlUtils.cs | 4 ++ Elwig/Windows/AdministrationWindow.cs | 79 +++++++++++++++++++++++ Elwig/Windows/AreaComAdminWindow.xaml.cs | 5 +- Elwig/Windows/DeliveryAdminWindow.xaml | 18 ++++-- Elwig/Windows/DeliveryAdminWindow.xaml.cs | 29 ++++++--- Elwig/Windows/MemberAdminWindow.xaml.cs | 8 +-- 6 files changed, 119 insertions(+), 24 deletions(-) diff --git a/Elwig/Helpers/ControlUtils.cs b/Elwig/Helpers/ControlUtils.cs index e6b295b..ee66310 100644 --- a/Elwig/Helpers/ControlUtils.cs +++ b/Elwig/Helpers/ControlUtils.cs @@ -25,6 +25,10 @@ namespace Elwig.Helpers { } } + public static void SetInputNotDefault(Control input) { + SetControlBrush(input, Brushes.Gold); + } + public static void SetInputChanged(Control input) { SetControlBrush(input, Brushes.Orange); } diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs index 44a7f21..3449965 100644 --- a/Elwig/Windows/AdministrationWindow.cs +++ b/Elwig/Windows/AdministrationWindow.cs @@ -43,6 +43,7 @@ namespace Elwig.Windows { private RadioButton[] RadioButtonInputs; private readonly Dictionary Valid; private readonly Dictionary OriginalValues; + private readonly Dictionary DefaultValues; public AdministrationWindow() : base() { IsEditing = false; @@ -58,6 +59,7 @@ namespace Elwig.Windows { RadioButtonInputs = Array.Empty(); Valid = new(); OriginalValues = new(); + DefaultValues = new(); Closing += OnClosing; Loaded += OnLoaded; } @@ -101,6 +103,12 @@ namespace Elwig.Windows { return (sender is Control c) && RequiredInputs.Contains(c); } + protected void FinishInputFilling() { + FillOriginalValues(); + ValidateDefaultValues(); + ValidateRequiredInputs(); + } + protected void ClearInputStates() { foreach (var tb in TextBoxInputs) ControlUtils.ClearInputState(tb); @@ -121,10 +129,25 @@ namespace Elwig.Windows { Valid[input] = false; } else if (input is ComboBox cb && cb.SelectedItem == null && cb.ItemsSource != null) { ControlUtils.SetInputInvalid(input); + } else if (input is CheckComboBox ccb && ccb.SelectedItem == null && ccb.ItemsSource != null) { + ControlUtils.SetInputInvalid(input); + } else if (input is CheckBox ckb && ckb.IsChecked != true) { + ControlUtils.SetInputInvalid(input); + Valid[input] = false; + } else if (input is RadioButton rb && rb.IsChecked != true) { + ControlUtils.SetInputInvalid(input); + Valid[input] = false; } } } + protected void ValidateDefaultValues() { + foreach (var input in DefaultValues.Keys) { + if (InputIsNotDefault(input)) + ControlUtils.SetInputNotDefault(input); + } + } + protected void LockInputs() { foreach (var tb in TextBoxInputs) { tb.IsReadOnly = true; @@ -159,6 +182,10 @@ namespace Elwig.Windows { OriginalValues.Clear(); } + protected void ClearDefaultValues() { + DefaultValues.Clear(); + } + protected void FillOriginalValues() { foreach (var tb in TextBoxInputs) OriginalValues[tb] = tb.Text; @@ -174,10 +201,34 @@ namespace Elwig.Windows { protected void SetOriginalValue(Control input, object? value) { OriginalValues[input] = value; + if (InputHasChanged(input)) { + ControlUtils.SetInputChanged(input); + } else { + ControlUtils.ClearInputState(input); + } } protected void UnsetOriginalValue(Control input) { OriginalValues.Remove(input); + ControlUtils.ClearInputState(input); + } + + protected void SetDefaultValue(Control input, object? value) { + DefaultValues[input] = value; + if (!InputHasChanged(input)) { + if (InputIsNotDefault(input)) { + ControlUtils.SetInputNotDefault(input); + } else { + ControlUtils.ClearInputState(input); + } + } + } + + protected void UnsetDefaultValue(Control input) { + DefaultValues.Remove(input); + if (!InputHasChanged(input)) { + ControlUtils.ClearInputState(input); + } } protected void ClearInputs(bool validate = true) { @@ -218,6 +269,24 @@ namespace Elwig.Windows { } } + protected bool InputIsNotDefault(Control input) { + if (!DefaultValues.ContainsKey(input)) { + return false; + } else if (input is TextBox tb) { + return DefaultValues[tb]?.ToString() != tb.Text; + } else if (input is ComboBox sb) { + return DefaultValues[sb] != sb.SelectedItem; + } else if (input is CheckComboBox ccb) { + return !ccb.SelectedItems.Cast().ToArray().SequenceEqual(((object[]?)DefaultValues[ccb]) ?? Array.Empty()); + } else if (input is CheckBox cb) { + return (DefaultValues[cb] != null) != (cb.IsChecked ?? false); + } else if (input is RadioButton rb) { + return (DefaultValues[rb] != null) != (rb.IsChecked ?? false); + } else { + return false; + } + } + protected bool HasChanged => !IsValid || TextBoxInputs.Any(InputHasChanged) || @@ -269,6 +338,8 @@ namespace Elwig.Windows { if (res.IsValid) { if (InputHasChanged(input)) { ControlUtils.SetInputChanged(input); + } else if (InputIsNotDefault(input)) { + ControlUtils.SetInputNotDefault(input); } else { ControlUtils.ClearInputState(input); } @@ -299,6 +370,8 @@ namespace Elwig.Windows { ControlUtils.SetInputInvalid(input); } else if (InputHasChanged(input)) { ControlUtils.SetInputChanged(input); + } else if (InputIsNotDefault(input)) { + ControlUtils.SetInputNotDefault(input); } else { ControlUtils.ClearInputState(input); } @@ -311,6 +384,8 @@ namespace Elwig.Windows { ControlUtils.SetInputInvalid(input); } else if (InputHasChanged(input)) { ControlUtils.SetInputChanged(input); + } else if (InputIsNotDefault(input)) { + ControlUtils.SetInputNotDefault(input); } else { ControlUtils.ClearInputState(input); } @@ -326,6 +401,8 @@ namespace Elwig.Windows { ValidateInput(input, true); if (InputHasChanged(input)) { ControlUtils.SetInputChanged(input); + } else if (InputIsNotDefault(input)) { + ControlUtils.SetInputNotDefault(input); } else { ControlUtils.ClearInputState(input); } @@ -344,6 +421,8 @@ namespace Elwig.Windows { ValidateInput(input, true); if (InputHasChanged(input)) { ControlUtils.SetInputChanged(input); + } else if (InputIsNotDefault(input)) { + ControlUtils.SetInputNotDefault(input); } else { ControlUtils.ClearInputState(input); } diff --git a/Elwig/Windows/AreaComAdminWindow.xaml.cs b/Elwig/Windows/AreaComAdminWindow.xaml.cs index f0329b8..bf9d368 100644 --- a/Elwig/Windows/AreaComAdminWindow.xaml.cs +++ b/Elwig/Windows/AreaComAdminWindow.xaml.cs @@ -80,14 +80,13 @@ namespace Elwig.Windows { AreaComTypeInput.SelectedItem = a.AreaComType; WineCultivationInput.SelectedItem = a.WineCult; - FillOriginalValues(); + FinishInputFilling(); } private async void InitInputs() { FbNrInput.Text = (await Context.NextFbNr()).ToString(); MgNrInput.Text = Member.MgNr.ToString(); - FillOriginalValues(); - ValidateRequiredInputs(); + FinishInputFilling(); } protected override async Task RenewContext() { diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml b/Elwig/Windows/DeliveryAdminWindow.xaml index d84cb44..a3d43b7 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml +++ b/Elwig/Windows/DeliveryAdminWindow.xaml @@ -192,7 +192,8 @@ DisplayMemberPath="Name" TextSearch.TextPath="Name"/>