central event for library altered: books added or removed

This commit is contained in:
Robert McRackan 2021-09-02 15:55:12 -04:00
parent 9e0caf34d6
commit c2c732b2b1
4 changed files with 28 additions and 24 deletions

View File

@ -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<LibraryBook> RemoveBooks(List<string> idsToRemove)
public static Task<List<LibraryBook>> RemoveBooksAsync(List<string> idsToRemove) => Task.Run(() => removeBooks(idsToRemove));
private static List<LibraryBook> removeBooks(List<string> 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);
}
/// <summary>Occurs when books are added or removed from library</summary>
public static event EventHandler LibrarySizeChanged;
/// <summary>
/// Occurs when <see cref="UserDefinedItem.Tags"/>, <see cref="UserDefinedItem.BookStatus"/>, or <see cref="UserDefinedItem.PdfStatus"/>
/// changed values are successfully persisted.

View File

@ -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<LibraryBook> GetLibrary_Flat_WithTracking(this LibationContext context)

View File

@ -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<LibraryBook> _libraryBooks;
private readonly SortableBindingList<RemovableGridEntry> _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();
}
}

View File

@ -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<Account> 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