From c2c732b2b1851f5c50df522568a0216c4883b667 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Thu, 2 Sep 2021 15:55:12 -0400 Subject: [PATCH] central event for library altered: books added or removed --- ApplicationServices/LibraryCommands.cs | 23 ++++++++++++++----- ...ibraryQueries.cs => LibraryBookQueries.cs} | 2 +- LibationWinForms/Dialogs/RemoveBooksDialog.cs | 8 ++----- LibationWinForms/Form1.cs | 19 +++++++-------- 4 files changed, 28 insertions(+), 24 deletions(-) rename DataLayer/QueryObjects/{LibraryQueries.cs => LibraryBookQueries.cs} (97%) diff --git a/ApplicationServices/LibraryCommands.cs b/ApplicationServices/LibraryCommands.cs index b0ad1de9..724fc937 100644 --- a/ApplicationServices/LibraryCommands.cs +++ b/ApplicationServices/LibraryCommands.cs @@ -81,9 +81,6 @@ namespace ApplicationServices var newCount = await importIntoDbAsync(importItems); Log.Logger.Information($"Import: New count {newCount}"); - await Task.Run(() => SearchEngineCommands.FullReIndex()); - Log.Logger.Information("FullReIndex: success"); - return (totalCount, newCount); } catch (AudibleApi.Authentication.LoginFailedException lfEx) @@ -149,14 +146,18 @@ namespace ApplicationServices using var context = DbContexts.GetContext(); var libraryImporter = new LibraryImporter(context); var newCount = await Task.Run(() => libraryImporter.Import(importItems)); - context.SaveChanges(); + var qtyChanges = context.SaveChanges(); + + if (qtyChanges > 0) + await Task.Run(() => finalizeLibrarySizeChange()); return newCount; } #endregion #region remove books - public static List RemoveBooks(List idsToRemove) + public static Task> RemoveBooksAsync(List idsToRemove) => Task.Run(() => removeBooks(idsToRemove)); + private static List removeBooks(List idsToRemove) { using var context = DbContexts.GetContext(); var libBooks = context.GetLibrary_Flat_NoTracking(); @@ -166,12 +167,22 @@ namespace ApplicationServices var qtyChanges = context.SaveChanges(); if (qtyChanges > 0) - SearchEngineCommands.FullReIndex(); + finalizeLibrarySizeChange(); return removeLibraryBooks; } #endregion + // call this whenever books are added or removed from library + private static void finalizeLibrarySizeChange() + { + SearchEngineCommands.FullReIndex(); + LibrarySizeChanged?.Invoke(null, null); + } + + /// Occurs when books are added or removed from library + public static event EventHandler LibrarySizeChanged; + /// /// Occurs when , , or /// changed values are successfully persisted. diff --git a/DataLayer/QueryObjects/LibraryQueries.cs b/DataLayer/QueryObjects/LibraryBookQueries.cs similarity index 97% rename from DataLayer/QueryObjects/LibraryQueries.cs rename to DataLayer/QueryObjects/LibraryBookQueries.cs index 573c282d..41016e52 100644 --- a/DataLayer/QueryObjects/LibraryQueries.cs +++ b/DataLayer/QueryObjects/LibraryBookQueries.cs @@ -6,7 +6,7 @@ namespace DataLayer { // only library importing should use tracking. All else should be NoTracking. // only library importing should directly query Book. All else should use LibraryBook - public static class LibraryQueries + public static class LibraryBookQueries { //// tracking is a bad idea for main grid. it prevents anything else from updating entities unless getting them from the grid //public static List GetLibrary_Flat_WithTracking(this LibationContext context) diff --git a/LibationWinForms/Dialogs/RemoveBooksDialog.cs b/LibationWinForms/Dialogs/RemoveBooksDialog.cs index e22bb42a..139612c5 100644 --- a/LibationWinForms/Dialogs/RemoveBooksDialog.cs +++ b/LibationWinForms/Dialogs/RemoveBooksDialog.cs @@ -15,8 +15,6 @@ namespace LibationWinForms.Dialogs { public partial class RemoveBooksDialog : Form { - public bool BooksRemoved { get; private set; } - private Account[] _accounts { get; } private readonly List _libraryBooks; private readonly SortableBindingList _removableGridEntries; @@ -83,7 +81,7 @@ namespace LibationWinForms.Dialogs } } - private void btnRemoveBooks_Click(object sender, EventArgs e) + private async void btnRemoveBooks_Click(object sender, EventArgs e) { var selectedBooks = SelectedEntries.ToList(); @@ -105,13 +103,11 @@ namespace LibationWinForms.Dialogs if (result == DialogResult.Yes) { var idsToRemove = selectedBooks.Select(rge => rge.AudibleProductId).ToList(); - var removeLibraryBooks = LibraryCommands.RemoveBooks(idsToRemove); + var removeLibraryBooks = await LibraryCommands.RemoveBooksAsync(idsToRemove); foreach (var rEntry in selectedBooks) _removableGridEntries.Remove(rEntry); - BooksRemoved = removeLibraryBooks.Count > 0; - UpdateSelection(); } } diff --git a/LibationWinForms/Form1.cs b/LibationWinForms/Form1.cs index 85b78062..deeebcae 100644 --- a/LibationWinForms/Form1.cs +++ b/LibationWinForms/Form1.cs @@ -30,8 +30,9 @@ namespace LibationWinForms return; // independent UI updates - this.Load += (_, __) => RestoreSizeAndLocation(); - this.Load += (_, __) => RefreshImportMenu(); + this.Load += restoreSizeAndLocation; + this.Load += RefreshImportMenu; + LibraryCommands.LibrarySizeChanged += reloadGridAndUpdateBottomNumbers; LibraryCommands.BookUserDefinedItemCommitted += setBackupCounts; var format = System.Drawing.Imaging.ImageFormat.Jpeg; @@ -45,6 +46,8 @@ namespace LibationWinForms if (this.DesignMode) return; + // can't refactor into "this.Load => reloadGridAndUpdateBottomNumbers" + // because loadInitialQuickFilterState must follow it reloadGridAndUpdateBottomNumbers(); // also applies filter. ONLY call AFTER loading grid @@ -56,7 +59,7 @@ namespace LibationWinForms SaveSizeAndLocation(); } - private void RestoreSizeAndLocation() + private void restoreSizeAndLocation(object _ = null, object __ = null) { var config = Configuration.Instance; @@ -126,7 +129,7 @@ namespace LibationWinForms config.MainFormIsMaximized = this.WindowState == FormWindowState.Maximized; } - private void reloadGridAndUpdateBottomNumbers() + private void reloadGridAndUpdateBottomNumbers(object _ = null, object __ = null) { // suppressed filter while init'ing UI var prev_isProcessingGridSelect = isProcessingGridSelect; @@ -303,7 +306,7 @@ namespace LibationWinForms #endregion #region Import menu - public void RefreshImportMenu() + public void RefreshImportMenu(object _ = null, EventArgs __ = null) { using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); var count = persister.AccountsSettings.Accounts.Count; @@ -393,9 +396,6 @@ namespace LibationWinForms { using var dialog = new RemoveBooksDialog(accounts); dialog.ShowDialog(); - - if (dialog.BooksRemoved) - reloadGridAndUpdateBottomNumbers(); } private void scanLibraries(IEnumerable accounts) => scanLibraries(accounts.ToArray()); @@ -408,9 +408,6 @@ namespace LibationWinForms var newAdded = dialog.NewBooksAdded; MessageBox.Show($"Total processed: {totalProcessed}\r\nNew: {newAdded}"); - - if (totalProcessed > 0) - reloadGridAndUpdateBottomNumbers(); } #endregion