Skip to content

Commit 3925763

Browse files
committed
Refactor item filtering to not include empty entries after comma
Also remove extra border and use one built into textbox so aligns with mouse over etc, move filtering logic into UI instead of item class
1 parent d358d6d commit 3925763

File tree

3 files changed

+20
-42
lines changed

3 files changed

+20
-42
lines changed

Loki/InventoryListItem.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public InventoryListItem(string name, string category, SharedItemData itemData)
1717
ItemData = itemData;
1818
}
1919

20-
public static string ItemsFilter { get; set; }
21-
2220
public static List<InventoryListItem> AllItems = ItemDb.AllItems
2321
.Select(CreateListItem).ToList();
2422

@@ -56,32 +54,5 @@ private static InventoryListItem CreateListItem(SharedItemData itemData)
5654
};
5755
return new InventoryListItem(displayName, category, itemData);
5856
}
59-
60-
public static bool FilterListItem(InventoryListItem item)
61-
{
62-
if (string.IsNullOrWhiteSpace(ItemsFilter))
63-
{
64-
return true;
65-
}
66-
else
67-
{
68-
string[] filterItems = ItemsFilter.ToLower().Split(new char[] { ',' });
69-
70-
foreach(string filterItem in filterItems)
71-
{
72-
if(item.Name.ToLower().Contains(filterItem))
73-
{
74-
return true;
75-
}
76-
}
77-
78-
return false;
79-
}
80-
}
81-
82-
public static bool FilterListItemObject(object itemObject)
83-
{
84-
return FilterListItem(itemObject as InventoryListItem);
85-
}
8657
}
8758
}

Loki/MainWindow.xaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,23 +216,25 @@
216216
<Grid Grid.Row="0" Margin="0,4,4,0">
217217
<Grid.ColumnDefinitions>
218218
<ColumnDefinition Width="Auto"></ColumnDefinition>
219-
<ColumnDefinition></ColumnDefinition>
219+
<ColumnDefinition/>
220220
</Grid.ColumnDefinitions>
221221
<Label Grid.Column="0" FontSize="10">Search:</Label>
222-
<Border Grid.Column="1" BorderBrush="DarkGray" BorderThickness="1" Padding="2,0,2,0">
223-
<TextBox x:Name="itemSearch" VerticalContentAlignment="Center" TextChanged="itemSearch_TextChanged" Padding="0,0,-1,0" BorderBrush="{x:Null}"></TextBox>
224-
</Border>
222+
<TextBox Grid.Column="1" BorderBrush="DarkGray" BorderThickness="1" Padding="2,0,2,0"
223+
x:Name="ItemSearch" VerticalContentAlignment="Center" TextChanged="itemSearch_TextChanged" />
224+
225225
</Grid>
226226

227227
<!-- Item Picker -->
228228
<ScrollViewer Grid.Row="1" Margin="4,4,4,4" Padding="0,0,8,0" MinWidth="250">
229-
<ItemsControl x:Name="itemsControl" DockPanel.Dock="Left">
229+
<ItemsControl DockPanel.Dock="Left">
230230

231231
<ItemsControl.ItemsSource>
232232
<Binding>
233233
<Binding.Source>
234234
<CollectionViewSource
235-
Source="{x:Static loki:InventoryListItem.AllItems}">
235+
x:Name ="InventoryViewSource"
236+
Filter="InventoryViewSource_OnFilter"
237+
Source="{x:Static loki:InventoryListItem.AllItems}">
236238
<CollectionViewSource.GroupDescriptions>
237239
<PropertyGroupDescription PropertyName="Category" />
238240
</CollectionViewSource.GroupDescriptions>

Loki/MainWindow.xaml.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
@@ -84,8 +85,7 @@ public bool SaveInProgress
8485
get => (bool) GetValue(SaveInProgressProperty);
8586
set => SetValue(SaveInProgressProperty, value);
8687
}
87-
88-
88+
8989
private static void SelectedCharacterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
9090
{
9191
var window = (MainWindow) d;
@@ -233,13 +233,18 @@ private async Task ShowNotification(string notifyText)
233233

234234
}
235235

236-
private void itemSearch_TextChanged(object sender, TextChangedEventArgs e)
236+
private void itemSearch_TextChanged(object sender, TextChangedEventArgs e)
237+
=> InventoryViewSource.View.Refresh();
238+
239+
private void InventoryViewSource_OnFilter(object sender, FilterEventArgs e)
237240
{
238-
InventoryListItem.ItemsFilter = itemSearch.Text;
239-
if (itemsControl?.ItemsSource != null)
241+
if (string.IsNullOrWhiteSpace(ItemSearch.Text))
242+
e.Accepted = true;
243+
else if (e.Item is InventoryListItem item)
240244
{
241-
var collectionViewSource = CollectionViewSource.GetDefaultView(itemsControl.ItemsSource);
242-
collectionViewSource.Filter = new Predicate<object>(InventoryListItem.FilterListItemObject);
245+
string[] filterItems = ItemSearch.Text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
246+
e.Accepted = filterItems.Any(filterItem =>
247+
item.Name.Contains(filterItem, StringComparison.OrdinalIgnoreCase));
243248
}
244249
}
245250
}

0 commit comments

Comments
 (0)