From 18cf20ecad0ec2952273d29c2cd128c2cd3d9ee8 Mon Sep 17 00:00:00 2001 From: MBucari Date: Fri, 17 Mar 2023 19:31:36 -0600 Subject: [PATCH] All books that pass the filter are counted as "visible" (#536) --- .../ViewModels/ProductsDisplayViewModel.cs | 21 +++++++++++++------ .../GridView/GridEntryBindingList.cs | 16 ++++++++++++-- .../LibationWinForms/GridView/ProductsGrid.cs | 12 +++++------ 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs b/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs index 452d0848..604b37c3 100644 --- a/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs +++ b/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs @@ -5,7 +5,6 @@ using Avalonia.Threading; using DataLayer; using LibationAvalonia.Dialogs.Login; using LibationUiBase.GridView; -using NPOI.SS.Formula.Functions; using ReactiveUI; using System; using System.Collections.Generic; @@ -32,10 +31,14 @@ namespace LibationAvalonia.ViewModels public bool RemoveColumnVisivle { get => _removeColumnVisivle; private set => this.RaiseAndSetIfChanged(ref _removeColumnVisivle, value); } public List GetVisibleBookEntries() - => GridEntries - .OfType() - .Select(lbe => lbe.LibraryBook) - .ToList(); + => FilteredInGridEntries? + .OfType() + .Select(lbe => lbe.LibraryBook) + .ToList() + ?? SOURCE + .OfType() + .Select(lbe => lbe.LibraryBook) + .ToList(); private IEnumerable GetAllBookEntries() => SOURCE @@ -122,7 +125,13 @@ namespace LibationAvalonia.ViewModels } private void GridEntries_CollectionChanged(object sender = null, EventArgs e = null) - => VisibleCountChanged?.Invoke(this, GridEntries.OfType().Count()); + { + var count + = FilteredInGridEntries?.OfType().Count() + ?? SOURCE.OfType().Count(); + + VisibleCountChanged?.Invoke(this, count); + } /// /// Call when there's been a change to the library diff --git a/Source/LibationWinForms/GridView/GridEntryBindingList.cs b/Source/LibationWinForms/GridView/GridEntryBindingList.cs index 7350334b..9203ad53 100644 --- a/Source/LibationWinForms/GridView/GridEntryBindingList.cs +++ b/Source/LibationWinForms/GridView/GridEntryBindingList.cs @@ -28,6 +28,18 @@ namespace LibationWinForms.GridView /// All items in the list, including those filtered out. public List AllItems() => Items.Concat(FilterRemoved).ToList(); + + /// All items that pass the current filter + public IEnumerable GetFilteredInItems() + => SearchResults is null + ? FilterRemoved + .OfType() + .Union(Items.OfType()) + : FilterRemoved + .OfType() + .Join(SearchResults.Docs, o => o.Book.AudibleProductId, i => i.ProductId, (o, _) => o) + .Union(Items.OfType()); + public bool SupportsFiltering => true; public string Filter { get => FilterString; set => ApplyFilter(value); } @@ -102,7 +114,7 @@ namespace LibationWinForms.GridView public void CollapseItem(ISeriesEntry sEntry) { - foreach (var episode in Items.BookEntries().Where(b => b.Parent == sEntry).ToList()) + foreach (var episode in sEntry.Children.Join(Items.BookEntries(), o => o, i => i, (_, i) => i).ToList()) { FilterRemoved.Add(episode); base.Remove(episode); @@ -115,7 +127,7 @@ namespace LibationWinForms.GridView { var sindex = Items.IndexOf(sEntry); - foreach (var episode in FilterRemoved.BookEntries().Where(b => b.Parent == sEntry).ToList()) + foreach (var episode in sEntry.Children.Join(FilterRemoved.BookEntries(), o => o, i => i, (_, i) => i).ToList()) { if (SearchResults is null || SearchResults.Docs.Any(d => d.ProductId == episode.AudibleProductId)) { diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index c5d59792..b7b8815f 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -7,7 +7,6 @@ using LibationWinForms.Dialogs; using System; using System.Collections.Generic; using System.Data; -using System.Diagnostics; using System.Drawing; using System.Linq; using System.Threading.Tasks; @@ -34,7 +33,7 @@ namespace LibationWinForms.GridView private GridEntryBindingList bindingList; internal IEnumerable GetVisibleBooks() => bindingList - .BookEntries() + .GetFilteredInItems() .Select(lbe => lbe.LibraryBook); internal IEnumerable GetAllBookEntries() => bindingList.AllItems().BookEntries(); @@ -84,7 +83,7 @@ namespace LibationWinForms.GridView else bindingList.ExpandItem(sEntry); - VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); + VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count()); } else if (e.ColumnIndex == descriptionGVColumn.Index) DescriptionClicked?.Invoke(sEntry, gridEntryDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false)); @@ -248,7 +247,7 @@ namespace LibationWinForms.GridView bindingList = new GridEntryBindingList(geList.OrderByDescending(e => e.DateAdded)); bindingList.CollapseAll(); syncBindingSource.DataSource = bindingList; - VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); + VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count()); } internal void UpdateGrid(List dbBooks) @@ -317,7 +316,7 @@ namespace LibationWinForms.GridView //no need to re-filter for removed books bindingList.Remove(removed); - VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); + VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count()); } private void AddOrUpdateBook(LibraryBook book, ILibraryBookEntry existingBookEntry) @@ -364,6 +363,7 @@ namespace LibationWinForms.GridView //Series exists. Create and add episode child then update the SeriesEntry episodeEntry = new LibraryBookEntry(episodeBook, seriesEntry); seriesEntry.Children.Add(episodeEntry); + seriesEntry.Children.Sort((c1,c2) => c1.SeriesIndex.CompareTo(c2.SeriesIndex)); var seriesBook = dbBooks.Single(lb => lb.Book.AudibleProductId == seriesEntry.LibraryBook.Book.AudibleProductId); seriesEntry.UpdateLibraryBook(seriesBook); } @@ -395,7 +395,7 @@ namespace LibationWinForms.GridView syncBindingSource.Filter = searchString; if (visibleCount != bindingList.Count) - VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); + VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count()); } #endregion