Remove duplicate logic

This commit is contained in:
Robert McRackan 2021-07-29 11:32:16 -04:00
parent 9e44a95ba2
commit a3542c53e2
4 changed files with 53 additions and 44 deletions

View File

@ -11,6 +11,13 @@ using Serilog;
namespace ApplicationServices namespace ApplicationServices
{ {
// subtly different from DataLayer.LiberatedStatus
// - DataLayer.LiberatedStatus: has no concept of partially downloaded
// - ApplicationServices.LiberatedState: has no concept of Error/skipped
public enum LiberatedState { NotDownloaded, PartialDownload, Liberated }
public enum PdfState { NoPdf, Downloaded, NotDownloaded }
public static class LibraryCommands public static class LibraryCommands
{ {
#region FULL LIBRARY scan and import #region FULL LIBRARY scan and import
@ -177,12 +184,18 @@ namespace ApplicationServices
} }
#endregion #endregion
// this is a query, not command so maybe I should make a LibraryQueries. except there's already one of those... // below are queries, not commands. maybe I should make a LibraryQueries. except there's already one of those...
private enum AudioFileState { full, aax, none }
private static AudioFileState getAudioFileState(Book book) public static LiberatedState Liberated_Status(Book book)
=> TransitionalFileLocator.Audio_Exists(book) ? AudioFileState.full => TransitionalFileLocator.Audio_Exists(book) ? LiberatedState.Liberated
: TransitionalFileLocator.AAXC_Exists(book) ? AudioFileState.aax : TransitionalFileLocator.AAXC_Exists(book) ? LiberatedState.PartialDownload
: AudioFileState.none; : LiberatedState.NotDownloaded;
public static PdfState Pdf_Status(Book book)
=> !book.Supplements.Any() ? PdfState.NoPdf
: TransitionalFileLocator.PDF_Exists(book) ? PdfState.Downloaded
: PdfState.NotDownloaded;
public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int pdfsDownloaded, int pdfsNotDownloaded) { } public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int pdfsDownloaded, int pdfsNotDownloaded) { }
public static LibraryStats GetCounts() public static LibraryStats GetCounts()
{ {
@ -190,21 +203,21 @@ namespace ApplicationServices
var results = libraryBooks var results = libraryBooks
.AsParallel() .AsParallel()
.Select(lb => getAudioFileState(lb.Book)) .Select(lb => Liberated_Status(lb.Book))
.ToList(); .ToList();
var booksFullyBackedUp = results.Count(r => r == AudioFileState.full); var booksFullyBackedUp = results.Count(r => r == LiberatedState.Liberated);
var booksDownloadedOnly = results.Count(r => r == AudioFileState.aax); var booksDownloadedOnly = results.Count(r => r == LiberatedState.PartialDownload);
var booksNoProgress = results.Count(r => r == AudioFileState.none); var booksNoProgress = results.Count(r => r == LiberatedState.NotDownloaded);
Log.Logger.Information("Book counts. {@DebugInfo}", new { total = results.Count, booksFullyBackedUp, booksDownloadedOnly, booksNoProgress }); Log.Logger.Information("Book counts. {@DebugInfo}", new { total = results.Count, booksFullyBackedUp, booksDownloadedOnly, booksNoProgress });
var boolResults = libraryBooks var boolResults = libraryBooks
.AsParallel() .AsParallel()
.Where(lb => lb.Book.Supplements.Any()) .Where(lb => lb.Book.Supplements.Any())
.Select(lb => TransitionalFileLocator.PDF_Exists(lb.Book)) .Select(lb => Pdf_Status(lb.Book))
.ToList(); .ToList();
var pdfsDownloaded = boolResults.Count(r => r); var pdfsDownloaded = boolResults.Count(r => r == PdfState.Downloaded);
var pdfsNotDownloaded = boolResults.Count(r => !r); var pdfsNotDownloaded = boolResults.Count(r => r == PdfState.NotDownloaded);
Log.Logger.Information("PDF counts. {@DebugInfo}", new { total = boolResults.Count, pdfsDownloaded, pdfsNotDownloaded }); Log.Logger.Information("PDF counts. {@DebugInfo}", new { total = boolResults.Count, pdfsDownloaded, pdfsNotDownloaded });

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.4.6.1</Version> <Version>5.4.7.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using ApplicationServices;
using DataLayer; using DataLayer;
namespace LibationWinForms namespace LibationWinForms
@ -19,24 +20,18 @@ namespace LibationWinForms
// hide from public fields from Data Source GUI with [Browsable(false)] // hide from public fields from Data Source GUI with [Browsable(false)]
[Browsable(false)]
public string AudibleProductId => book.AudibleProductId;
[Browsable(false)] [Browsable(false)]
public string Tags => book.UserDefinedItem.Tags; public string Tags => book.UserDefinedItem.Tags;
[Browsable(false)] [Browsable(false)]
public IEnumerable<string> TagsEnumerated => book.UserDefinedItem.TagsEnumerated; public IEnumerable<string> TagsEnumerated => book.UserDefinedItem.TagsEnumerated;
public enum LiberatedState { NotDownloaded, PartialDownload, Liberated }
[Browsable(false)] [Browsable(false)]
public LiberatedState Liberated_Status public string PictureId => book.PictureId;
=> ApplicationServices.TransitionalFileLocator.Audio_Exists(book) ? LiberatedState.Liberated
: ApplicationServices.TransitionalFileLocator.AAXC_Exists(book) ? LiberatedState.PartialDownload
: LiberatedState.NotDownloaded;
public enum PdfState { NoPdf, Downloaded, NotDownloaded }
[Browsable(false)] [Browsable(false)]
public PdfState Pdf_Status public LiberatedState Liberated_Status => LibraryCommands.Liberated_Status(book);
=> !book.Supplements.Any() ? PdfState.NoPdf [Browsable(false)]
: ApplicationServices.TransitionalFileLocator.PDF_Exists(book) ? PdfState.Downloaded public PdfState Pdf_Status => LibraryCommands.Pdf_Status(book);
: PdfState.NotDownloaded;
// displayValues is what gets displayed // displayValues is what gets displayed
// the value that gets returned from the property is the cell's value // the value that gets returned from the property is the cell's value

View File

@ -134,25 +134,25 @@ namespace LibationWinForms
{ {
var libState = liberatedStatus switch var libState = liberatedStatus switch
{ {
GridEntry.LiberatedState.Liberated => "Liberated", LiberatedState.Liberated => "Liberated",
GridEntry.LiberatedState.PartialDownload => "File has been at least\r\npartially downloaded", LiberatedState.PartialDownload => "File has been at least\r\npartially downloaded",
GridEntry.LiberatedState.NotDownloaded => "Book NOT downloaded", LiberatedState.NotDownloaded => "Book NOT downloaded",
_ => throw new Exception("Unexpected liberation state") _ => throw new Exception("Unexpected liberation state")
}; };
var pdfState = pdfStatus switch var pdfState = pdfStatus switch
{ {
GridEntry.PdfState.Downloaded => "\r\nPDF downloaded", PdfState.Downloaded => "\r\nPDF downloaded",
GridEntry.PdfState.NotDownloaded => "\r\nPDF NOT downloaded", PdfState.NotDownloaded => "\r\nPDF NOT downloaded",
GridEntry.PdfState.NoPdf => "", PdfState.NoPdf => "",
_ => throw new Exception("Unexpected PDF state") _ => throw new Exception("Unexpected PDF state")
}; };
var text = libState + pdfState; var text = libState + pdfState;
if (liberatedStatus == GridEntry.LiberatedState.NotDownloaded || if (liberatedStatus == LiberatedState.NotDownloaded ||
liberatedStatus == GridEntry.LiberatedState.PartialDownload || liberatedStatus == LiberatedState.PartialDownload ||
pdfStatus == GridEntry.PdfState.NotDownloaded) pdfStatus == PdfState.NotDownloaded)
text += "\r\nClick to complete"; text += "\r\nClick to complete";
//DEBUG//cell.Value = text; //DEBUG//cell.Value = text;
@ -162,14 +162,14 @@ namespace LibationWinForms
// draw img // draw img
{ {
var image_lib var image_lib
= liberatedStatus == GridEntry.LiberatedState.NotDownloaded ? "red" = liberatedStatus == LiberatedState.NotDownloaded ? "red"
: liberatedStatus == GridEntry.LiberatedState.PartialDownload ? "yellow" : liberatedStatus == LiberatedState.PartialDownload ? "yellow"
: liberatedStatus == GridEntry.LiberatedState.Liberated ? "green" : liberatedStatus == LiberatedState.Liberated ? "green"
: throw new Exception("Unexpected liberation state"); : throw new Exception("Unexpected liberation state");
var image_pdf var image_pdf
= pdfStatus == GridEntry.PdfState.NoPdf ? "" = pdfStatus == PdfState.NoPdf ? ""
: pdfStatus == GridEntry.PdfState.NotDownloaded ? "_pdf_no" : pdfStatus == PdfState.NotDownloaded ? "_pdf_no"
: pdfStatus == GridEntry.PdfState.Downloaded ? "_pdf_yes" : pdfStatus == PdfState.Downloaded ? "_pdf_yes"
: throw new Exception("Unexpected PDF state"); : throw new Exception("Unexpected PDF state");
var image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}"); var image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}");
drawImage(e, image); drawImage(e, image);
@ -192,13 +192,14 @@ namespace LibationWinForms
return; return;
} }
// else: liberate
await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(libraryBook, (_, __) => RefreshRow(libraryBook.Book.AudibleProductId)); await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(libraryBook, (_, __) => RefreshRow(libraryBook.Book.AudibleProductId));
} }
#endregion #endregion
public void RefreshRow(string productId) public void RefreshRow(string productId)
{ {
var rowId = getRowId((ge) => ge.GetBook().AudibleProductId == productId); var rowId = getRowId((ge) => ge.AudibleProductId == productId);
// update cells incl Liberate button text // update cells incl Liberate button text
dataGridView.InvalidateRow(rowId); dataGridView.InvalidateRow(rowId);
@ -329,7 +330,7 @@ namespace LibationWinForms
=> dataGridView.UIThread(() => updateRowImage(pictureId)); => dataGridView.UIThread(() => updateRowImage(pictureId));
private void updateRowImage(string pictureId) private void updateRowImage(string pictureId)
{ {
var rowId = getRowId((ge) => ge.GetBook().PictureId == pictureId); var rowId = getRowId((ge) => ge.PictureId == pictureId);
if (rowId > -1) if (rowId > -1)
dataGridView.InvalidateRow(rowId); dataGridView.InvalidateRow(rowId);
} }
@ -397,7 +398,7 @@ namespace LibationWinForms
currencyManager.SuspendBinding(); currencyManager.SuspendBinding();
{ {
for (var r = dataGridView.RowCount - 1; r >= 0; r--) for (var r = dataGridView.RowCount - 1; r >= 0; r--)
dataGridView.Rows[r].Visible = productIds.Contains(getGridEntry(r).GetBook().AudibleProductId); dataGridView.Rows[r].Visible = productIds.Contains(getGridEntry(r).AudibleProductId);
} }
currencyManager.ResumeBinding(); currencyManager.ResumeBinding();
VisibleCountChanged?.Invoke(this, dataGridView.AsEnumerable().Count(r => r.Visible)); VisibleCountChanged?.Invoke(this, dataGridView.AsEnumerable().Count(r => r.Visible));