Fix Original/Default input detection in DeliveryAdminWindow
This commit is contained in:
@ -43,6 +43,7 @@ namespace Elwig.Windows {
|
||||
private RadioButton[] RadioButtonInputs;
|
||||
private readonly Dictionary<Control, bool> Valid;
|
||||
private readonly Dictionary<Control, object?> OriginalValues;
|
||||
private readonly Dictionary<Control, object?> DefaultValues;
|
||||
|
||||
public AdministrationWindow() : base() {
|
||||
IsEditing = false;
|
||||
@ -58,6 +59,7 @@ namespace Elwig.Windows {
|
||||
RadioButtonInputs = Array.Empty<RadioButton>();
|
||||
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<object>().ToArray().SequenceEqual(((object[]?)DefaultValues[ccb]) ?? Array.Empty<object>());
|
||||
} 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user