From 2df5d7842daad9c9d4669c4bf39424d29794f3f6 Mon Sep 17 00:00:00 2001 From: Mc-Pain Date: Sun, 11 Dec 2022 15:55:40 +0300 Subject: [PATCH 1/6] Optimize GetEnumerator() --- EDEngineer/Views/ShoppingListViewModel.cs | 38 ++++++++++++++--------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/EDEngineer/Views/ShoppingListViewModel.cs b/EDEngineer/Views/ShoppingListViewModel.cs index cb77a55c..2bc86fb8 100644 --- a/EDEngineer/Views/ShoppingListViewModel.cs +++ b/EDEngineer/Views/ShoppingListViewModel.cs @@ -219,27 +219,35 @@ public bool SynchronizeWithLogs public IEnumerator GetEnumerator() { - var ingredients = blueprints - .SelectMany(b => b) - .SelectMany(b => Enumerable.Repeat(b, b.ShoppingListCount)) - .SelectMany(b => b.Ingredients) - .ToList(); + var ingredients = new Dictionary(); + foreach (var b in blueprints.SelectMany(b => b).Where(b => b.ShoppingListCount > 0)) + { + foreach (var ingredient in b.Ingredients) + { + if (!ingredients.ContainsKey(ingredient.Entry)) + { + ingredients[ingredient.Entry] = 0; + } + ingredients[ingredient.Entry] += ingredient.Size * b.ShoppingListCount; + } + } if (!ingredients.Any()) { yield break; } - var composition = ingredients.GroupBy(i => i.Entry.Data.Name) - .Select( - i => - new BlueprintIngredient(i.First().Entry, - i.Sum(c => c.Size))) - .OrderBy(i => i.Entry.Count - i.Size > 0 ? 1 : 0) - .ThenByDescending(i => i.Entry.Data.Subkind) - .ThenBy(i => i.Entry.Data.Kind) - .ThenBy(i => languages.Translate(i.Entry.Data.Name)) - .ToList(); + var composition = new List(); + foreach (var i in ingredients) + { + composition.Add(new BlueprintIngredient(i.Key, i.Value)); + } + + composition = composition.OrderBy(i => i.Entry.Count - i.Size > 0 ? 1 : 0) + .ThenByDescending(i => i.Entry.Data.Subkind) + .ThenBy(i => i.Entry.Data.Kind) + .ThenBy(i => languages.Translate(i.Entry.Data.Name)) + .ToList(); var metaBlueprint = new Blueprint(languages, "", From 82f19d60fb0d075b9b70ce2cabcec0e44944fa31 Mon Sep 17 00:00:00 2001 From: Mc-Pain Date: Sun, 11 Dec 2022 15:55:40 +0300 Subject: [PATCH 2/6] store ShoppingListViewModel.ToList() --- EDEngineer/Views/ShoppingListViewModel.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/EDEngineer/Views/ShoppingListViewModel.cs b/EDEngineer/Views/ShoppingListViewModel.cs index 2bc86fb8..f2e79a24 100644 --- a/EDEngineer/Views/ShoppingListViewModel.cs +++ b/EDEngineer/Views/ShoppingListViewModel.cs @@ -18,12 +18,25 @@ public class ShoppingListViewModel : INotifyPropertyChanged, IShoppingList private readonly StateCargo stateCargo; private readonly ILanguage languages; private readonly List, Blueprint>> blueprints; + public List list = null; public ShoppingListViewModel(StateCargo stateCargo, List blueprints, ILanguage languages) { this.blueprints = blueprints.GroupBy(b => Tuple.Create(b.Type, b.BlueprintName)).ToList(); this.stateCargo = stateCargo; this.languages = languages; + list = this.ToList(); + + foreach (var blueprint in blueprints) + { + blueprint.PropertyChanged += (o, e) => + { + if (e.PropertyName == "ShoppingListCount") + { + list = this.ToList(); + } + }; + } foreach (var ingredientsValue in stateCargo.Ingredients) { @@ -44,7 +57,7 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } - public List List => this.ToList(); + public List List => list; public ILanguage Languages => this.languages; public List> Composition @@ -205,14 +218,12 @@ public bool SynchronizeWithLogs } public Dictionary Deduction => - this.ToList() - .FirstOrDefault() + list.FirstOrDefault() ?.Ingredients .ToDictionary(i => i.Entry.Data, i => i.Size); public Dictionary MissingIngredients => - this.ToList() - .FirstOrDefault() + list.FirstOrDefault() ?.Ingredients .Where(i => i.Size - i.Entry.Count > 0) .ToDictionary(i => i.Entry, i => i.Size - i.Entry.Count); From 190d0e4afe2dd7ea09e9cf39a7d301d6a7e9592f Mon Sep 17 00:00:00 2001 From: Mc-Pain Date: Sun, 11 Dec 2022 15:55:41 +0300 Subject: [PATCH 3/6] Don't call Settings.Default.Save() on each blueprint --- EDEngineer/Views/CommanderViewModel.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/EDEngineer/Views/CommanderViewModel.cs b/EDEngineer/Views/CommanderViewModel.cs index f5f31d1f..c52e8c45 100644 --- a/EDEngineer/Views/CommanderViewModel.cs +++ b/EDEngineer/Views/CommanderViewModel.cs @@ -242,7 +242,6 @@ private void LoadBlueprints(ILanguage languages, IEnumerable blueprin if (Settings.Default.Favorites.Contains($"{blueprint}")) { Settings.Default.Favorites.Remove($"{blueprint}"); - Settings.Default.Save(); } } else if (Settings.Default.Favorites.Contains($"{blueprint}")) @@ -251,7 +250,6 @@ private void LoadBlueprints(ILanguage languages, IEnumerable blueprin favoritedBlueprints.Add(blueprint); Settings.Default.Favorites.Remove($"{blueprint}"); Settings.Default.Favorites.Add(text); - Settings.Default.Save(); } if (Settings.Default.Ignored.Contains(text)) @@ -261,7 +259,6 @@ private void LoadBlueprints(ILanguage languages, IEnumerable blueprin if (Settings.Default.Ignored.Contains($"{blueprint}")) { Settings.Default.Ignored.Remove($"{blueprint}"); - Settings.Default.Save(); } } else if (Settings.Default.Ignored.Contains($"{blueprint}")) @@ -269,7 +266,6 @@ private void LoadBlueprints(ILanguage languages, IEnumerable blueprin blueprint.Ignored = true; Settings.Default.Ignored.Remove($"{blueprint}"); Settings.Default.Ignored.Add(text); - Settings.Default.Save(); } blueprint.ShoppingListCount = Settings.Default.ShoppingList.Cast().Count(l => l == text); @@ -321,6 +317,7 @@ private void LoadBlueprints(ILanguage languages, IEnumerable blueprin }; } + Settings.Default.Save(); Filters = new BlueprintFilters(languages, State.Blueprints); ShoppingList = new ShoppingListViewModel(State.Cargo, State.Blueprints, languages); From dfc8bc06806015cc6bf926f21df1bff43927a086 Mon Sep 17 00:00:00 2001 From: Mc-Pain Date: Sun, 11 Dec 2022 15:55:41 +0300 Subject: [PATCH 4/6] Optimize import/clear shopping list --- EDEngineer/Views/CommanderViewModel.cs | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/EDEngineer/Views/CommanderViewModel.cs b/EDEngineer/Views/CommanderViewModel.cs index c52e8c45..8567f9e8 100644 --- a/EDEngineer/Views/CommanderViewModel.cs +++ b/EDEngineer/Views/CommanderViewModel.cs @@ -312,7 +312,10 @@ private void LoadBlueprints(ILanguage languages, IEnumerable blueprin Settings.Default.ShoppingList.Add(text); } - Settings.Default.Save(); + if (!importingShoppingList) + { + Settings.Default.Save(); + } } }; } @@ -400,8 +403,12 @@ public void ShoppingListChange(Blueprint blueprint, int i) { blueprint.ShoppingListCount += i; - OnPropertyChanged(nameof(ShoppingList)); - OnPropertyChanged(nameof(ShoppingListItem)); + // Don't bother UI when there is import in progress. + if (!importingShoppingList) + { + OnPropertyChanged(nameof(ShoppingList)); + OnPropertyChanged(nameof(ShoppingListItem)); + } } } @@ -409,6 +416,7 @@ public void ImportShoppingList() { if (Helpers.TryRetrieveShoppingList(out var shoppingListItems)) { + importingShoppingList = true; var blueprints = State.Blueprints; if (shoppingListItems != null && shoppingListItems.Count > 0) @@ -433,6 +441,7 @@ public void ImportShoppingList() break; case MessageBoxResult.Cancel: // User pressed Cancel button so skip out of Import + importingShoppingList = false; return; } @@ -441,7 +450,7 @@ public void ImportShoppingList() } RefreshShoppingList(); - + importingShoppingList = false; } } @@ -478,14 +487,26 @@ public void ExportShoppingList() public void ClearShoppingList() { + var importingShoppingListOld = importingShoppingList; + importingShoppingList = true; + foreach (var tuple in ShoppingList.Composition.ToList()) { ShoppingListChange(tuple.Item1, tuple.Item2 * -1); } + + importingShoppingList = importingShoppingListOld; + + if (!importingShoppingList) + { + RefreshShoppingList(); + } } public int ShoppingListItem => 0; + public bool importingShoppingList { get; private set; } + public override string ToString() { return $"CMDR {CommanderName}"; From b9b231625bfa91a163ea876c643bff82f7e2c3f2 Mon Sep 17 00:00:00 2001 From: Mc-Pain Date: Sun, 11 Dec 2022 16:24:34 +0300 Subject: [PATCH 5/6] Use LINQ instead of foreach() --- EDEngineer/Views/ShoppingListViewModel.cs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/EDEngineer/Views/ShoppingListViewModel.cs b/EDEngineer/Views/ShoppingListViewModel.cs index f2e79a24..b0274139 100644 --- a/EDEngineer/Views/ShoppingListViewModel.cs +++ b/EDEngineer/Views/ShoppingListViewModel.cs @@ -248,17 +248,12 @@ public IEnumerator GetEnumerator() yield break; } - var composition = new List(); - foreach (var i in ingredients) - { - composition.Add(new BlueprintIngredient(i.Key, i.Value)); - } - - composition = composition.OrderBy(i => i.Entry.Count - i.Size > 0 ? 1 : 0) - .ThenByDescending(i => i.Entry.Data.Subkind) - .ThenBy(i => i.Entry.Data.Kind) - .ThenBy(i => languages.Translate(i.Entry.Data.Name)) - .ToList(); + var composition = ingredients.Select(i => new BlueprintIngredient(i.Key, i.Value)) + .OrderBy(i => i.Entry.Count - i.Size > 0 ? 1 : 0) + .ThenByDescending(i => i.Entry.Data.Subkind) + .ThenBy(i => i.Entry.Data.Kind) + .ThenBy(i => languages.Translate(i.Entry.Data.Name)) + .ToList(); var metaBlueprint = new Blueprint(languages, "", From 71d2439fbad21140bca3ab17a5731fbdf005ea1d Mon Sep 17 00:00:00 2001 From: Mc-Pain Date: Sat, 14 Jan 2023 20:08:17 +0300 Subject: [PATCH 6/6] Ability to hide capped materials --- EDEngineer/Views/CommanderViewModel.cs | 5 ++++- EDEngineer/Views/MainWindow.cs | 1 + EDEngineer/Views/MainWindow.xaml | 15 ++++++++++++--- EDEngineer/Views/MainWindowViewModel.cs | 10 ++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/EDEngineer/Views/CommanderViewModel.cs b/EDEngineer/Views/CommanderViewModel.cs index 8567f9e8..f3d72d1b 100644 --- a/EDEngineer/Views/CommanderViewModel.cs +++ b/EDEngineer/Views/CommanderViewModel.cs @@ -175,12 +175,15 @@ public ICollectionView FilterView(MainWindowViewModel parentViewModel, Kind kind e.Accepted = ((entry.Data.Kind & kind) == entry.Data.Kind || entry.Data.Kind == Kind.Unknown) && (parentViewModel.MaterialSubkindFilter == null || entry.Data.Kind == Kind.Data || parentViewModel.MaterialSubkindFilter == entry.Data.Subkind) && (parentViewModel.ShowZeroes || entry.Count != 0) && + (parentViewModel.ShowFull || entry.Count < entry.Data.MaximumCapacity) && (!parentViewModel.ShowOnlyForFavorites || favoritedBlueprints.Any(b => b.Ingredients.Any(i => i.Entry == entry))); }; parentViewModel.PropertyChanged += (o, e) => { - if (e.PropertyName == nameof(parentViewModel.ShowZeroes) || e.PropertyName == nameof(parentViewModel.ShowOnlyForFavorites)) + if (e.PropertyName == nameof(parentViewModel.ShowZeroes) || + e.PropertyName == nameof(parentViewModel.ShowFull) || + e.PropertyName == nameof(parentViewModel.ShowOnlyForFavorites)) { source.View.Refresh(); } diff --git a/EDEngineer/Views/MainWindow.cs b/EDEngineer/Views/MainWindow.cs index ed866947..a6703700 100644 --- a/EDEngineer/Views/MainWindow.cs +++ b/EDEngineer/Views/MainWindow.cs @@ -107,6 +107,7 @@ public MainWindow() { if (e.PropertyName == "ShowOnlyForFavorites" || e.PropertyName == "ShowZeroes" || + e.PropertyName == "ShowFull" || e.PropertyName == "CurrentCommander" || e.PropertyName == "MaterialSubkindFilter" || e.PropertyName == "IngredientsGrouped") diff --git a/EDEngineer/Views/MainWindow.xaml b/EDEngineer/Views/MainWindow.xaml index 57612516..053fe19a 100644 --- a/EDEngineer/Views/MainWindow.xaml +++ b/EDEngineer/Views/MainWindow.xaml @@ -485,6 +485,7 @@ + + + + @@ -505,7 +514,7 @@ @@ -513,14 +522,14 @@ + Grid.Column="5"> showFull; + set + { + showFull = value; + OnPropertyChanged(); + } + } public Subkind? MaterialSubkindFilter {