From 1939aae81c95b613756ab5ff513cb92b3d8642cd Mon Sep 17 00:00:00 2001 From: Mbucari <37587114+Mbucari@users.noreply.github.com> Date: Mon, 10 Apr 2023 18:51:11 -0600 Subject: [PATCH] Simplify and comment --- .../ViewModels/ProductsDisplayViewModel.cs | 2 +- .../GridView/QueryExtensions.cs | 6 +++ .../GridView/GridEntryBindingList.cs | 39 +++++++------------ 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs b/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs index fdb94086..8f6f1485 100644 --- a/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs +++ b/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs @@ -322,7 +322,7 @@ namespace LibationAvalonia.ViewModels { var filterResults = SOURCE.FilterEntries(FilterString); - if (filterResults is not null && (FilteredInGridEntries is null || FilteredInGridEntries.Intersect(filterResults).Count() != FilteredInGridEntries.Count)) + if (FilteredInGridEntries.SearchSetsDiffer(filterResults)) { FilteredInGridEntries = filterResults; await refreshGrid(); diff --git a/Source/LibationUiBase/GridView/QueryExtensions.cs b/Source/LibationUiBase/GridView/QueryExtensions.cs index 827d791b..77cf2a8a 100644 --- a/Source/LibationUiBase/GridView/QueryExtensions.cs +++ b/Source/LibationUiBase/GridView/QueryExtensions.cs @@ -41,6 +41,12 @@ namespace LibationUiBase.GridView } } + public static bool SearchSetsDiffer(this HashSet? searchSet, HashSet? otherSet) + => searchSet is null != otherSet is null || + (searchSet is not null && + otherSet is not null && + searchSet.Intersect(otherSet).Count() != searchSet.Count); + public static HashSet? FilterEntries(this IEnumerable entries, string searchString) { if (string.IsNullOrEmpty(searchString)) return null; diff --git a/Source/LibationWinForms/GridView/GridEntryBindingList.cs b/Source/LibationWinForms/GridView/GridEntryBindingList.cs index 8dd178d5..25f878ff 100644 --- a/Source/LibationWinForms/GridView/GridEntryBindingList.cs +++ b/Source/LibationWinForms/GridView/GridEntryBindingList.cs @@ -18,17 +18,21 @@ namespace LibationWinForms.GridView * * Remove is overridden to ensure that removed items are removed from * the base list (visible items) as well as the FilterRemoved list. + * + * Using BindingList.Add/Insert and BindingList.Remove will cause the + * BindingList to subscribe/unsibscribe to/from the item's PropertyChanged + * event. Adding or removing from the underlying list will not change the + * BindingList's subscription to that item. */ internal class GridEntryBindingList : BindingList, IBindingListView { public GridEntryBindingList(IEnumerable enumeration) : base(new List(enumeration)) { SearchEngineCommands.SearchEngineUpdated += SearchEngineCommands_SearchEngineUpdated; - + ListChanged += GridEntryBindingList_ListChanged; refreshEntries(); } - /// All items in the list, including those filtered out. public List AllItems() => Items.Concat(FilterRemoved).ToList(); @@ -51,7 +55,7 @@ namespace LibationWinForms.GridView if (Items.Count + FilterRemoved.Count == 0) return; - FilteredInGridEntries = Items.Concat(FilterRemoved).FilterEntries(FilterString); + FilteredInGridEntries = AllItems().FilterEntries(FilterString); refreshEntries(); } } @@ -61,18 +65,16 @@ namespace LibationWinForms.GridView protected override bool SupportsSearchingCore => true; protected override bool IsSortedCore => isSorted; protected override PropertyDescriptor SortPropertyCore => propertyDescriptor; - protected override ListSortDirection SortDirectionCore => listSortDirection; + protected override ListSortDirection SortDirectionCore => Comparer.SortOrder; /// Items that were removed from the base list due to filtering private readonly List FilterRemoved = new(); private string FilterString; private bool isSorted; - private ListSortDirection listSortDirection; private PropertyDescriptor propertyDescriptor; /// All GridEntries present in the current filter set. If null, no filter is applied and all entries are filtered in.(This was a performance choice) private HashSet FilteredInGridEntries; - #region Unused - Advanced Filtering public bool SupportsAdvancedSorting => false; @@ -113,15 +115,7 @@ namespace LibationWinForms.GridView } } - if (IsSortedCore) - Sort(); - else - { - //No user sort is applied, so do default sorting by DateAdded, descending - Comparer.PropertyName = nameof(IGridEntry.DateAdded); - Comparer.SortOrder = ListSortDirection.Descending; - Sort(); - } + SortInternal(); OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); @@ -144,7 +138,7 @@ namespace LibationWinForms.GridView { var filterResults = AllItems().FilterEntries(FilterString); - if (filterResults is not null && (FilteredInGridEntries is null || FilteredInGridEntries.Intersect(filterResults).Count() != FilteredInGridEntries.Count)) + if (FilteredInGridEntries.SearchSetsDiffer(filterResults)) { FilteredInGridEntries = filterResults; refreshEntries(); @@ -201,16 +195,15 @@ namespace LibationWinForms.GridView Comparer.PropertyName = property.Name; Comparer.SortOrder = direction; - Sort(); + SortInternal(); propertyDescriptor = property; - listSortDirection = direction; isSorted = true; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); } - private void Sort() + private void SortInternal() { var itemsList = (List)Items; //User Order/OrderDescending and replace items in list instead of using List.Sort() to achieve stable sorting. @@ -220,19 +213,17 @@ namespace LibationWinForms.GridView itemsList.AddRange(sortedItems); } - protected override void OnListChanged(ListChangedEventArgs e) + private void GridEntryBindingList_ListChanged(object sender, ListChangedEventArgs e) { - if (e.ListChangedType == ListChangedType.ItemChanged && isSorted && e.PropertyDescriptor == SortPropertyCore) + if (e.ListChangedType == ListChangedType.ItemChanged && IsSortedCore && e.PropertyDescriptor == SortPropertyCore) refreshEntries(); - else - base.OnListChanged(e); } protected override void RemoveSortCore() { isSorted = false; propertyDescriptor = base.SortPropertyCore; - listSortDirection = base.SortDirectionCore; + Comparer.SortOrder = base.SortDirectionCore; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); }