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
{
// 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
{
#region FULL LIBRARY scan and import
@ -177,12 +184,18 @@ namespace ApplicationServices
}
#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(Book book)
=> TransitionalFileLocator.Audio_Exists(book) ? AudioFileState.full
: TransitionalFileLocator.AAXC_Exists(book) ? AudioFileState.aax
: AudioFileState.none;
// below are queries, not commands. maybe I should make a LibraryQueries. except there's already one of those...
public static LiberatedState Liberated_Status(Book book)
=> TransitionalFileLocator.Audio_Exists(book) ? LiberatedState.Liberated
: TransitionalFileLocator.AAXC_Exists(book) ? LiberatedState.PartialDownload
: 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 static LibraryStats GetCounts()
{
@ -190,21 +203,21 @@ namespace ApplicationServices
var results = libraryBooks
.AsParallel()
.Select(lb => getAudioFileState(lb.Book))
.Select(lb => Liberated_Status(lb.Book))
.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);
var booksFullyBackedUp = results.Count(r => r == LiberatedState.Liberated);
var booksDownloadedOnly = results.Count(r => r == LiberatedState.PartialDownload);
var booksNoProgress = results.Count(r => r == LiberatedState.NotDownloaded);
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 => TransitionalFileLocator.PDF_Exists(lb.Book))
.Select(lb => Pdf_Status(lb.Book))
.ToList();
var pdfsDownloaded = boolResults.Count(r => r);
var pdfsNotDownloaded = boolResults.Count(r => !r);
var pdfsDownloaded = boolResults.Count(r => r == PdfState.Downloaded);
var pdfsNotDownloaded = boolResults.Count(r => r == PdfState.NotDownloaded);
Log.Logger.Information("PDF counts. {@DebugInfo}", new { total = boolResults.Count, pdfsDownloaded, pdfsNotDownloaded });

View File

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

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using ApplicationServices;
using DataLayer;
namespace LibationWinForms
@ -19,24 +20,18 @@ namespace LibationWinForms
// hide from public fields from Data Source GUI with [Browsable(false)]
[Browsable(false)]
public string AudibleProductId => book.AudibleProductId;
[Browsable(false)]
public string Tags => book.UserDefinedItem.Tags;
[Browsable(false)]
public IEnumerable<string> TagsEnumerated => book.UserDefinedItem.TagsEnumerated;
public enum LiberatedState { NotDownloaded, PartialDownload, Liberated }
[Browsable(false)]
public LiberatedState Liberated_Status
=> ApplicationServices.TransitionalFileLocator.Audio_Exists(book) ? LiberatedState.Liberated
: ApplicationServices.TransitionalFileLocator.AAXC_Exists(book) ? LiberatedState.PartialDownload
: LiberatedState.NotDownloaded;
public enum PdfState { NoPdf, Downloaded, NotDownloaded }
public string PictureId => book.PictureId;
[Browsable(false)]
public PdfState Pdf_Status
=> !book.Supplements.Any() ? PdfState.NoPdf
: ApplicationServices.TransitionalFileLocator.PDF_Exists(book) ? PdfState.Downloaded
: PdfState.NotDownloaded;
public LiberatedState Liberated_Status => LibraryCommands.Liberated_Status(book);
[Browsable(false)]
public PdfState Pdf_Status => LibraryCommands.Pdf_Status(book);
// displayValues is what gets displayed
// 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
{
GridEntry.LiberatedState.Liberated => "Liberated",
GridEntry.LiberatedState.PartialDownload => "File has been at least\r\npartially downloaded",
GridEntry.LiberatedState.NotDownloaded => "Book NOT downloaded",
LiberatedState.Liberated => "Liberated",
LiberatedState.PartialDownload => "File has been at least\r\npartially downloaded",
LiberatedState.NotDownloaded => "Book NOT downloaded",
_ => throw new Exception("Unexpected liberation state")
};
var pdfState = pdfStatus switch
{
GridEntry.PdfState.Downloaded => "\r\nPDF downloaded",
GridEntry.PdfState.NotDownloaded => "\r\nPDF NOT downloaded",
GridEntry.PdfState.NoPdf => "",
PdfState.Downloaded => "\r\nPDF downloaded",
PdfState.NotDownloaded => "\r\nPDF NOT downloaded",
PdfState.NoPdf => "",
_ => throw new Exception("Unexpected PDF state")
};
var text = libState + pdfState;
if (liberatedStatus == GridEntry.LiberatedState.NotDownloaded ||
liberatedStatus == GridEntry.LiberatedState.PartialDownload ||
pdfStatus == GridEntry.PdfState.NotDownloaded)
if (liberatedStatus == LiberatedState.NotDownloaded ||
liberatedStatus == LiberatedState.PartialDownload ||
pdfStatus == PdfState.NotDownloaded)
text += "\r\nClick to complete";
//DEBUG//cell.Value = text;
@ -162,14 +162,14 @@ namespace LibationWinForms
// draw img
{
var image_lib
= liberatedStatus == GridEntry.LiberatedState.NotDownloaded ? "red"
: liberatedStatus == GridEntry.LiberatedState.PartialDownload ? "yellow"
: liberatedStatus == GridEntry.LiberatedState.Liberated ? "green"
= liberatedStatus == LiberatedState.NotDownloaded ? "red"
: liberatedStatus == LiberatedState.PartialDownload ? "yellow"
: liberatedStatus == LiberatedState.Liberated ? "green"
: throw new Exception("Unexpected liberation state");
var image_pdf
= pdfStatus == GridEntry.PdfState.NoPdf ? ""
: pdfStatus == GridEntry.PdfState.NotDownloaded ? "_pdf_no"
: pdfStatus == GridEntry.PdfState.Downloaded ? "_pdf_yes"
= pdfStatus == PdfState.NoPdf ? ""
: pdfStatus == PdfState.NotDownloaded ? "_pdf_no"
: pdfStatus == PdfState.Downloaded ? "_pdf_yes"
: throw new Exception("Unexpected PDF state");
var image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}");
drawImage(e, image);
@ -192,13 +192,14 @@ namespace LibationWinForms
return;
}
// else: liberate
await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(libraryBook, (_, __) => RefreshRow(libraryBook.Book.AudibleProductId));
}
#endregion
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
dataGridView.InvalidateRow(rowId);
@ -329,7 +330,7 @@ namespace LibationWinForms
=> dataGridView.UIThread(() => updateRowImage(pictureId));
private void updateRowImage(string pictureId)
{
var rowId = getRowId((ge) => ge.GetBook().PictureId == pictureId);
var rowId = getRowId((ge) => ge.PictureId == pictureId);
if (rowId > -1)
dataGridView.InvalidateRow(rowId);
}
@ -397,7 +398,7 @@ namespace LibationWinForms
currencyManager.SuspendBinding();
{
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();
VisibleCountChanged?.Invoke(this, dataGridView.AsEnumerable().Count(r => r.Visible));