Simplify filtering

This commit is contained in:
Michael Bucari-Tovo 2022-05-16 12:06:41 -06:00
parent 5dcdf670be
commit 7b8a4e4d72

View File

@ -13,12 +13,11 @@ namespace LibationWinForms
* *
* When filtering is applied, the filtered-out items are removed * When filtering is applied, the filtered-out items are removed
* from the base list and added to the private FilterRemoved list. * from the base list and added to the private FilterRemoved list.
* All items, filtered or not, are stored in the private AllItems * When filtering is removed, items in the FilterRemoved list are
* list. When filtering is removed, items in the FilterRemoved list * added back to the base list.
* are added back to the base list.
* *
* Remove and InsertItem are overridden to ensure that the base * Remove is overridden to ensure that removed items are removed from
* list remains synchronized with the AllItems list. * the base list (visible items) as well as the FilterRemoved list.
*/ */
internal class SortableFilterableBindingList : SortableBindingList<GridEntry>, IBindingListView internal class SortableFilterableBindingList : SortableBindingList<GridEntry>, IBindingListView
{ {
@ -29,13 +28,10 @@ namespace LibationWinForms
/// <summary> /// <summary>
/// Tracks all items in the list, both filtered and not. /// Tracks all items in the list, both filtered and not.
/// </summary> /// </summary>
public readonly List<GridEntry> AllItems;
private string FilterString; private string FilterString;
private Action Sort; private Action Sort;
public SortableFilterableBindingList(IEnumerable<GridEntry> enumeration) : base(enumeration) public SortableFilterableBindingList(IEnumerable<GridEntry> enumeration) : base(enumeration)
{ {
AllItems = new List<GridEntry>(Items);
//This is only necessary because SortableBindingList doesn't expose Sort() //This is only necessary because SortableBindingList doesn't expose Sort()
//You should make SortableBindingList.Sort protected and remove reflection //You should make SortableBindingList.Sort protected and remove reflection
var method = typeof(SortableBindingList<GridEntry>).GetMethod("Sort", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var method = typeof(SortableBindingList<GridEntry>).GetMethod("Sort", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
@ -58,16 +54,11 @@ namespace LibationWinForms
public new void Remove(GridEntry entry) public new void Remove(GridEntry entry)
{ {
AllItems.Remove(entry);
FilterRemoved.Remove(entry); FilterRemoved.Remove(entry);
base.Remove(entry); base.Remove(entry);
} }
protected override void InsertItem(int index, GridEntry item) public List<GridEntry> AllItems => Items.Concat(FilterRemoved).ToList();
{
AllItems.Insert(index, item);
base.InsertItem(index, item);
}
private void ApplyFilter(string filterString) private void ApplyFilter(string filterString)
{ {
@ -95,7 +86,7 @@ namespace LibationWinForms
if (FilterString is null) return; if (FilterString is null) return;
for (int i = 0; i < FilterRemoved.Count; i++) for (int i = 0; i < FilterRemoved.Count; i++)
Insert(i, FilterRemoved[i]); base.InsertItem(i, FilterRemoved[i]);
FilterRemoved.Clear(); FilterRemoved.Clear();