Grid refresh performance and behavior improvements

This commit is contained in:
Mbucari 2023-04-11 11:42:12 -06:00
parent 1a95f2923b
commit 82fba7e752
3 changed files with 24 additions and 4 deletions

View File

@ -53,10 +53,10 @@ namespace LibationUiBase.GridView
var searchResultSet = SearchEngineCommands.Search(searchString); 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 //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<ILibraryBookEntry>().Where(lbe => lbe.Parent is not null).Select(lbe => lbe.Parent).Distinct();
return booksFilteredIn.Concat(seriesFilteredIn).ToHashSet(); return booksFilteredIn.Concat(seriesFilteredIn).ToHashSet();
} }

View File

@ -97,6 +97,9 @@ namespace LibationWinForms.GridView
/// </summary> /// </summary>
private void refreshEntries() private void refreshEntries()
{ {
var priorState = RaiseListChangedEvents;
RaiseListChangedEvents = false;
if (FilteredInGridEntries is null) if (FilteredInGridEntries is null)
{ {
addRemovedItemsBack(FilterRemoved.ToList()); addRemovedItemsBack(FilterRemoved.ToList());
@ -117,7 +120,8 @@ namespace LibationWinForms.GridView
SortInternal(); SortInternal();
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); ResetList();
RaiseListChangedEvents = priorState;
void addRemovedItemsBack(List<IGridEntry> addBackEntries) void addRemovedItemsBack(List<IGridEntry> addBackEntries)
{ {
@ -200,7 +204,7 @@ namespace LibationWinForms.GridView
propertyDescriptor = property; propertyDescriptor = property;
isSorted = true; isSorted = true;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); ResetList();
} }
private void SortInternal() private void SortInternal()
@ -224,8 +228,15 @@ namespace LibationWinForms.GridView
isSorted = false; isSorted = false;
propertyDescriptor = base.SortPropertyCore; propertyDescriptor = base.SortPropertyCore;
Comparer.SortOrder = base.SortDirectionCore; Comparer.SortOrder = base.SortDirectionCore;
ResetList();
}
private void ResetList()
{
var priorState = RaiseListChangedEvents;
RaiseListChangedEvents = true;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
RaiseListChangedEvents = priorState;
} }
} }
} }

View File

@ -189,6 +189,9 @@ namespace LibationWinForms.GridView
internal void UpdateGrid(List<LibraryBook> dbBooks) internal void UpdateGrid(List<LibraryBook> dbBooks)
{ {
//First row that is in view in the DataGridView
var topRow = gridEntryDataGridView.Rows.Cast<DataGridViewRow>().FirstOrDefault(r => r.Displayed)?.Index ?? 0;
#region Add new or update existing grid entries #region Add new or update existing grid entries
//Remove filter prior to adding/updating boooks //Remove filter prior to adding/updating boooks
@ -201,6 +204,7 @@ namespace LibationWinForms.GridView
var seriesEntries = bindingList.AllItems().SeriesEntries().ToList(); var seriesEntries = bindingList.AllItems().SeriesEntries().ToList();
var parentedEpisodes = dbBooks.ParentedEpisodes().ToHashSet(); var parentedEpisodes = dbBooks.ParentedEpisodes().ToHashSet();
bindingList.RaiseListChangedEvents = false;
foreach (var libraryBook in dbBooks.OrderBy(e => e.DateAdded)) foreach (var libraryBook in dbBooks.OrderBy(e => e.DateAdded))
{ {
var existingEntry = allEntries.FindByAsin(libraryBook.Book.AudibleProductId); var existingEntry = allEntries.FindByAsin(libraryBook.Book.AudibleProductId);
@ -216,8 +220,11 @@ namespace LibationWinForms.GridView
AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks);
} }
} }
bindingList.RaiseListChangedEvents = true;
//Re-apply filter after adding new/updating existing books to capture any changes //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); Filter(existingFilter);
#endregion #endregion
@ -231,6 +238,8 @@ namespace LibationWinForms.GridView
.ExceptBy(dbBooks.Select(lb => lb.Book.AudibleProductId), ge => ge.AudibleProductId); .ExceptBy(dbBooks.Select(lb => lb.Book.AudibleProductId), ge => ge.AudibleProductId);
RemoveBooks(removedBooks); RemoveBooks(removedBooks);
gridEntryDataGridView.FirstDisplayedScrollingRowIndex = topRow;
} }
public void RemoveBooks(IEnumerable<ILibraryBookEntry> removedBooks) public void RemoveBooks(IEnumerable<ILibraryBookEntry> removedBooks)