From 59de9a9947bfd4de43c5d4da124c8d2b1590a565 Mon Sep 17 00:00:00 2001 From: James stapleton Date: Fri, 14 Aug 2020 21:15:42 -0400 Subject: [PATCH] #173 - Search for items in containers This commit builds on my previous to allow for searching of items within containers. --- .../Model/SaveComponentModel.cs | 23 ++++++++++++++++++- .../Model/SaveObjectModel.cs | 6 +++++ .../ViewModel/MainViewModel.cs | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/SatisfactorySaveEditor/Model/SaveComponentModel.cs b/SatisfactorySaveEditor/Model/SaveComponentModel.cs index 103931a..934ebcb 100644 --- a/SatisfactorySaveEditor/Model/SaveComponentModel.cs +++ b/SatisfactorySaveEditor/Model/SaveComponentModel.cs @@ -1,4 +1,8 @@ -using System.Windows; +using System; +using System.Collections.ObjectModel; +using System.Globalization; +using System.Linq; +using System.Windows; using GalaSoft.MvvmLight.Command; using SatisfactorySaveEditor.View; using SatisfactorySaveEditor.ViewModel; @@ -42,6 +46,23 @@ public ArrayPropertyViewModel Inventory get => FindField("mInventoryStacks"); } + public override bool MatchesFilter(string filter) + { + return base.MatchesFilter(filter) || MatchesFilterInventory(filter); + } + + private bool MatchesFilterInventory(string filter) + { + + return Inventory?.Elements.Cast().Any(element => + { + DynamicStructDataViewModel structData = (DynamicStructDataViewModel)element.StructData; + InventoryItem item = (InventoryItem)((StructPropertyViewModel) structData.Fields[0]).StructData; + + return item.ItemType.ToLower(CultureInfo.InvariantCulture).Contains(filter); + }) ?? false; + } + private void FillInventory() { FillWindow dialog = new FillWindow(); diff --git a/SatisfactorySaveEditor/Model/SaveObjectModel.cs b/SatisfactorySaveEditor/Model/SaveObjectModel.cs index 081f749..abe956b 100644 --- a/SatisfactorySaveEditor/Model/SaveObjectModel.cs +++ b/SatisfactorySaveEditor/Model/SaveObjectModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Globalization; using System.Linq; using System.Windows; using GalaSoft.MvvmLight; @@ -227,6 +228,11 @@ public T FindOrCreateField(string fieldName, Action edit = null) where T : throw new InvalidOperationException($"A field with the name {fieldName} already exists but with a different type ({field.GetType()} != {typeof(T)})"); } + public virtual bool MatchesFilter(string filter) + { + return this.Model?.InstanceName.ToLower(CultureInfo.InvariantCulture).Contains(filter) ?? false; + } + private void AddProperty() { AddWindow window = new AddWindow diff --git a/SatisfactorySaveEditor/ViewModel/MainViewModel.cs b/SatisfactorySaveEditor/ViewModel/MainViewModel.cs index 8935c04..7b20780 100644 --- a/SatisfactorySaveEditor/ViewModel/MainViewModel.cs +++ b/SatisfactorySaveEditor/ViewModel/MainViewModel.cs @@ -691,7 +691,7 @@ private void Filter(string value) else { var valueLower = value.ToLower(CultureInfo.InvariantCulture); - var filter = rootItem.DescendantSelfViewModel.WithCancellation(tokenSource.Token).Where(vm => vm.Model?.InstanceName.ToLower(CultureInfo.InvariantCulture).Contains(valueLower) ?? false); + var filter = rootItem.DescendantSelfViewModel.WithCancellation(tokenSource.Token).Where(vm => vm.MatchesFilter(valueLower)); Application.Current.Dispatcher.Invoke(() => { RootItem = new ObservableCollection(filter);