[#43] AdministrationWindow: Do not use Context from ContextWindow any more
This commit is contained in:
		@@ -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<Control>();
 | 
			
		||||
            RequiredInputs = Array.Empty<Control>();
 | 
			
		||||
            TextBoxInputs = Array.Empty<TextBox>();
 | 
			
		||||
            PlzInputs = Array.Empty<TextBox>();
 | 
			
		||||
            ComboBoxInputs = Array.Empty<ComboBox>();
 | 
			
		||||
            CheckComboBoxInputs = Array.Empty<CheckComboBox>();
 | 
			
		||||
            PlzOrtInputs = Array.Empty<ComboBox>();
 | 
			
		||||
            CheckBoxInputs = Array.Empty<CheckBox>();
 | 
			
		||||
            RadioButtonInputs = Array.Empty<RadioButton>();
 | 
			
		||||
            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<object>().ToArray().SequenceEqual(((object[]?)OriginalValues[ccb]) ?? Array.Empty<object>());
 | 
			
		||||
                return !ccb.SelectedItems.Cast<object>().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<object>().ToArray().SequenceEqual(((object[]?)DefaultValues[ccb]) ?? Array.Empty<object>());
 | 
			
		||||
                return !ccb.SelectedItems.Cast<object>().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<AT_PlzDest>? 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<TextBox, bool, ValidationResult> checker) {
 | 
			
		||||
            return InputTextChanged(input, (tb, required, ctx) => checker(tb, required));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected bool InputTextChanged(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> 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<TextBox, bool, ValidationResult> checker, string? msg = null) {
 | 
			
		||||
            return InputLostFocus(input, (tb, requiered, ctx) => checker(tb, requiered), msg);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected bool InputLostFocus(TextBox input, Func<TextBox, bool, AppDbContext, ValidationResult> 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) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user