From 0c63e315bb9e20b8b2e67b95a26f0a0e5aa2f054 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Mon, 11 Mar 2024 10:14:39 +0100 Subject: [PATCH] [#43] AdministrationWindow: Do not use Context from ContextWindow any more --- Elwig/Helpers/Validator.cs | 12 +++-- Elwig/Windows/AdministrationWindow.cs | 68 ++++++++++++++------------- 2 files changed, 43 insertions(+), 37 deletions(-) diff --git a/Elwig/Helpers/Validator.cs b/Elwig/Helpers/Validator.cs index bdb58df..be5904d 100644 --- a/Elwig/Helpers/Validator.cs +++ b/Elwig/Helpers/Validator.cs @@ -128,13 +128,14 @@ namespace Elwig.Helpers { return new(true, null); } - public static ValidationResult CheckPlz(TextBox input, bool required, AppDbContext ctx) { + public static ValidationResult CheckPlz(TextBox input, bool required) { CheckInteger(input, false, 4); if (!required && input.Text.Length == 0) { return new(true, null); } else if (input.Text.Length != 4) { return new(false, "PLZ zu kurz"); } + using var ctx = new AppDbContext(); int plz = int.Parse(input.Text); if (ctx.Postleitzahlen.Find(plz) == null) { return new(false, "Ungültige PLZ"); @@ -413,7 +414,7 @@ namespace Elwig.Helpers { } } - public static ValidationResult CheckMgNr(TextBox input, bool required, AppDbContext ctx) { + public static ValidationResult CheckMgNr(TextBox input, bool required) { var res = CheckInteger(input, required); if (!res.IsValid) { return res; @@ -421,6 +422,7 @@ namespace Elwig.Helpers { return new(true, null); } + using var ctx = new AppDbContext(); int nr = int.Parse(input.Text); if (!ctx.MgNrExists(nr).GetAwaiter().GetResult()) { return new(false, "Ungültige Mitgliedsnummer"); @@ -446,7 +448,7 @@ namespace Elwig.Helpers { return new(true, null); } - public static ValidationResult CheckSortId(TextBox input, bool required, AppDbContext ctx) { + public static ValidationResult CheckSortId(TextBox input, bool required) { var res = CheckUpperCase(input, required, 3); if (!res.IsValid) { return res; @@ -454,6 +456,7 @@ namespace Elwig.Helpers { return new(true, null); } + using var ctx = new AppDbContext(); if (input.Text.Length < 2 || !ctx.SortIdExists(input.Text[0..2]).GetAwaiter().GetResult()) { return new(false, "Ungültige Sorte"); } else if (input.Text.Length >= 3) { @@ -466,8 +469,9 @@ namespace Elwig.Helpers { return new(true, null); } - public static ValidationResult CheckPredecessorMgNr(TextBox input, bool required, AppDbContext ctx) { + public static ValidationResult CheckPredecessorMgNr(TextBox input, bool required) { var res = CheckInteger(input, required); + using var ctx = new AppDbContext(); if (!res.IsValid) { return res; } else if (!required && input.Text.Length == 0) { diff --git a/Elwig/Windows/AdministrationWindow.cs b/Elwig/Windows/AdministrationWindow.cs index 10f9ac3..730c4e5 100644 --- a/Elwig/Windows/AdministrationWindow.cs +++ b/Elwig/Windows/AdministrationWindow.cs @@ -4,6 +4,7 @@ using Elwig.Models.Entities; using System; using System.Collections.Generic; using System.ComponentModel; +using Microsoft.EntityFrameworkCore; using System.Linq; using System.Threading.Tasks; using System.Windows; @@ -50,18 +51,18 @@ namespace Elwig.Windows { public AdministrationWindow() : base() { IsEditing = false; IsCreating = false; - ExemptInputs = Array.Empty(); - RequiredInputs = Array.Empty(); - TextBoxInputs = Array.Empty(); - PlzInputs = Array.Empty(); - ComboBoxInputs = Array.Empty(); - CheckComboBoxInputs = Array.Empty(); - PlzOrtInputs = Array.Empty(); - CheckBoxInputs = Array.Empty(); - RadioButtonInputs = Array.Empty(); - Valid = new(); - OriginalValues = new(); - DefaultValues = new(); + ExemptInputs = []; + RequiredInputs = []; + TextBoxInputs = []; + PlzInputs = []; + ComboBoxInputs = []; + CheckComboBoxInputs = []; + PlzOrtInputs = []; + CheckBoxInputs = []; + RadioButtonInputs = []; + Valid = []; + OriginalValues = []; + DefaultValues = []; Closing += OnClosing; Loaded -= base.OnLoaded; Loaded += OnLoaded; @@ -104,7 +105,7 @@ namespace Elwig.Windows { protected override async Task OnRenewContext() { for (int i = 0; i < PlzInputs.Length; i++) - UpdatePlz(PlzInputs[i], PlzOrtInputs[i]); + await UpdatePlz(PlzInputs[i], PlzOrtInputs[i]); } protected void ValidateInput(Control input, bool valid) { @@ -284,7 +285,7 @@ namespace Elwig.Windows { } else if (input is ComboBox sb) { return OriginalValues[sb] != sb.SelectedItem; } else if (input is CheckComboBox ccb) { - return !ccb.SelectedItems.Cast().ToArray().SequenceEqual(((object[]?)OriginalValues[ccb]) ?? Array.Empty()); + return !ccb.SelectedItems.Cast().ToArray().SequenceEqual(((object[]?)OriginalValues[ccb]) ?? []); } else if (input is CheckBox cb) { return (string?)OriginalValues[cb] != cb.IsChecked?.ToString(); } else if (input is RadioButton rb) { @@ -303,7 +304,7 @@ namespace Elwig.Windows { } 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()); + return !ccb.SelectedItems.Cast().ToArray().SequenceEqual(((object[]?)DefaultValues[ccb]) ?? []); } else if (input is CheckBox cb) { return (string?)DefaultValues[cb] != cb.IsChecked?.ToString(); } else if (input is RadioButton rb) { @@ -329,10 +330,19 @@ namespace Elwig.Windows { RadioButtonInputs.Any(InputIsNotDefault) ); - protected void UpdatePlz(TextBox plzInput, ComboBox ortInput) { - var plzInputValid = Validator.CheckPlz(plzInput, RequiredInputs.Contains(plzInput), Context).IsValid; - var item = ortInput.SelectedItem; - var list = plzInputValid && plzInput.Text.Length == 4 ? Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList() : null; + protected async Task UpdatePlz(TextBox plzInput, ComboBox ortInput) { + var plzInputValid = Validator.CheckPlz(plzInput, RequiredInputs.Contains(plzInput)).IsValid; + + List? list = null; + if (plzInputValid && plzInput.Text.Length == 4) { + var plz = int.Parse(plzInput.Text); + using var ctx = new AppDbContext(); + list = await ctx.PlzDestinations + .Where(p => p.Plz == plz) + .Include(p => p.Ort) + .ToListAsync(); + } + ControlUtils.RenewItemsSource(ortInput, list, i => (i as AT_PlzDest)?.Id); if (list != null && ortInput.SelectedItem == null && list.Count == 1) ortInput.SelectedItem = list[0]; @@ -360,11 +370,7 @@ namespace Elwig.Windows { } protected bool InputTextChanged(TextBox input, Func checker) { - return InputTextChanged(input, (tb, required, ctx) => checker(tb, required)); - } - - protected bool InputTextChanged(TextBox input, Func checker) { - return InputTextChanged(input, checker(input, SenderIsRequired(input), Context)); + return InputTextChanged(input, checker(input, SenderIsRequired(input))); } protected bool InputTextChanged(TextBox input, ValidationResult res) { @@ -385,11 +391,7 @@ namespace Elwig.Windows { } protected bool InputLostFocus(TextBox input, Func checker, string? msg = null) { - return InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg); - } - - protected bool InputLostFocus(TextBox input, Func checker, string? msg = null) { - return InputLostFocus(input, checker(input, SenderIsRequired(input), Context), msg); + return InputLostFocus(input, checker(input, SenderIsRequired(input)), msg); } protected bool InputLostFocus(TextBox input, ValidationResult res, string? msg = null) { @@ -506,18 +508,18 @@ namespace Elwig.Windows { InputLostFocus((TextBox)sender, Validator.CheckTime); } - protected void PlzInput_TextChanged(object sender, TextChangedEventArgs evt) { + protected async void PlzInput_TextChanged(object sender, TextChangedEventArgs evt) { var plz = (TextBox)sender; InputTextChanged(plz, Validator.CheckPlz); if ("PLZ".Equals(plz.Tag)) - UpdatePlz(plz, GetPlzOrtInput(plz)); + await UpdatePlz(plz, GetPlzOrtInput(plz)); } - protected void PlzInput_LostFocus(object sender, RoutedEventArgs evt) { + protected async void PlzInput_LostFocus(object sender, RoutedEventArgs evt) { var plz = (TextBox)sender; InputLostFocus(plz, Validator.CheckPlz); if ("PLZ".Equals(plz.Tag)) - UpdatePlz(plz, GetPlzOrtInput(plz)); + await UpdatePlz(plz, GetPlzOrtInput(plz)); } protected void EmailAddressInput_TextChanged(object sender, TextChangedEventArgs evt) {