diff --git a/Source/AppScaffolding/LibationScaffolding.cs b/Source/AppScaffolding/LibationScaffolding.cs index 229cdce4..9a0b385d 100644 --- a/Source/AppScaffolding/LibationScaffolding.cs +++ b/Source/AppScaffolding/LibationScaffolding.cs @@ -279,8 +279,11 @@ namespace AppScaffolding private static void wireUpSystemEvents(Configuration configuration) { - LibraryCommands.LibrarySizeChanged += (_, __) => SearchEngineCommands.FullReIndex(); - LibraryCommands.BookUserDefinedItemCommitted += (_, books) => SearchEngineCommands.UpdateBooks(books); + LibraryCommands.LibrarySizeChanged += (object _, List libraryBooks) + => SearchEngineCommands.FullReIndex(libraryBooks); + + LibraryCommands.BookUserDefinedItemCommitted += (_, books) + => SearchEngineCommands.UpdateBooks(books); } public static UpgradeProperties GetLatestRelease() diff --git a/Source/ApplicationServices/LibraryCommands.cs b/Source/ApplicationServices/LibraryCommands.cs index 71ed011e..5aa60340 100644 --- a/Source/ApplicationServices/LibraryCommands.cs +++ b/Source/ApplicationServices/LibraryCommands.cs @@ -445,10 +445,14 @@ namespace ApplicationServices #endregion // call this whenever books are added or removed from library - private static void finalizeLibrarySizeChange() => LibrarySizeChanged?.Invoke(null, null); + private static void finalizeLibrarySizeChange() + { + var library = DbContexts.GetLibrary_Flat_NoTracking(includeParents: true); + LibrarySizeChanged?.Invoke(null, library); + } /// Occurs when the size of the library changes. ie: books are added or removed - public static event EventHandler LibrarySizeChanged; + public static event EventHandler> LibrarySizeChanged; /// /// Occurs when the size of the library does not change but book(s) details do. Especially when , , or changed values are successfully persisted. @@ -600,7 +604,8 @@ namespace ApplicationServices var results = libraryBooks .AsParallel() - .Select(lb => new { absent = lb.AbsentFromLastScan, status = Liberated_Status(lb.Book) }) + .WithoutParents() + .Select(lb => new { absent = lb.AbsentFromLastScan, status = Liberated_Status(lb.Book) }) .ToList(); var booksFullyBackedUp = results.Count(r => r.status == LiberatedStatus.Liberated); diff --git a/Source/ApplicationServices/SearchEngineCommands.cs b/Source/ApplicationServices/SearchEngineCommands.cs index 377248d5..7b292e44 100644 --- a/Source/ApplicationServices/SearchEngineCommands.cs +++ b/Source/ApplicationServices/SearchEngineCommands.cs @@ -48,6 +48,8 @@ namespace ApplicationServices } public static void FullReIndex() => performSafeCommand(fullReIndex); + public static void FullReIndex(List libraryBooks) + => performSafeCommand(se => fullReIndex(se, libraryBooks.WithoutParents())); internal static void UpdateUserDefinedItems(LibraryBook book) => performSafeCommand(e => { @@ -94,8 +96,11 @@ namespace ApplicationServices private static void fullReIndex(SearchEngine engine) { var library = DbContexts.GetLibrary_Flat_NoTracking(); - engine.CreateNewIndex(library); + fullReIndex(engine, library); } + + private static void fullReIndex(SearchEngine engine, IEnumerable libraryBooks) + => engine.CreateNewIndex(libraryBooks); #endregion } } diff --git a/Source/DataLayer/QueryObjects/BookQueries.cs b/Source/DataLayer/QueryObjects/BookQueries.cs index 4a3c9171..81cdc162 100644 --- a/Source/DataLayer/QueryObjects/BookQueries.cs +++ b/Source/DataLayer/QueryObjects/BookQueries.cs @@ -44,7 +44,11 @@ namespace DataLayer public static bool IsEpisodeParent(this Book book) => book.ContentType is ContentType.Parent; - public static bool HasLiberated(this Book book) + + public static IEnumerable WithoutParents(this IEnumerable libraryBooks) + => libraryBooks.Where(lb => !lb.Book.IsEpisodeParent()); + + public static bool HasLiberated(this Book book) => book.UserDefinedItem.BookStatus is LiberatedStatus.Liberated || book.UserDefinedItem.PdfStatus is not null and LiberatedStatus.Liberated; } diff --git a/Source/LibationAvalonia/ViewModels/MainVM.BackupCounts.cs b/Source/LibationAvalonia/ViewModels/MainVM.BackupCounts.cs index b0a103ea..658ef683 100644 --- a/Source/LibationAvalonia/ViewModels/MainVM.BackupCounts.cs +++ b/Source/LibationAvalonia/ViewModels/MainVM.BackupCounts.cs @@ -44,16 +44,18 @@ namespace LibationAvalonia.ViewModels private void Configure_BackupCounts() { - MainWindow.LibraryLoaded += (_, e) => setBackupCounts(e.Where(l => !l.Book.IsEpisodeParent())); - LibraryCommands.LibrarySizeChanged += (_,_) => setBackupCounts(); - LibraryCommands.BookUserDefinedItemCommitted += (_, _) => setBackupCounts(); + LibraryCommands.LibrarySizeChanged += async (object _, List libraryBooks) + => await SetBackupCountsAsync(libraryBooks); + + //Pass null to the setup count to get the whole library. + LibraryCommands.BookUserDefinedItemCommitted += async (_, _) + => await SetBackupCountsAsync(null); } - private async void setBackupCounts(IEnumerable libraryBooks = null) + public async Task SetBackupCountsAsync(IEnumerable libraryBooks) { if (updateCountsTask?.IsCompleted ?? true) { - libraryBooks ??= DbContexts.GetLibrary_Flat_NoTracking(); updateCountsTask = Task.Run(() => LibraryCommands.GetCounts(libraryBooks)); var stats = await updateCountsTask; await Dispatcher.UIThread.InvokeAsync(() => LibraryStats = stats); diff --git a/Source/LibationAvalonia/ViewModels/MainVM.cs b/Source/LibationAvalonia/ViewModels/MainVM.cs index b5f102bd..027f13d9 100644 --- a/Source/LibationAvalonia/ViewModels/MainVM.cs +++ b/Source/LibationAvalonia/ViewModels/MainVM.cs @@ -1,7 +1,9 @@ using ApplicationServices; +using DataLayer; using LibationAvalonia.Views; using LibationFileManager; using ReactiveUI; +using System.Collections.Generic; namespace LibationAvalonia.ViewModels { @@ -34,9 +36,8 @@ namespace LibationAvalonia.ViewModels Configure_VisibleBooks(); } - private async void LibraryCommands_LibrarySizeChanged(object sender, System.EventArgs e) + private async void LibraryCommands_LibrarySizeChanged(object sender, List fullLibrary) { - var fullLibrary = await System.Threading.Tasks.Task.Run(() => DbContexts.GetLibrary_Flat_NoTracking(includeParents: true)); await ProductsDisplay.UpdateGridAsync(fullLibrary); } diff --git a/Source/LibationAvalonia/Views/MainWindow.axaml.cs b/Source/LibationAvalonia/Views/MainWindow.axaml.cs index 1850c857..f9687d2a 100644 --- a/Source/LibationAvalonia/Views/MainWindow.axaml.cs +++ b/Source/LibationAvalonia/Views/MainWindow.axaml.cs @@ -13,7 +13,6 @@ namespace LibationAvalonia.Views { public partial class MainWindow : ReactiveWindow { - public event EventHandler> LibraryLoaded; public MainWindow() { DataContext = new MainVM(this); @@ -66,6 +65,7 @@ namespace LibationAvalonia.Views if (QuickFilters.UseDefault) await ViewModel.PerformFilter(QuickFilters.Filters.FirstOrDefault()); + await ViewModel.SetBackupCountsAsync(initialLibrary); await ViewModel.ProductsDisplay.BindToGridAsync(initialLibrary); } diff --git a/Source/LibationWinForms/Form1.BackupCounts.cs b/Source/LibationWinForms/Form1.BackupCounts.cs index 941cedf4..8c8ee876 100644 --- a/Source/LibationWinForms/Form1.BackupCounts.cs +++ b/Source/LibationWinForms/Form1.BackupCounts.cs @@ -17,7 +17,9 @@ namespace LibationWinForms beginPdfBackupsToolStripMenuItem.Format(0); LibraryCommands.LibrarySizeChanged += setBackupCounts; - LibraryCommands.BookUserDefinedItemCommitted += setBackupCounts; + //Pass null to the runner to get the whole library. + LibraryCommands.BookUserDefinedItemCommitted += (_, _) + => setBackupCounts(null, null); updateCountsBw.DoWork += UpdateCountsBw_DoWork; updateCountsBw.RunWorkerCompleted += exportMenuEnable; @@ -28,12 +30,12 @@ namespace LibationWinForms private bool runBackupCountsAgain; - private void setBackupCounts(object _, object __) + private void setBackupCounts(object _, List libraryBooks) { runBackupCountsAgain = true; if (!updateCountsBw.IsBusy) - updateCountsBw.RunWorkerAsync(); + updateCountsBw.RunWorkerAsync(libraryBooks); } private void UpdateCountsBw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) @@ -41,11 +43,7 @@ namespace LibationWinForms while (runBackupCountsAgain) { runBackupCountsAgain = false; - - if (e.Argument is not IEnumerable lbs) - lbs = DbContexts.GetLibrary_Flat_NoTracking(); - - e.Result = LibraryCommands.GetCounts(lbs); + e.Result = LibraryCommands.GetCounts(e.Argument as IEnumerable); } } diff --git a/Source/LibationWinForms/Form1.cs b/Source/LibationWinForms/Form1.cs index b7031136..50232c5c 100644 --- a/Source/LibationWinForms/Form1.cs +++ b/Source/LibationWinForms/Form1.cs @@ -52,7 +52,8 @@ namespace LibationWinForms // Configure_Grid(); // since it's just this, can keep here. If it needs more, then give grid it's own 'partial class Form1' { - LibraryCommands.LibrarySizeChanged += (_, __) => Invoke(() => productsDisplay.DisplayAsync()); + LibraryCommands.LibrarySizeChanged += (object _, List fullLibrary) + => Invoke(() => productsDisplay.DisplayAsync(fullLibrary)); } Shown += Form1_Shown; } @@ -75,7 +76,7 @@ namespace LibationWinForms public async Task InitLibraryAsync(List libraryBooks) { runBackupCountsAgain = true; - updateCountsBw.RunWorkerAsync(libraryBooks.Where(b => !b.Book.IsEpisodeParent())); + updateCountsBw.RunWorkerAsync(libraryBooks); await productsDisplay.DisplayAsync(libraryBooks); }