From 82fba7e75271341c2238118ee56b033da9e788e1 Mon Sep 17 00:00:00 2001 From: Mbucari Date: Tue, 11 Apr 2023 11:42:12 -0600 Subject: [PATCH] Grid refresh performance and behavior improvements --- Source/LibationUiBase/GridView/QueryExtensions.cs | 4 ++-- .../GridView/GridEntryBindingList.cs | 15 +++++++++++++-- Source/LibationWinForms/GridView/ProductsGrid.cs | 9 +++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Source/LibationUiBase/GridView/QueryExtensions.cs b/Source/LibationUiBase/GridView/QueryExtensions.cs index 77cf2a8a..571c0a3f 100644 --- a/Source/LibationUiBase/GridView/QueryExtensions.cs +++ b/Source/LibationUiBase/GridView/QueryExtensions.cs @@ -53,10 +53,10 @@ namespace LibationUiBase.GridView var searchResultSet = SearchEngineCommands.Search(searchString); - var booksFilteredIn = entries.BookEntries().Join(searchResultSet.Docs, lbe => lbe.AudibleProductId, d => d.ProductId, (lbe, d) => (IGridEntry)lbe); + var booksFilteredIn = entries.IntersectBy(searchResultSet.Docs.Select(d => d.ProductId), l => l.AudibleProductId); //Find all series containing children that match the search criteria - var seriesFilteredIn = entries.SeriesEntries().Where(s => s.Children.Join(searchResultSet.Docs, lbe => lbe.AudibleProductId, d => d.ProductId, (lbe, d) => lbe).Any()); + var seriesFilteredIn = booksFilteredIn.OfType().Where(lbe => lbe.Parent is not null).Select(lbe => lbe.Parent).Distinct(); return booksFilteredIn.Concat(seriesFilteredIn).ToHashSet(); } diff --git a/Source/LibationWinForms/GridView/GridEntryBindingList.cs b/Source/LibationWinForms/GridView/GridEntryBindingList.cs index 25f878ff..571db313 100644 --- a/Source/LibationWinForms/GridView/GridEntryBindingList.cs +++ b/Source/LibationWinForms/GridView/GridEntryBindingList.cs @@ -97,6 +97,9 @@ namespace LibationWinForms.GridView /// private void refreshEntries() { + var priorState = RaiseListChangedEvents; + RaiseListChangedEvents = false; + if (FilteredInGridEntries is null) { addRemovedItemsBack(FilterRemoved.ToList()); @@ -117,7 +120,8 @@ namespace LibationWinForms.GridView SortInternal(); - OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); + ResetList(); + RaiseListChangedEvents = priorState; void addRemovedItemsBack(List addBackEntries) { @@ -200,7 +204,7 @@ namespace LibationWinForms.GridView propertyDescriptor = property; isSorted = true; - OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); + ResetList(); } private void SortInternal() @@ -224,8 +228,15 @@ namespace LibationWinForms.GridView isSorted = false; propertyDescriptor = base.SortPropertyCore; Comparer.SortOrder = base.SortDirectionCore; + ResetList(); + } + private void ResetList() + { + var priorState = RaiseListChangedEvents; + RaiseListChangedEvents = true; OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); + RaiseListChangedEvents = priorState; } } } diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index 95d287e5..dfe6f530 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -189,6 +189,9 @@ namespace LibationWinForms.GridView internal void UpdateGrid(List dbBooks) { + //First row that is in view in the DataGridView + var topRow = gridEntryDataGridView.Rows.Cast().FirstOrDefault(r => r.Displayed)?.Index ?? 0; + #region Add new or update existing grid entries //Remove filter prior to adding/updating boooks @@ -201,6 +204,7 @@ namespace LibationWinForms.GridView var seriesEntries = bindingList.AllItems().SeriesEntries().ToList(); var parentedEpisodes = dbBooks.ParentedEpisodes().ToHashSet(); + bindingList.RaiseListChangedEvents = false; foreach (var libraryBook in dbBooks.OrderBy(e => e.DateAdded)) { var existingEntry = allEntries.FindByAsin(libraryBook.Book.AudibleProductId); @@ -216,8 +220,11 @@ namespace LibationWinForms.GridView AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); } } + bindingList.RaiseListChangedEvents = true; //Re-apply filter after adding new/updating existing books to capture any changes + //The Filter call also ensures that the binding list is reset so the DataGridView + //is made aware of all changes that were made while RaiseListChangedEvents was false Filter(existingFilter); #endregion @@ -231,6 +238,8 @@ namespace LibationWinForms.GridView .ExceptBy(dbBooks.Select(lb => lb.Book.AudibleProductId), ge => ge.AudibleProductId); RemoveBooks(removedBooks); + + gridEntryDataGridView.FirstDisplayedScrollingRowIndex = topRow; } public void RemoveBooks(IEnumerable removedBooks)