From 278d79429bcac7c9969b4746de235487ea309961 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sun, 5 Apr 2026 23:19:13 +0200 Subject: [PATCH] [#79] DeliveryAdminWindow: Cache modifiers --- Elwig/Helpers/AppDbContext.cs | 10 +++++----- Elwig/Windows/BaseDataWindow.xaml.Mod.cs | 4 ++-- Elwig/Windows/BaseDataWindow.xaml.cs | 2 +- Elwig/Windows/DeliveryAdminWindow.xaml.cs | 14 +++++++------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Elwig/Helpers/AppDbContext.cs b/Elwig/Helpers/AppDbContext.cs index ec90fe4..9a29d31 100644 --- a/Elwig/Helpers/AppDbContext.cs +++ b/Elwig/Helpers/AppDbContext.cs @@ -113,10 +113,10 @@ namespace Elwig.Helpers { .Where(l => includePredicate || !l.IsPredicate) .OrderBy(l => l.MinKmw)); - private static readonly Func> _compiledQueryModifiers = - EF.CompileAsyncQuery((ctx, year, incudeNotActive) => ctx.Modifiers - .Where(m => m.Year == year && (incudeNotActive || m.IsActive)) - .OrderBy(m => m.Ordering).ThenBy(m => m.Name)); + private static readonly Func> _compiledQueryModifiers = + EF.CompileAsyncQuery((ctx, year, incudeNotActive) => ctx.Modifiers + .Where(m => (year == null || m.Year == year) && (incudeNotActive || m.IsActive)) + .OrderBy(m => m.Year).ThenBy(m => m.Ordering).ThenBy(m => m.Name)); private static readonly Func> _compiledQueryMembers = EF.CompileAsyncQuery((ctx, mgnr, includeNotActive) => ctx.Members @@ -333,7 +333,7 @@ namespace Elwig.Helpers { } public IAsyncEnumerable FetchModifiers(int? year, bool incudeNotActive = true) { - return _compiledQueryModifiers.Invoke(this, year ?? 0, incudeNotActive); + return _compiledQueryModifiers.Invoke(this, year, incudeNotActive); } public IAsyncEnumerable FetchMembers(int? mgnr = null, bool includeNotActive = false, bool includeContactInfo = false) { diff --git a/Elwig/Windows/BaseDataWindow.xaml.Mod.cs b/Elwig/Windows/BaseDataWindow.xaml.Mod.cs index 431814e..d97b70d 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.Mod.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.Mod.cs @@ -22,7 +22,7 @@ namespace Elwig.Windows { private async Task ModifiersInitEditing(AppDbContext ctx) { SeasonList.IsEnabled = false; var year = (SeasonList.SelectedItem as Season)?.Year; - _modList = new(await ctx.FetchModifiers(year).ToListAsync()); + _modList = new(await ctx.FetchModifiers(year ?? 0).ToListAsync()); _mods = _modList.ToDictionary(m => m.ModId, m => (string?)m.ModId); _modIds = _modList.ToDictionary(m => m, m => m.ModId); ControlUtils.RenewItemsSource(SeasonModifierList, _modList); @@ -31,7 +31,7 @@ namespace Elwig.Windows { private async Task ModifiersFinishEditing(AppDbContext ctx) { var year = (SeasonList.SelectedItem as Season)?.Year; - ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.FetchModifiers(year).ToListAsync()); + ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.FetchModifiers(year ?? 0).ToListAsync()); _modList = null; _mods = null; _modIds = null; diff --git a/Elwig/Windows/BaseDataWindow.xaml.cs b/Elwig/Windows/BaseDataWindow.xaml.cs index a28d5f5..fa79cbe 100644 --- a/Elwig/Windows/BaseDataWindow.xaml.cs +++ b/Elwig/Windows/BaseDataWindow.xaml.cs @@ -168,7 +168,7 @@ namespace Elwig.Windows { .OrderBy(t => t.VtrgId) .ToListAsync(), null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(WineCultivationList, await ctx.FetchWineCultivations().ToListAsync(), null, ControlUtils.RenewSourceDefault.First); - ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.FetchModifiers(year).ToListAsync(), null, ControlUtils.RenewSourceDefault.First); + ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.FetchModifiers(year ?? 0).ToListAsync(), null, ControlUtils.RenewSourceDefault.First); } protected override void UpdateButtons() { diff --git a/Elwig/Windows/DeliveryAdminWindow.xaml.cs b/Elwig/Windows/DeliveryAdminWindow.xaml.cs index 9ed81a2..3b4ef88 100644 --- a/Elwig/Windows/DeliveryAdminWindow.xaml.cs +++ b/Elwig/Windows/DeliveryAdminWindow.xaml.cs @@ -37,6 +37,7 @@ namespace Elwig.Windows { private readonly Button[] WeighingButtons; private List WineQualityLevels = []; + private Dictionary> Modifiers = []; public DeliveryAdminWindow(bool receipt = false) { InitializeComponent(); @@ -506,7 +507,7 @@ namespace Elwig.Windows { } var attributes = await ctx.FetchWineAttributes(!IsCreating).ToListAsync(); - var modifiers = await ctx.FetchModifiers(year, !IsCreating).ToListAsync(); + Modifiers = await ctx.FetchModifiers(null).GroupBy(m => m.Year).ToDictionaryAsync(g => g.Key, g => g.ToList()); var font = new FontFamily("Segoe MDL2 Assets"); Menu_BulkAction_SetAttribute.Items.Clear(); @@ -526,7 +527,7 @@ namespace Elwig.Windows { Menu_BulkAction_AddModifier.Items.Clear(); Menu_BulkAction_RemoveModifier.Items.Clear(); - foreach (var mod in modifiers) { + foreach (var mod in Modifiers.GetValueOrDefault(ViewModel.SelectedDelivery?.Year ?? 0, []).Where(m => m.IsActive || !IsCreating)) { var i1 = new MenuItem { Header = mod.Name, }; @@ -552,7 +553,7 @@ namespace Elwig.Windows { ControlUtils.RenewItemsSource(CultivationInput, cultList, null, ControlUtils.RenewSourceDefault.First); WineQualityLevels = await ctx.FetchWineQualityLevels().ToListAsync(); ControlUtils.RenewItemsSource(WineQualityLevelInput, WineQualityLevels); - ControlUtils.RenewItemsSource(ModifiersInput, modifiers); + ControlUtils.RenewItemsSource(ModifiersInput, Modifiers.GetValueOrDefault(year, []).Where(m => m.IsActive || !IsCreating).ToList()); var origins = await ctx.WineOrigins.ToListAsync(); origins.ForEach(o => { origins.FirstOrDefault(p => p.HkId == o.ParentHkId)?.Children.Add(o); }); origins = [.. origins.OrderByDescending(o => o.SortKey).ThenBy(o => o.HkId)]; @@ -579,12 +580,11 @@ namespace Elwig.Windows { } private async Task RefreshDeliveryParts() { - using var ctx = new AppDbContext(); if (DeliveryList.SelectedItem is Delivery d) { - ControlUtils.RenewItemsSource(ModifiersInput, await ctx.FetchModifiers(d.Year, !IsCreating).ToListAsync()); + ControlUtils.RenewItemsSource(ModifiersInput, Modifiers.GetValueOrDefault(d.Year, []).Where(m => m.IsActive || !IsCreating).ToList()); ControlUtils.RenewItemsSource(DeliveryPartList, d.Parts, DeliveryPartList_SelectionChanged, ControlUtils.RenewSourceDefault.First); } else { - ControlUtils.RenewItemsSource(ModifiersInput, await ctx.FetchModifiers(ViewModel.FilterSeason, !IsCreating).ToListAsync()); + ControlUtils.RenewItemsSource(ModifiersInput, Modifiers.GetValueOrDefault(ViewModel.FilterSeason ?? 0, []).Where(m => m.IsActive || !IsCreating).ToList()); DeliveryPartList.ItemsSource = null; } } @@ -913,7 +913,7 @@ namespace Elwig.Windows { var attrList = await ctx.FetchWineAttributes(false).Cast().ToListAsync(); attrList.Insert(0, new NullItem("")); ControlUtils.RenewItemsSource(AttributeInput, attrList, null, ControlUtils.RenewSourceDefault.First); - ControlUtils.RenewItemsSource(ModifiersInput, await ctx.FetchModifiers(ViewModel.FilterSeason, false).ToListAsync()); + ControlUtils.RenewItemsSource(ModifiersInput, Modifiers.GetValueOrDefault(ViewModel.FilterSeason ?? 0, []).Where(m => m.IsActive).ToList()); ControlUtils.RenewItemsSource(MemberInput, await ctx.FetchMembers(includeNotActive: !ViewModel.IsReceipt, includeContactInfo: true).ToListAsync()); IsCreating = true; DeliveryList.IsEnabled = false;