Skip to content

Commit

Permalink
Goz3rr#173 - Search for items in containers
Browse files Browse the repository at this point in the history
This commit builds on my previous to allow for searching of items within containers.
  • Loading branch information
tasermonkey authored and Goz3rr committed Aug 15, 2020
1 parent 6e23f07 commit 59de9a9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
23 changes: 22 additions & 1 deletion SatisfactorySaveEditor/Model/SaveComponentModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,6 +46,23 @@ public ArrayPropertyViewModel Inventory
get => FindField<ArrayPropertyViewModel>("mInventoryStacks");
}

public override bool MatchesFilter(string filter)
{
return base.MatchesFilter(filter) || MatchesFilterInventory(filter);
}

private bool MatchesFilterInventory(string filter)
{

return Inventory?.Elements.Cast<StructPropertyViewModel>().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();
Expand Down
6 changes: 6 additions & 0 deletions SatisfactorySaveEditor/Model/SaveObjectModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -227,6 +228,11 @@ public T FindOrCreateField<T>(string fieldName, Action<T> 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
Expand Down
2 changes: 1 addition & 1 deletion SatisfactorySaveEditor/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SaveObjectModel>(filter);
Expand Down

0 comments on commit 59de9a9

Please sign in to comment.