AdministrationWindow: Use GetHashCode() to compare default/original values

This commit is contained in:
2024-03-18 16:10:31 +01:00
parent 729d2fd76c
commit 51e345f1fd
3 changed files with 45 additions and 29 deletions

View File

@ -217,19 +217,19 @@ namespace Elwig.Helpers {
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) {
return tb.Text;
return Utils.GetEntityIdentifier(tb.Text);
} else if (input is ComboBox sb) {
return sb.SelectedItem;
return Utils.GetEntityIdentifier(sb.SelectedItem);
} 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) {
return cb.IsChecked?.ToString();
return Utils.GetEntityIdentifier(cb.IsChecked);
} else if (input is RadioButton rb) {
return rb.IsChecked?.ToString();
return Utils.GetEntityIdentifier(rb.IsChecked);
} else {
return null;
return 0;
}
}
}

View File

@ -18,7 +18,9 @@ using System.Text.Json.Nodes;
using System.IO;
using MailKit.Net.Smtp;
using MailKit.Security;
using OpenTK.Compute.OpenCL;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using System.Collections;
namespace Elwig.Helpers {
public static partial class Utils {
@ -432,5 +434,19 @@ namespace Elwig.Helpers {
await client.AuthenticateAsync(username, password);
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);
}
}
}
}