AdministrationWindow: Use GetHashCode() to compare default/original values
This commit is contained in:
@ -217,19 +217,19 @@ namespace Elwig.Helpers {
|
|||||||
SelectCheckComboBoxItems(ccb, getId, items?.Select(i => getId(i)));
|
SelectCheckComboBoxItems(ccb, getId, items?.Select(i => getId(i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static object? GetInputValue(Control input) {
|
public static int GetInputHashCode(Control input) {
|
||||||
if (input is TextBox tb) {
|
if (input is TextBox tb) {
|
||||||
return tb.Text;
|
return Utils.GetEntityIdentifier(tb.Text);
|
||||||
} else if (input is ComboBox sb) {
|
} else if (input is ComboBox sb) {
|
||||||
return sb.SelectedItem;
|
return Utils.GetEntityIdentifier(sb.SelectedItem);
|
||||||
} else if (input is Xceed.Wpf.Toolkit.CheckComboBox ccb) {
|
} else if (input is Xceed.Wpf.Toolkit.CheckComboBox ccb) {
|
||||||
return ccb.SelectedItems.Cast<object>().ToArray();
|
return Utils.GetEntityIdentifier(ccb.SelectedItems);
|
||||||
} else if (input is CheckBox cb) {
|
} else if (input is CheckBox cb) {
|
||||||
return cb.IsChecked?.ToString();
|
return Utils.GetEntityIdentifier(cb.IsChecked);
|
||||||
} else if (input is RadioButton rb) {
|
} else if (input is RadioButton rb) {
|
||||||
return rb.IsChecked?.ToString();
|
return Utils.GetEntityIdentifier(rb.IsChecked);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,9 @@ using System.Text.Json.Nodes;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using MailKit.Net.Smtp;
|
using MailKit.Net.Smtp;
|
||||||
using MailKit.Security;
|
using MailKit.Security;
|
||||||
using OpenTK.Compute.OpenCL;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace Elwig.Helpers {
|
namespace Elwig.Helpers {
|
||||||
public static partial class Utils {
|
public static partial class Utils {
|
||||||
@ -432,5 +434,19 @@ namespace Elwig.Helpers {
|
|||||||
await client.AuthenticateAsync(username, password);
|
await client.AuthenticateAsync(username, password);
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int GetEntityIdentifier(object? obj) {
|
||||||
|
if (obj == null) {
|
||||||
|
return 0;
|
||||||
|
} else if (obj is IEnumerable list) {
|
||||||
|
var arr = list.Cast<object>().Select(o => GetEntityIdentifier(o)).ToArray();
|
||||||
|
return ((IStructuralEquatable)arr).GetHashCode(EqualityComparer<int>.Default);
|
||||||
|
} else if (obj.GetType().GetCustomAttribute(typeof(PrimaryKeyAttribute), false) is not PrimaryKeyAttribute pkAttr) {
|
||||||
|
return obj.GetHashCode();
|
||||||
|
} else {
|
||||||
|
var pk = pkAttr.PropertyNames.Select(name => obj.GetType().GetProperty(name)!.GetValue(obj)?.GetHashCode() ?? 0).ToArray();
|
||||||
|
return ((IStructuralEquatable)pk).GetHashCode(EqualityComparer<int>.Default);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,8 @@ namespace Elwig.Windows {
|
|||||||
private CheckBox[] CheckBoxInputs;
|
private CheckBox[] CheckBoxInputs;
|
||||||
private RadioButton[] RadioButtonInputs;
|
private RadioButton[] RadioButtonInputs;
|
||||||
private readonly Dictionary<Control, bool> Valid;
|
private readonly Dictionary<Control, bool> Valid;
|
||||||
private readonly Dictionary<Control, object?> OriginalValues;
|
private readonly Dictionary<Control, int?> OriginalValues;
|
||||||
private readonly Dictionary<Control, object?> DefaultValues;
|
private readonly Dictionary<Control, int?> DefaultValues;
|
||||||
|
|
||||||
public AdministrationWindow() : base() {
|
public AdministrationWindow() : base() {
|
||||||
IsEditing = false;
|
IsEditing = false;
|
||||||
@ -198,20 +198,20 @@ namespace Elwig.Windows {
|
|||||||
|
|
||||||
protected void FillOriginalValues() {
|
protected void FillOriginalValues() {
|
||||||
foreach (var tb in TextBoxInputs)
|
foreach (var tb in TextBoxInputs)
|
||||||
OriginalValues[tb] = tb.Text;
|
OriginalValues[tb] = Utils.GetEntityIdentifier(tb.Text);
|
||||||
foreach (var cb in ComboBoxInputs)
|
foreach (var cb in ComboBoxInputs)
|
||||||
OriginalValues[cb] = cb.SelectedItem;
|
OriginalValues[cb] = Utils.GetEntityIdentifier(cb.SelectedItem);
|
||||||
foreach (var ccb in CheckComboBoxInputs)
|
foreach (var ccb in CheckComboBoxInputs)
|
||||||
OriginalValues[ccb] = ccb.SelectedItems.Cast<object>().ToArray();
|
OriginalValues[ccb] = Utils.GetEntityIdentifier(ccb.SelectedItems);
|
||||||
foreach (var cb in CheckBoxInputs)
|
foreach (var cb in CheckBoxInputs)
|
||||||
OriginalValues[cb] = cb.IsChecked?.ToString();
|
OriginalValues[cb] = Utils.GetEntityIdentifier(cb.IsChecked);
|
||||||
foreach (var rb in RadioButtonInputs)
|
foreach (var rb in RadioButtonInputs)
|
||||||
OriginalValues[rb] = rb.IsChecked?.ToString();
|
OriginalValues[rb] = Utils.GetEntityIdentifier(rb.IsChecked);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void SetOriginalValue(Control input, object? value) {
|
protected void SetOriginalValue(Control input, object? value) {
|
||||||
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
||||||
OriginalValues[input] = value is bool b ? b.ToString() : value;
|
OriginalValues[input] = Utils.GetEntityIdentifier(value);
|
||||||
if (InputHasChanged(input)) {
|
if (InputHasChanged(input)) {
|
||||||
ControlUtils.SetInputChanged(input);
|
ControlUtils.SetInputChanged(input);
|
||||||
} else {
|
} else {
|
||||||
@ -221,7 +221,7 @@ namespace Elwig.Windows {
|
|||||||
|
|
||||||
protected void SetOriginalValue(Control input) {
|
protected void SetOriginalValue(Control input) {
|
||||||
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
||||||
SetOriginalValue(input, ControlUtils.GetInputValue(input));
|
SetOriginalValue(input, ControlUtils.GetInputHashCode(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UnsetOriginalValue(Control input) {
|
protected void UnsetOriginalValue(Control input) {
|
||||||
@ -232,7 +232,7 @@ namespace Elwig.Windows {
|
|||||||
|
|
||||||
protected void SetDefaultValue(Control input, object? value) {
|
protected void SetDefaultValue(Control input, object? value) {
|
||||||
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
||||||
DefaultValues[input] = value is bool b ? b.ToString() : value;
|
DefaultValues[input] = value != null ? Utils.GetEntityIdentifier(value) : null;
|
||||||
if (!InputHasChanged(input)) {
|
if (!InputHasChanged(input)) {
|
||||||
if (InputIsNotDefault(input)) {
|
if (InputIsNotDefault(input)) {
|
||||||
ControlUtils.SetInputNotDefault(input);
|
ControlUtils.SetInputNotDefault(input);
|
||||||
@ -244,7 +244,7 @@ namespace Elwig.Windows {
|
|||||||
|
|
||||||
protected void SetDefaultValue(Control input) {
|
protected void SetDefaultValue(Control input) {
|
||||||
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
if (input is UnitTextBox utbx) input = utbx.TextBox;
|
||||||
SetDefaultValue(input, ControlUtils.GetInputValue(input));
|
SetDefaultValue(input, ControlUtils.GetInputHashCode(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void UnsetDefaultValue(Control input) {
|
protected void UnsetDefaultValue(Control input) {
|
||||||
@ -281,15 +281,15 @@ namespace Elwig.Windows {
|
|||||||
if (!OriginalValues.ContainsKey(input)) {
|
if (!OriginalValues.ContainsKey(input)) {
|
||||||
return false;
|
return false;
|
||||||
} else if (input is TextBox tb) {
|
} else if (input is TextBox tb) {
|
||||||
return OriginalValues[tb]?.ToString() != tb.Text;
|
return OriginalValues[tb] != Utils.GetEntityIdentifier(tb.Text);
|
||||||
} else if (input is ComboBox sb) {
|
} else if (input is ComboBox sb) {
|
||||||
return OriginalValues[sb] != sb.SelectedItem;
|
return OriginalValues[sb] != Utils.GetEntityIdentifier(sb.SelectedItem);
|
||||||
} else if (input is CheckComboBox ccb) {
|
} else if (input is CheckComboBox ccb) {
|
||||||
return !ccb.SelectedItems.Cast<object>().ToArray().SequenceEqual(((object[]?)OriginalValues[ccb]) ?? []);
|
return OriginalValues[ccb] != Utils.GetEntityIdentifier(ccb.SelectedItems);
|
||||||
} else if (input is CheckBox cb) {
|
} else if (input is CheckBox cb) {
|
||||||
return (string?)OriginalValues[cb] != cb.IsChecked?.ToString();
|
return OriginalValues[cb] != Utils.GetEntityIdentifier(cb.IsChecked);
|
||||||
} else if (input is RadioButton rb) {
|
} else if (input is RadioButton rb) {
|
||||||
return (string?)OriginalValues[rb] != rb.IsChecked?.ToString();
|
return OriginalValues[rb] != Utils.GetEntityIdentifier(rb.IsChecked);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -300,15 +300,15 @@ namespace Elwig.Windows {
|
|||||||
if (!DefaultValues.ContainsKey(input)) {
|
if (!DefaultValues.ContainsKey(input)) {
|
||||||
return false;
|
return false;
|
||||||
} else if (input is TextBox tb) {
|
} else if (input is TextBox tb) {
|
||||||
return DefaultValues[tb]?.ToString() != tb.Text;
|
return DefaultValues[tb] != Utils.GetEntityIdentifier(tb.Text);
|
||||||
} else if (input is ComboBox sb) {
|
} else if (input is ComboBox sb) {
|
||||||
return DefaultValues[sb] != sb.SelectedItem;
|
return DefaultValues[sb] != Utils.GetEntityIdentifier(sb.SelectedItem);
|
||||||
} else if (input is CheckComboBox ccb) {
|
} else if (input is CheckComboBox ccb) {
|
||||||
return !ccb.SelectedItems.Cast<object>().ToArray().SequenceEqual(((object[]?)DefaultValues[ccb]) ?? []);
|
return DefaultValues[ccb] != Utils.GetEntityIdentifier(ccb.SelectedItems);
|
||||||
} else if (input is CheckBox cb) {
|
} else if (input is CheckBox cb) {
|
||||||
return (string?)DefaultValues[cb] != cb.IsChecked?.ToString();
|
return DefaultValues[cb] != Utils.GetEntityIdentifier(cb.IsChecked);
|
||||||
} else if (input is RadioButton rb) {
|
} else if (input is RadioButton rb) {
|
||||||
return (string?)DefaultValues[rb] != rb.IsChecked?.ToString();
|
return DefaultValues[rb] != Utils.GetEntityIdentifier(rb.IsChecked);
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user