mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-11-30 10:02:53 +01:00
Two states were wrong, both when the live library is empty but the trash is not. GetLibrary() filters IsDeleted, so an all-trashed library reads as empty and the getting-started panel took over. Without a filter it said 'Libation is empty' while 29 books sat in the trash - the books are not gone, they are one click away. The panel now carries a trash line when there is anything in there. With a filter it was worse: searching for a book that IS in the trash showed 'Libation is empty. Add your Audible account' and suppressed the trash hint entirely, which is the exact failure #1925 was about, reintroduced in the one case where the library is empty. The rule is now that someone searching is not someone getting started, so a filter always answers for itself. That leaves one merely-suboptimal case, an empty library filtered to something in neither place, which says 'no books match' - true, and better than claiming the library is empty when it is being filtered. Also fixes what made this invisible while testing: GettingStartedVisible depends on the filter but nothing raised it when the filter changed, so the panel never re-evaluated. Co-authored-by: rmcrackan <rmcrackan@gmail.com>
150 lines
5.5 KiB
C#
150 lines
5.5 KiB
C#
using ApplicationServices;
|
|
using DataLayer;
|
|
using Dinah.Core.Threading;
|
|
using System.Collections.Generic;
|
|
|
|
namespace LibationWinForms;
|
|
|
|
public partial class Form1
|
|
{
|
|
private readonly System.ComponentModel.BackgroundWorker updateCountsBw = new();
|
|
|
|
protected void Configure_BackupCounts()
|
|
{
|
|
// init formattable
|
|
beginBookBackupsToolStripMenuItem.Format(0);
|
|
beginPdfBackupsToolStripMenuItem.Format(0);
|
|
|
|
LibraryCommands.LibrarySizeChanged += setBackupCounts;
|
|
LibraryCommands.LibrarySizeChanged += (_, _) => refreshBooksInTrash();
|
|
//Pass null to the runner to get the whole library.
|
|
LibraryCommands.BookUserDefinedItemCommitted += (_, _)
|
|
=> setBackupCounts(null, null);
|
|
|
|
trashBinLbl.Text = "";
|
|
trashBinLbl.Visible = false;
|
|
trashBinLbl.ToolTipText = LibationUiBase.TrashBinUi.StatusToolTip;
|
|
refreshBooksInTrash();
|
|
|
|
updateCountsBw.DoWork += UpdateCountsBw_DoWork;
|
|
// Register the error logger first so a failed count is logged exactly once, before the
|
|
// display handlers below run and (safely) treat the stats as unavailable.
|
|
updateCountsBw.RunWorkerCompleted += logBackupCountsError;
|
|
updateCountsBw.RunWorkerCompleted += exportMenuEnable;
|
|
updateCountsBw.RunWorkerCompleted += updateBottomStats;
|
|
updateCountsBw.RunWorkerCompleted += update_BeginBookBackups_menuItem;
|
|
updateCountsBw.RunWorkerCompleted += udpate_BeginPdfOnlyBackups_menuItem;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Re-read the trash count and show it only when there is something in there. Kept off
|
|
/// <see cref="LibraryCommands.GetCounts"/> on purpose: that also runs against the visible subset on
|
|
/// every filter change, where a database round trip would be wasted work.
|
|
/// </summary>
|
|
private async void refreshBooksInTrash()
|
|
{
|
|
int booksInTrash;
|
|
try
|
|
{
|
|
booksInTrash = await System.Threading.Tasks.Task.Run(DbContexts.GetTrashedBookCount);
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
//A stale count must not take down the window that displays it.
|
|
Serilog.Log.Logger.Error(ex, "Failed to count books in the trash");
|
|
return;
|
|
}
|
|
|
|
statusStrip1.UIThreadAsync(() =>
|
|
{
|
|
trashBinLbl.Text = LibationUiBase.TrashBinUi.StatusText(booksInTrash);
|
|
trashBinLbl.Visible = LibationUiBase.TrashBinUi.ShowStatus(booksInTrash);
|
|
openTrashBinToolStripMenuItem.Text = LibationUiBase.TrashBinUi.MenuText(booksInTrash);
|
|
setBooksInTrash(booksInTrash);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Safely extract the completed <see cref="LibraryCommands.LibraryStats"/>. Returns null when the
|
|
/// background count was cancelled or faulted. Reading <see cref="System.ComponentModel.RunWorkerCompletedEventArgs.Result"/>
|
|
/// directly rethrows any <see cref="System.ComponentModel.RunWorkerCompletedEventArgs.Error"/>, which would otherwise
|
|
/// crash the app; callers must degrade gracefully on null.
|
|
/// </summary>
|
|
private static LibraryCommands.LibraryStats? getLibraryStats(System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
=> e.Cancelled || e.Error is not null ? null : e.Result as LibraryCommands.LibraryStats;
|
|
|
|
private static void logBackupCountsError(object? _, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
if (e.Error is not null)
|
|
Serilog.Log.Logger.Error(e.Error, "Failed to update backup counts");
|
|
}
|
|
|
|
private bool runBackupCountsAgain;
|
|
|
|
private void setBackupCounts(object? _, List<LibraryBook>? libraryBooks)
|
|
{
|
|
runBackupCountsAgain = true;
|
|
|
|
if (!updateCountsBw.IsBusy)
|
|
updateCountsBw.RunWorkerAsync(libraryBooks);
|
|
}
|
|
|
|
private void UpdateCountsBw_DoWork(object? sender, System.ComponentModel.DoWorkEventArgs e)
|
|
{
|
|
while (runBackupCountsAgain)
|
|
{
|
|
runBackupCountsAgain = false;
|
|
e.Result = LibraryCommands.GetCounts(e.Argument as IEnumerable<LibraryBook>);
|
|
}
|
|
}
|
|
|
|
private void exportMenuEnable(object? _, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
var libraryStats = getLibraryStats(e);
|
|
Invoke(() => exportLibraryToolStripMenuItem.Enabled = libraryStats?.HasBookResults is true);
|
|
}
|
|
|
|
private void updateBottomStats(object? _, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
var libraryStats = getLibraryStats(e);
|
|
statusStrip1.UIThreadAsync(() => backupsCountsLbl.Text = libraryStats?.StatusString ?? "ERROR GETTING STATUS");
|
|
|
|
//Whether the library is empty is only known once these counts have been taken, which is also what
|
|
//keeps the getting-started panel off screen while a full library is still loading.
|
|
if (libraryStats is not null)
|
|
this.UIThreadAsync(() => setLibraryIsEmpty(!libraryStats.HasBookResults));
|
|
}
|
|
|
|
// update 'begin book and pdf backups' menu item
|
|
private void update_BeginBookBackups_menuItem(object? _, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
var libraryStats = getLibraryStats(e);
|
|
|
|
var menuItemText
|
|
= libraryStats?.HasPendingBooks is true
|
|
? $"{libraryStats.PendingBooks} remaining"
|
|
: "All books have been liberated";
|
|
menuStrip1.UIThreadAsync(() =>
|
|
{
|
|
beginBookBackupsToolStripMenuItem.Format(menuItemText);
|
|
beginBookBackupsToolStripMenuItem.Enabled = libraryStats?.HasPendingBooks is true;
|
|
});
|
|
}
|
|
|
|
// update 'begin pdf only backups' menu item
|
|
private void udpate_BeginPdfOnlyBackups_menuItem(object? _, System.ComponentModel.RunWorkerCompletedEventArgs e)
|
|
{
|
|
var libraryStats = getLibraryStats(e);
|
|
|
|
var menuItemText
|
|
= libraryStats?.pdfsNotDownloaded > 0
|
|
? $"{libraryStats.pdfsNotDownloaded} remaining"
|
|
: "All PDFs have been downloaded";
|
|
menuStrip1.UIThreadAsync(() =>
|
|
{
|
|
beginPdfBackupsToolStripMenuItem.Format(menuItemText);
|
|
beginPdfBackupsToolStripMenuItem.Enabled = libraryStats?.pdfsNotDownloaded > 0;
|
|
});
|
|
}
|
|
}
|