Outsource many methods from MemberAdminWindow to abstract classes
This commit is contained in:
@ -13,20 +13,11 @@ using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
|
||||
namespace Elwig.Windows {
|
||||
public partial class MemberAdminWindow : Window {
|
||||
public partial class MemberAdminWindow : AdministrationWindow {
|
||||
private bool IsEditing = false;
|
||||
private bool IsCreating = false;
|
||||
private List<string> TextFilter = new();
|
||||
private readonly Control[] ExemptInputs;
|
||||
private readonly Control[] RequiredInputs;
|
||||
private IEnumerable<TextBox> TextBoxInputs = Array.Empty<TextBox>();
|
||||
private IEnumerable<ComboBox> ComboBoxInputs = Array.Empty<ComboBox>();
|
||||
private IEnumerable<CheckBox> CheckBoxInputs = Array.Empty<CheckBox>();
|
||||
private IEnumerable<RadioButton> RadioButtonInputs = Array.Empty<RadioButton>();
|
||||
private readonly Dictionary<Control, bool> Valid = new();
|
||||
private readonly Dictionary<Control, object?> OriginalValues = new();
|
||||
private readonly RoutedCommand CtrlF = new();
|
||||
private readonly AppDbContext Context = new();
|
||||
|
||||
public MemberAdminWindow() {
|
||||
InitializeComponent();
|
||||
@ -48,21 +39,6 @@ namespace Elwig.Windows {
|
||||
ActiveMemberInput.IsChecked = true;
|
||||
BranchInput.ItemsSource = Context.Branches.OrderBy(b => b.Name).ToList();
|
||||
DefaultKgInput.ItemsSource = Context.WbKgs.Select(k => k.Kg).OrderBy(k => k.Name).ToList();
|
||||
TextBoxInputs = Utils.FindVisualChilds<TextBox>(this, ExemptInputs).ToList();
|
||||
ComboBoxInputs = Utils.FindVisualChilds<ComboBox>(this, ExemptInputs).ToList();
|
||||
CheckBoxInputs = Utils.FindVisualChilds<CheckBox>(this, ExemptInputs).ToList();
|
||||
RadioButtonInputs = Utils.FindVisualChilds<RadioButton>(this, ExemptInputs).ToList();
|
||||
foreach (var tb in TextBoxInputs)
|
||||
Valid[tb] = true;
|
||||
}
|
||||
|
||||
protected override void OnClosing(CancelEventArgs evt) {
|
||||
Context.Dispose();
|
||||
base.OnClosing(evt);
|
||||
}
|
||||
|
||||
private bool SenderIsRequired(object sender) {
|
||||
return (sender is Control c) && RequiredInputs.Contains(c);
|
||||
}
|
||||
|
||||
private async Task RefreshMemberList() {
|
||||
@ -100,17 +76,6 @@ namespace Elwig.Windows {
|
||||
RefreshInputs();
|
||||
}
|
||||
|
||||
private void ClearInputStates() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
Utils.ClearInputState(tb);
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
Utils.ClearInputState(cb);
|
||||
foreach (var cb in CheckBoxInputs)
|
||||
Utils.ClearInputState(cb);
|
||||
foreach (var rb in RadioButtonInputs)
|
||||
Utils.ClearInputState(rb);
|
||||
}
|
||||
|
||||
private void RefreshInputs(bool validate = false) {
|
||||
ClearInputStates();
|
||||
Member m = (Member)MemberList.SelectedItem;
|
||||
@ -137,14 +102,7 @@ namespace Elwig.Windows {
|
||||
ActiveInput.IsChecked = true;
|
||||
ContactPostInput.IsChecked = true;
|
||||
FillOriginalValues();
|
||||
foreach (var input in RequiredInputs) {
|
||||
if (input is TextBox tb && tb.Text.Length == 0) {
|
||||
Utils.SetInputInvalid(input);
|
||||
Valid[input] = false;
|
||||
} else if (input is ComboBox cb && cb.SelectedItem == null) {
|
||||
Utils.SetInputInvalid(input);
|
||||
}
|
||||
}
|
||||
ValidateRequiredInputs();
|
||||
}
|
||||
|
||||
private void MemberList_SelectionChanged(object sender, RoutedEventArgs evt) {
|
||||
@ -288,28 +246,6 @@ namespace Elwig.Windows {
|
||||
DeleteMemberButton.Visibility = Visibility.Hidden;
|
||||
}
|
||||
|
||||
private void LockInputs() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
tb.IsReadOnly = true;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.IsEnabled = false;
|
||||
foreach (var cb in CheckBoxInputs)
|
||||
cb.IsEnabled = false;
|
||||
foreach (var rb in RadioButtonInputs)
|
||||
rb.IsEnabled = false;
|
||||
}
|
||||
|
||||
private void UnlockInputs() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
tb.IsReadOnly = false;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.IsEnabled = true;
|
||||
foreach (var cb in CheckBoxInputs)
|
||||
cb.IsEnabled = true;
|
||||
foreach (var rb in RadioButtonInputs)
|
||||
rb.IsEnabled = true;
|
||||
}
|
||||
|
||||
private void LockSearchInputs() {
|
||||
SearchInput.IsEnabled = false;
|
||||
ActiveMemberInput.IsEnabled = false;
|
||||
@ -386,7 +322,7 @@ namespace Elwig.Windows {
|
||||
}
|
||||
|
||||
private void FillInputs(Member m) {
|
||||
OriginalValues.Clear();
|
||||
ClearOriginalValues();
|
||||
|
||||
MgNrInput.Text = m.MgNr.ToString();
|
||||
PredecessorMgNrInput.Text = m.PredecessorMgNr.ToString();
|
||||
@ -459,267 +395,122 @@ namespace Elwig.Windows {
|
||||
FillOriginalValues();
|
||||
}
|
||||
|
||||
private void FillOriginalValues() {
|
||||
foreach (var tb in TextBoxInputs)
|
||||
OriginalValues[tb] = tb.Text;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
OriginalValues[cb] = cb.SelectedItem;
|
||||
foreach (var cb in CheckBoxInputs)
|
||||
OriginalValues[cb] = (cb.IsChecked ?? false) ? bool.TrueString : null;
|
||||
foreach (var rb in RadioButtonInputs)
|
||||
OriginalValues[rb] = (rb.IsChecked ?? false) ? bool.TrueString : null;
|
||||
}
|
||||
|
||||
private void ClearInputs() {
|
||||
protected void ClearInputs() {
|
||||
Menu_Member_SendEmail.IsEnabled = false;
|
||||
AreaCommitment.Text = "- m²";
|
||||
OriginalValues.Clear();
|
||||
foreach (var tb in TextBoxInputs) {
|
||||
tb.Text = " ";
|
||||
tb.Text = "";
|
||||
}
|
||||
foreach (var cb in ComboBoxInputs) {
|
||||
cb.SelectedItem = null;
|
||||
if (cb.ItemsSource != null)
|
||||
Utils.SetInputInvalid(cb);
|
||||
}
|
||||
foreach (var cb in CheckBoxInputs)
|
||||
cb.IsChecked = false;
|
||||
foreach (var rb in RadioButtonInputs)
|
||||
rb.IsChecked = false;
|
||||
ClearOriginalValues();
|
||||
base.ClearInputs();
|
||||
}
|
||||
|
||||
private bool IsValid() {
|
||||
return Valid.All(kv => kv.Value) && ComboBoxInputs.All(cb => cb.ItemsSource == null || cb.SelectedItem != null);
|
||||
}
|
||||
|
||||
private void UpdateButtons() {
|
||||
override protected void UpdateButtons() {
|
||||
if (!IsEditing && !IsCreating) return;
|
||||
bool ch = HasChanged(), v = IsValid();
|
||||
ResetButton.IsEnabled = (ch);
|
||||
SaveButton.IsEnabled = (v && ch);
|
||||
}
|
||||
|
||||
private bool InputHasChanged(Control input) {
|
||||
if (!OriginalValues.ContainsKey(input)) {
|
||||
return false;
|
||||
} else if (input is TextBox tb) {
|
||||
return OriginalValues[tb]?.ToString() != tb.Text;
|
||||
} else if (input is ComboBox sb) {
|
||||
return OriginalValues[sb] != sb.SelectedItem;
|
||||
} else if (input is CheckBox cb) {
|
||||
return (OriginalValues[cb] != null) != (cb.IsChecked ?? false);
|
||||
} else if (input is RadioButton rb) {
|
||||
return (OriginalValues[rb] != null) != (rb.IsChecked ?? false);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
protected void InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, Member?, ValidationResult> checker) {
|
||||
InputTextChanged(input, checker(input, SenderIsRequired(input), Context, (Member)MemberList.SelectedItem));
|
||||
}
|
||||
|
||||
private bool HasChanged() {
|
||||
return !IsValid() ||
|
||||
TextBoxInputs.Any(InputHasChanged) ||
|
||||
ComboBoxInputs.Any(InputHasChanged) ||
|
||||
CheckBoxInputs.Any(InputHasChanged) ||
|
||||
RadioButtonInputs.Any(InputHasChanged);
|
||||
}
|
||||
|
||||
private void UpdatePlz(TextBox plzInput, ComboBox ortInput, bool required) {
|
||||
if (plzInput.Text.Length == 4) {
|
||||
int plz = int.Parse(plzInput.Text);
|
||||
ortInput.ItemsSource = Context.Postleitzahlen.Where(p => p.Plz == plz).ToHashSet();
|
||||
} else {
|
||||
ortInput.ItemsSource = null;
|
||||
}
|
||||
ortInput.SelectedItem = null;
|
||||
if (ortInput.ItemsSource != null) {
|
||||
Utils.SetInputInvalid(ortInput);
|
||||
} else {
|
||||
Utils.ClearInputState(ortInput);
|
||||
}
|
||||
Valid[plzInput] = !required || (ortInput.ItemsSource != null);
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void InputTextChanged(TextBox input, bool required, Func<TextBox, bool, ValidationResult> checker) {
|
||||
InputTextChanged(input, required, (tb, opt, ctx) => checker(tb, opt));
|
||||
}
|
||||
|
||||
private void InputTextChanged(TextBox input, bool required, Func<TextBox, bool, AppDbContext, ValidationResult> checker) {
|
||||
InputTextChanged(input, required, (tb, opt, ctx, m) => checker(tb, opt, ctx));
|
||||
}
|
||||
|
||||
private void InputTextChanged(TextBox input, bool required, Func<TextBox, bool, AppDbContext, Member, ValidationResult> checker) {
|
||||
var res = checker(input, required, Context, (Member)MemberList.SelectedItem);
|
||||
Valid[input] = res.IsValid;
|
||||
if (res.IsValid) {
|
||||
if (InputHasChanged(input)) {
|
||||
Utils.SetInputChanged(input);
|
||||
} else {
|
||||
Utils.ClearInputState(input);
|
||||
}
|
||||
} else {
|
||||
Utils.SetInputInvalid(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void InputLostFocus(TextBox input, bool required, Func<TextBox, bool, ValidationResult> checker, string? msg = null) {
|
||||
InputLostFocus(input, required, (tb, required, ctx) => checker(tb, required), msg);
|
||||
}
|
||||
|
||||
private void InputLostFocus(TextBox input, bool required, Func<TextBox, bool, AppDbContext, ValidationResult> checker, string? msg = null) {
|
||||
InputLostFocus(input, required, (tb, required, ctx, m) => checker(tb, required, ctx), msg);
|
||||
}
|
||||
|
||||
private void InputLostFocus(TextBox input, bool required, Func<TextBox, bool, AppDbContext, Member?, ValidationResult> checker, string? msg = null) {
|
||||
var res = checker(input, required, Context, (Member)MemberList.SelectedItem);
|
||||
if (!res.IsValid)
|
||||
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
}
|
||||
|
||||
private void CheckBox_Changed(object sender, RoutedEventArgs evt) {
|
||||
var input = (CheckBox)sender;
|
||||
if (InputHasChanged(input)) {
|
||||
Utils.SetInputChanged(input);
|
||||
} else {
|
||||
Utils.ClearInputState(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void RadioButton_Changed(object sender, RoutedEventArgs evt) {
|
||||
var input = (RadioButton)sender;
|
||||
if (InputHasChanged(input)) {
|
||||
Utils.SetInputChanged(input);
|
||||
} else {
|
||||
Utils.ClearInputState(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void TextBox_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
var input = (TextBox)sender;
|
||||
if (SenderIsRequired(input) && input.Text.Length == 0) {
|
||||
Valid[input] = false;
|
||||
Utils.SetInputInvalid(input);
|
||||
} else {
|
||||
Valid[input] = true;
|
||||
if (InputHasChanged(input)) {
|
||||
Utils.SetInputChanged(input);
|
||||
} else {
|
||||
Utils.ClearInputState(input);
|
||||
}
|
||||
}
|
||||
UpdateButtons();
|
||||
}
|
||||
|
||||
private void ComboBox_SelectionChanged(object sender, RoutedEventArgs evt) {
|
||||
var input = (ComboBox)sender;
|
||||
if (input.ItemsSource != null && input.SelectedItem == null) {
|
||||
Utils.SetInputInvalid(input);
|
||||
} else if (InputHasChanged(input)) {
|
||||
Utils.SetInputChanged(input);
|
||||
} else {
|
||||
Utils.ClearInputState(input);
|
||||
}
|
||||
UpdateButtons();
|
||||
protected void InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, Member?, ValidationResult> checker, string? msg = null) {
|
||||
InputLostFocus(input, checker(input, SenderIsRequired(input), Context, (Member)MemberList.SelectedItem), msg);
|
||||
}
|
||||
|
||||
private void NumericInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckNumeric);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckNumeric);
|
||||
}
|
||||
|
||||
private void MgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckMgNr);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckMgNr);
|
||||
}
|
||||
|
||||
private void MgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckMgNr);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckMgNr);
|
||||
}
|
||||
|
||||
private void PredecessorMgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPredecessorMgNr);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckPredecessorMgNr);
|
||||
}
|
||||
|
||||
private void PredecessorMgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPredecessorMgNr);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckPredecessorMgNr);
|
||||
}
|
||||
|
||||
private void BirthdayInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPartialDate);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckPartialDate);
|
||||
}
|
||||
|
||||
private void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, OrtInput, SenderIsRequired(sender));
|
||||
InputTextChanged((TextBox)sender, Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, OrtInput);
|
||||
}
|
||||
|
||||
private void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, OrtInput, SenderIsRequired(sender));
|
||||
InputLostFocus((TextBox)sender, Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, OrtInput);
|
||||
}
|
||||
|
||||
private void PhoneNrInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPhoneNumber);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckPhoneNumber);
|
||||
}
|
||||
|
||||
private void PhoneNrInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPhoneNumber);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckPhoneNumber);
|
||||
}
|
||||
|
||||
private void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckEmailAddress);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckEmailAddress);
|
||||
}
|
||||
|
||||
private void EmailInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckEmailAddress);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckEmailAddress);
|
||||
}
|
||||
|
||||
private void IbanInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckIban);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckIban);
|
||||
}
|
||||
|
||||
private void IbanInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckIban);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckIban);
|
||||
}
|
||||
|
||||
private void BicInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckBic);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckBic);
|
||||
}
|
||||
|
||||
private void BicInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckBic);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckBic);
|
||||
}
|
||||
|
||||
private void UstIdInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckUstId);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckUstId);
|
||||
}
|
||||
|
||||
private void UstIdInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckUstId);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckUstId);
|
||||
}
|
||||
|
||||
private void LfbisNrInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckLfbisNr);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckLfbisNr);
|
||||
}
|
||||
|
||||
private void LfbisNrInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckLfbisNr);
|
||||
InputLostFocus((TextBox)sender, Validator.CheckLfbisNr);
|
||||
}
|
||||
|
||||
private void BillingPlzInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, BillingOrtInput, SenderIsRequired(sender));
|
||||
InputTextChanged((TextBox)sender, Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, BillingOrtInput);
|
||||
}
|
||||
|
||||
private void BillingPlzInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
InputLostFocus((TextBox)sender, SenderIsRequired(sender), Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, BillingOrtInput, SenderIsRequired(sender));
|
||||
InputLostFocus((TextBox)sender, Validator.CheckPlz);
|
||||
UpdatePlz((TextBox)sender, BillingOrtInput);
|
||||
}
|
||||
|
||||
private void DateInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
InputTextChanged((TextBox)sender, SenderIsRequired(sender), Validator.CheckDate);
|
||||
InputTextChanged((TextBox)sender, Validator.CheckDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user