Add RenewContext
This commit is contained in:
@ -1,27 +1,56 @@
|
||||
using Elwig.Helpers;
|
||||
using Elwig.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Xceed.Wpf.Toolkit;
|
||||
|
||||
namespace Elwig.Windows {
|
||||
public abstract class AdministrationWindow : ContextWindow {
|
||||
|
||||
protected Control[] ExemptInputs { private get; set; }
|
||||
protected Control[] RequiredInputs { private get; set; }
|
||||
|
||||
private bool _IsEditing;
|
||||
private bool _IsCreating;
|
||||
protected bool IsEditing {
|
||||
get { return _IsEditing; }
|
||||
set {
|
||||
_IsEditing = value;
|
||||
LockContext = IsEditing || IsCreating;
|
||||
}
|
||||
}
|
||||
protected bool IsCreating {
|
||||
get { return _IsCreating; }
|
||||
set {
|
||||
_IsCreating = value;
|
||||
LockContext = IsEditing || IsCreating;
|
||||
}
|
||||
}
|
||||
|
||||
private TextBox[] TextBoxInputs;
|
||||
private TextBox[] PlzInputs;
|
||||
private ComboBox[] ComboBoxInputs;
|
||||
private ComboBox[] PlzOrtInputs;
|
||||
private CheckComboBox[] CheckComboBoxInputs;
|
||||
private CheckBox[] CheckBoxInputs;
|
||||
private RadioButton[] RadioButtonInputs;
|
||||
private readonly Dictionary<Control, bool> Valid;
|
||||
private readonly Dictionary<Control, object?> OriginalValues;
|
||||
|
||||
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();
|
||||
@ -33,14 +62,30 @@ namespace Elwig.Windows {
|
||||
TextBoxInputs = Utils.FindAllChildren<TextBox>(this, ExemptInputs).ToArray();
|
||||
ComboBoxInputs = Utils.FindAllChildren<ComboBox>(this, ExemptInputs).ToArray();
|
||||
CheckBoxInputs = Utils.FindAllChildren<CheckBox>(this, ExemptInputs).ToArray();
|
||||
CheckComboBoxInputs = Utils.FindAllChildren<CheckComboBox>(this, ExemptInputs).ToArray();
|
||||
RadioButtonInputs = Utils.FindAllChildren<RadioButton>(this, ExemptInputs).ToArray();
|
||||
PlzInputs = TextBoxInputs.Where(tb => "PLZ".Equals(tb.Tag)).ToArray();
|
||||
PlzOrtInputs = PlzInputs.Select(tb => Utils.FindNextSibling<ComboBox>(tb) ?? throw new MissingMemberException()).ToArray();
|
||||
foreach (var tb in TextBoxInputs)
|
||||
Valid[tb] = true;
|
||||
foreach (var cb in ComboBoxInputs)
|
||||
cb.SelectionChanged += ComboBox_SelectionChanged;
|
||||
foreach (var cb in CheckComboBoxInputs)
|
||||
cb.ItemSelectionChanged += ComboBox_SelectionChanged;
|
||||
ValidateRequiredInputs();
|
||||
}
|
||||
|
||||
private ComboBox GetPlzOrtInput(TextBox input) {
|
||||
return PlzOrtInputs[Array.IndexOf(PlzInputs, input)];
|
||||
}
|
||||
|
||||
abstract protected void UpdateButtons();
|
||||
|
||||
protected override async Task RenewContext() {
|
||||
for (int i = 0; i < PlzInputs.Length; i++)
|
||||
UpdatePlz(PlzInputs[i], PlzOrtInputs[i]);
|
||||
}
|
||||
|
||||
protected void ValidateInput(Control input, bool valid) {
|
||||
Valid[input] = valid;
|
||||
}
|
||||
@ -122,6 +167,10 @@ namespace Elwig.Windows {
|
||||
|
||||
protected bool IsValid => Valid.All(kv => kv.Value);
|
||||
|
||||
protected bool GetInputValid(Control input) {
|
||||
return Valid[input];
|
||||
}
|
||||
|
||||
protected bool InputHasChanged(Control input) {
|
||||
if (!OriginalValues.ContainsKey(input)) {
|
||||
return false;
|
||||
@ -145,14 +194,13 @@ namespace Elwig.Windows {
|
||||
CheckBoxInputs.Any(InputHasChanged) ||
|
||||
RadioButtonInputs.Any(InputHasChanged);
|
||||
|
||||
protected void UpdatePlz(TextBox plzInput, bool plzInputValid, ComboBox ortInput) {
|
||||
protected void UpdatePlz(TextBox plzInput, ComboBox ortInput) {
|
||||
var plzInputValid = GetInputValid(plzInput);
|
||||
var item = ortInput.SelectedItem;
|
||||
var list = plzInputValid && plzInput.Text.Length == 4 ? Context.Postleitzahlen.Find(int.Parse(plzInput.Text))?.Orte.ToList() : null;
|
||||
ortInput.ItemsSource = list;
|
||||
ortInput.SelectedItem = (list != null && item != null && list.Contains(item)) ? item : null;
|
||||
Utils.RenewItemsSource(ortInput, list, i => (i as AT_PlzDest)?.Id);
|
||||
if (list != null && ortInput.SelectedItem == null && list.Count == 1)
|
||||
ortInput.SelectedItem = list[0];
|
||||
ComboBox_SelectionChanged(ortInput, new());
|
||||
}
|
||||
|
||||
protected bool InputTextChanged(TextBox input, Func<TextBox, bool, ValidationResult> checker) {
|
||||
@ -188,7 +236,7 @@ namespace Elwig.Windows {
|
||||
|
||||
protected bool InputLostFocus(TextBox input, ValidationResult res, string? msg = null) {
|
||||
if (!res.IsValid)
|
||||
MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
System.Windows.MessageBox.Show(res.ErrorContent.ToString(), msg ?? res.ErrorContent.ToString(), MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return res.IsValid;
|
||||
}
|
||||
|
||||
@ -270,14 +318,14 @@ namespace Elwig.Windows {
|
||||
|
||||
protected void PlzInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
var plz = (TextBox)sender;
|
||||
var valid = InputTextChanged(plz, Validator.CheckPlz);
|
||||
UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
|
||||
InputTextChanged(plz, Validator.CheckPlz);
|
||||
UpdatePlz(plz, GetPlzOrtInput(plz));
|
||||
}
|
||||
|
||||
protected void PlzInput_LostFocus(object sender, RoutedEventArgs evt) {
|
||||
var plz = (TextBox)sender;
|
||||
var valid = InputLostFocus(plz, Validator.CheckPlz);
|
||||
UpdatePlz(plz, valid, Utils.FindNextSibling<ComboBox>(plz));
|
||||
InputLostFocus(plz, Validator.CheckPlz);
|
||||
UpdatePlz(plz, GetPlzOrtInput(plz));
|
||||
}
|
||||
|
||||
protected void EmailInput_TextChanged(object sender, RoutedEventArgs evt) {
|
||||
|
Reference in New Issue
Block a user