Pull non-presentation logic out of main form

This commit is contained in:
Robert McRackan 2021-07-28 17:20:16 -04:00
parent fb3126b0c6
commit 53a46b5dfc
2 changed files with 59 additions and 52 deletions

View File

@ -6,6 +6,7 @@ using AudibleApi;
using DataLayer; using DataLayer;
using Dinah.Core; using Dinah.Core;
using DtoImporterService; using DtoImporterService;
using FileManager;
using InternalUtilities; using InternalUtilities;
using Serilog; using Serilog;
@ -13,6 +14,7 @@ namespace ApplicationServices
{ {
public static class LibraryCommands public static class LibraryCommands
{ {
#region FULL LIBRARY scan and import
public static async Task<(int totalCount, int newCount)> ImportAccountAsync(Func<Account, ILoginCallback> loginCallbackFactoryFunc, params Account[] accounts) public static async Task<(int totalCount, int newCount)> ImportAccountAsync(Func<Account, ILoginCallback> loginCallbackFactoryFunc, params Account[] accounts)
{ {
if (accounts is null || accounts.Length == 0) if (accounts is null || accounts.Length == 0)
@ -100,7 +102,9 @@ namespace ApplicationServices
return newCount; return newCount;
} }
#endregion
#region Update book details
public static int UpdateTags(Book book, string newTags) public static int UpdateTags(Book book, string newTags)
{ {
try try
@ -172,5 +176,40 @@ namespace ApplicationServices
throw; throw;
} }
} }
#endregion
// this is a query, not command so maybe I should make a LibraryQueries. except there's already one of those...
private enum AudioFileState { full, aax, none }
private static AudioFileState getAudioFileState(string productId)
=> AudibleFileStorage.Audio.Exists(productId) ? AudioFileState.full
: AudibleFileStorage.AAXC.Exists(productId) ? AudioFileState.aax
: AudioFileState.none;
public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int pdfsDownloaded, int pdfsNotDownloaded) { }
public static LibraryStats GetCounts()
{
var libraryBooks = DbContexts.GetContext().GetLibrary_Flat_NoTracking();
var results = libraryBooks
.AsParallel()
.Select(lb => getAudioFileState(lb.Book.AudibleProductId))
.ToList();
var booksFullyBackedUp = results.Count(r => r == AudioFileState.full);
var booksDownloadedOnly = results.Count(r => r == AudioFileState.aax);
var booksNoProgress = results.Count(r => r == AudioFileState.none);
Log.Logger.Information("Book counts. {@DebugInfo}", new { total = results.Count, booksFullyBackedUp, booksDownloadedOnly, booksNoProgress });
var boolResults = libraryBooks
.AsParallel()
.Where(lb => lb.Book.Supplements.Any())
.Select(lb => AudibleFileStorage.PDF.Exists(lb.Book.AudibleProductId))
.ToList();
var pdfsDownloaded = boolResults.Count(r => r);
var pdfsNotDownloaded = boolResults.Count(r => !r);
Log.Logger.Information("PDF counts. {@DebugInfo}", new { total = boolResults.Count, pdfsDownloaded, pdfsNotDownloaded });
return new(booksFullyBackedUp, booksDownloadedOnly, booksNoProgress, pdfsDownloaded, pdfsNotDownloaded);
}
} }
} }

View File

@ -191,45 +191,24 @@ namespace LibationWinForms
#region bottom: backup counts #region bottom: backup counts
private async void setBackupCountsAsync(object _, object __) private async void setBackupCountsAsync(object _, object __)
{ {
await Task.Run(() => { LibraryCommands.LibraryStats libraryStats = null;
var books = DbContexts.GetContext() await Task.Run(() => libraryStats = LibraryCommands.GetCounts());
.GetLibrary_Flat_NoTracking()
.Select(sp => sp.Book)
.ToList();
setBookBackupCounts(books); setBookBackupCounts(libraryStats.booksFullyBackedUp, libraryStats.booksDownloadedOnly, libraryStats.booksNoProgress);
setPdfBackupCounts(books); setPdfBackupCounts(libraryStats.pdfsDownloaded, libraryStats.pdfsNotDownloaded);
}); }
} private void setBookBackupCounts(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress)
enum AudioFileState { full, aax, none }
private void setBookBackupCounts(IEnumerable<Book> books)
{ {
static AudioFileState getAudioFileState(string productId)
{
if (AudibleFileStorage.Audio.Exists(productId))
return AudioFileState.full;
if (AudibleFileStorage.AAXC.Exists(productId))
return AudioFileState.aax;
return AudioFileState.none;
}
var results = books
.AsParallel()
.Select(b => getAudioFileState(b.AudibleProductId))
.ToList();
var fullyBackedUp = results.Count(r => r == AudioFileState.full);
var downloadedOnly = results.Count(r => r == AudioFileState.aax);
var noProgress = results.Count(r => r == AudioFileState.none);
// enable/disable export // enable/disable export
exportLibraryToolStripMenuItem.Enabled = results.Any(); var hasResults = 0 < (booksFullyBackedUp + booksDownloadedOnly + booksNoProgress);
exportLibraryToolStripMenuItem.Enabled = hasResults;
// update bottom numbers // update bottom numbers
var pending = noProgress + downloadedOnly; var pending = booksNoProgress + booksDownloadedOnly;
var statusStripText var statusStripText
= !results.Any() ? "No books. Begin by importing your library" = !hasResults ? "No books. Begin by importing your library"
: pending > 0 ? string.Format(backupsCountsLbl_Format, noProgress, downloadedOnly, fullyBackedUp) : pending > 0 ? string.Format(backupsCountsLbl_Format, booksNoProgress, booksDownloadedOnly, booksFullyBackedUp)
: $"All {"book".PluralizeWithCount(fullyBackedUp)} backed up"; : $"All {"book".PluralizeWithCount(booksFullyBackedUp)} backed up";
// update menu item // update menu item
var menuItemText var menuItemText
@ -237,40 +216,29 @@ namespace LibationWinForms
? $"{pending} remaining" ? $"{pending} remaining"
: "All books have been liberated"; : "All books have been liberated";
Serilog.Log.Logger.Information("Book counts. {@DebugInfo}", new { fullyBackedUp, downloadedOnly, noProgress, pending, statusStripText, menuItemText });
// update UI // update UI
statusStrip1.UIThread(() => backupsCountsLbl.Text = statusStripText); statusStrip1.UIThread(() => backupsCountsLbl.Text = statusStripText);
menuStrip1.UIThread(() => beginBookBackupsToolStripMenuItem.Enabled = pending > 0); menuStrip1.UIThread(() => beginBookBackupsToolStripMenuItem.Enabled = pending > 0);
menuStrip1.UIThread(() => beginBookBackupsToolStripMenuItem.Text = string.Format(beginBookBackupsToolStripMenuItem_format, menuItemText)); menuStrip1.UIThread(() => beginBookBackupsToolStripMenuItem.Text = string.Format(beginBookBackupsToolStripMenuItem_format, menuItemText));
} }
private void setPdfBackupCounts(IEnumerable<Book> books) private void setPdfBackupCounts(int pdfsDownloaded, int pdfsNotDownloaded)
{ {
var boolResults = books
.AsParallel()
.Where(b => b.Supplements.Any())
.Select(b => AudibleFileStorage.PDF.Exists(b.AudibleProductId))
.ToList();
var downloaded = boolResults.Count(r => r);
var notDownloaded = boolResults.Count(r => !r);
// update bottom numbers // update bottom numbers
var hasResults = 0 < (pdfsNotDownloaded + pdfsDownloaded);
var statusStripText var statusStripText
= !boolResults.Any() ? "" = !hasResults ? ""
: notDownloaded > 0 ? string.Format(pdfsCountsLbl_Format, notDownloaded, downloaded) : pdfsNotDownloaded > 0 ? string.Format(pdfsCountsLbl_Format, pdfsNotDownloaded, pdfsDownloaded)
: $"| All {downloaded} PDFs downloaded"; : $"| All {pdfsDownloaded} PDFs downloaded";
// update menu item // update menu item
var menuItemText var menuItemText
= notDownloaded > 0 = pdfsNotDownloaded > 0
? $"{notDownloaded} remaining" ? $"{pdfsNotDownloaded} remaining"
: "All PDFs have been downloaded"; : "All PDFs have been downloaded";
Serilog.Log.Logger.Information("PDF counts. {@DebugInfo}", new { downloaded, notDownloaded, statusStripText, menuItemText });
// update UI // update UI
statusStrip1.UIThread(() => pdfsCountsLbl.Text = statusStripText); statusStrip1.UIThread(() => pdfsCountsLbl.Text = statusStripText);
menuStrip1.UIThread(() => beginPdfBackupsToolStripMenuItem.Enabled = notDownloaded > 0); menuStrip1.UIThread(() => beginPdfBackupsToolStripMenuItem.Enabled = pdfsNotDownloaded > 0);
menuStrip1.UIThread(() => beginPdfBackupsToolStripMenuItem.Text = string.Format(beginPdfBackupsToolStripMenuItem_format, menuItemText)); menuStrip1.UIThread(() => beginPdfBackupsToolStripMenuItem.Text = string.Format(beginPdfBackupsToolStripMenuItem_format, menuItemText));
} }
#endregion #endregion