Refactoring.
This commit is contained in:
parent
e1dfefbadf
commit
95766a43c5
11 changed files with 197 additions and 242 deletions
|
|
@ -4,36 +4,32 @@ using System.Collections.Generic;
|
|||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using ApplicationServices;
|
||||
using DataLayer;
|
||||
using Dinah.Core.Drawing;
|
||||
|
||||
namespace LibationWinForms
|
||||
{
|
||||
internal class GridEntry : INotifyPropertyChanged, IObjectMemberComparable
|
||||
internal class GridEntry : AsyncNotifyPropertyChanged, IObjectMemberComparable
|
||||
{
|
||||
#region implementation properties
|
||||
// hide from public fields from Data Source GUI with [Browsable(false)]
|
||||
#region implementation properties
|
||||
// hide from public fields from Data Source GUI with [Browsable(false)]
|
||||
|
||||
[Browsable(false)]
|
||||
[Browsable(false)]
|
||||
public string AudibleProductId => Book.AudibleProductId;
|
||||
[Browsable(false)]
|
||||
public LibraryBook LibraryBook { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
private Book Book => LibraryBook.Book;
|
||||
private SynchronizationContext SyncContext { get; } = SynchronizationContext.Current;
|
||||
private Image _cover;
|
||||
|
||||
public GridEntry(LibraryBook libraryBook)
|
||||
{
|
||||
LibraryBook = libraryBook;
|
||||
|
||||
_compareValues = CreatePropertyValueDictionary();
|
||||
_memberValues = CreateMemberValueDictionary();
|
||||
|
||||
//Get cover art. If it's default, subscribe to PictureCached
|
||||
var picDef = new FileManager.PictureDefinition(Book.PictureId, FileManager.PictureSize._80x80);
|
||||
|
|
@ -42,7 +38,7 @@ namespace LibationWinForms
|
|||
if (isDefault)
|
||||
FileManager.PictureStorage.PictureCached += PictureStorage_PictureCached;
|
||||
|
||||
//Mutable property. Set the field so PropertyChanged doesn't fire.
|
||||
//Mutable property. Set the field so PropertyChanged isn't fired.
|
||||
_cover = ImageReader.ToImage(picture);
|
||||
|
||||
//Immutable properties
|
||||
|
|
@ -50,9 +46,9 @@ namespace LibationWinForms
|
|||
Title = Book.Title;
|
||||
Series = Book.SeriesNames;
|
||||
Length = Book.LengthInMinutes == 0 ? "" : $"{Book.LengthInMinutes / 60} hr {Book.LengthInMinutes % 60} min";
|
||||
MyRating = GetStarString(Book.UserDefinedItem.Rating);
|
||||
MyRating = ValueOrDefault(Book.UserDefinedItem.Rating?.ToStarString(), "");
|
||||
PurchaseDate = libraryBook.DateAdded.ToString("d");
|
||||
ProductRating = GetStarString(Book.Rating);
|
||||
ProductRating = ValueOrDefault(Book.Rating?.ToStarString(), "");
|
||||
Authors = Book.AuthorNames;
|
||||
Narrators = Book.NarratorNames;
|
||||
Category = string.Join(" > ", Book.CategoriesNames);
|
||||
|
|
@ -60,28 +56,20 @@ namespace LibationWinForms
|
|||
Description = GetDescriptionDisplay(Book);
|
||||
}
|
||||
|
||||
//DisplayTags and Liberate are live.
|
||||
//DisplayTags and Liberate properties are live.
|
||||
}
|
||||
|
||||
private void PictureStorage_PictureCached(object sender, string pictureId)
|
||||
private void PictureStorage_PictureCached(object sender, string pictureId)
|
||||
{
|
||||
if (pictureId == Book.PictureId)
|
||||
{
|
||||
//GridEntry SHOULD be UI-ignorant, but PropertyChanged
|
||||
Cover = WindowsDesktopUtilities.WinAudibleImageServer.GetImage(pictureId, FileManager.PictureSize._80x80);
|
||||
FileManager.PictureStorage.PictureCached -= PictureStorage_PictureCached;
|
||||
}
|
||||
}
|
||||
|
||||
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
|
||||
=> SyncContext.Post(
|
||||
args => OnPropertyChangedAsync(args as AsyncCompletedEventArgs),
|
||||
new AsyncCompletedEventArgs(null, false, new PropertyChangedEventArgs(propertyName)));
|
||||
|
||||
private void OnPropertyChangedAsync(AsyncCompletedEventArgs e) =>
|
||||
PropertyChanged?.Invoke(this, e.UserState as PropertyChangedEventArgs);
|
||||
|
||||
#region Data Source properties
|
||||
|
||||
public Image Cover
|
||||
{
|
||||
get
|
||||
|
|
@ -106,7 +94,6 @@ namespace LibationWinForms
|
|||
public string Category { get; }
|
||||
public string Misc { get; }
|
||||
public string Description { get; }
|
||||
|
||||
public string DisplayTags => string.Join("\r\n", Book.UserDefinedItem.TagsEnumerated);
|
||||
public (LiberatedState, PdfState) Liberate => (LibraryCommands.Liberated_Status(Book), LibraryCommands.Pdf_Status(Book));
|
||||
|
||||
|
|
@ -114,49 +101,43 @@ namespace LibationWinForms
|
|||
|
||||
#region Data Sorting
|
||||
|
||||
private Dictionary<string, Func<object>> _compareValues { get; }
|
||||
private static Dictionary<Type, IComparer> _objectComparers;
|
||||
|
||||
public virtual object GetMemberValue(string memberName) => _compareValues[memberName]();
|
||||
public virtual IComparer GetMemberComparer(Type memberType) => _objectComparers[memberType];
|
||||
private Dictionary<string, Func<object>> _memberValues { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Instantiate comparers for every type needed to sort columns.
|
||||
/// Create getters for all member object values by name
|
||||
/// </summary>
|
||||
static GridEntry()
|
||||
Dictionary<string, Func<object>> CreateMemberValueDictionary() => new()
|
||||
{
|
||||
_objectComparers = new Dictionary<Type, IComparer>()
|
||||
{
|
||||
{ typeof(string), new ObjectComparer<string>() },
|
||||
{ typeof(int), new ObjectComparer<int>() },
|
||||
{ typeof(float), new ObjectComparer<float>() },
|
||||
{ typeof(DateTime), new ObjectComparer<DateTime>() },
|
||||
{ typeof(LiberatedState), new ObjectComparer<LiberatedState>() },
|
||||
};
|
||||
}
|
||||
{ nameof(Title), () => GetSortName(Book.Title) },
|
||||
{ nameof(Series), () => GetSortName(Book.SeriesNames) },
|
||||
{ nameof(Length), () => Book.LengthInMinutes },
|
||||
{ nameof(MyRating), () => Book.UserDefinedItem.Rating.FirstScore },
|
||||
{ nameof(PurchaseDate), () => LibraryBook.DateAdded },
|
||||
{ nameof(ProductRating), () => Book.Rating.FirstScore },
|
||||
{ nameof(Authors), () => Authors },
|
||||
{ nameof(Narrators), () => Narrators },
|
||||
{ nameof(Description), () => Description },
|
||||
{ nameof(Category), () => Category },
|
||||
{ nameof(Misc), () => Misc },
|
||||
{ nameof(DisplayTags), () => DisplayTags },
|
||||
{ nameof(Liberate), () => Liberate.Item1 }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Create getters for all member values by name
|
||||
/// </summary>
|
||||
Dictionary<string, Func<object>> CreatePropertyValueDictionary() => new()
|
||||
{
|
||||
{ nameof(Title), () => getSortName(Book.Title)},
|
||||
{ nameof(Series),() => getSortName(Book.SeriesNames)},
|
||||
{ nameof(Length), () => Book.LengthInMinutes},
|
||||
{ nameof(MyRating), () => Book.UserDefinedItem.Rating.FirstScore},
|
||||
{ nameof(PurchaseDate), () => LibraryBook.DateAdded},
|
||||
{ nameof(ProductRating), () => Book.Rating.FirstScore},
|
||||
{ nameof(Authors), () => Authors},
|
||||
{ nameof(Narrators), () => Narrators},
|
||||
{ nameof(Description), () => Description},
|
||||
{ nameof(Category), () => Category},
|
||||
{ nameof(Misc), () => Misc},
|
||||
{ nameof(DisplayTags), () => DisplayTags},
|
||||
{ nameof(Liberate), () => Liberate.Item1}
|
||||
};
|
||||
// Instantiate comparers for every exposed member object type.
|
||||
private static readonly Dictionary<Type, IComparer> _memberTypeComparers = new()
|
||||
{
|
||||
{ typeof(string), new ObjectComparer<string>() },
|
||||
{ typeof(int), new ObjectComparer<int>() },
|
||||
{ typeof(float), new ObjectComparer<float>() },
|
||||
{ typeof(DateTime), new ObjectComparer<DateTime>() },
|
||||
{ typeof(LiberatedState), new ObjectComparer<LiberatedState>() },
|
||||
};
|
||||
|
||||
private static readonly string[] sortPrefixIgnores = { "the", "a", "an" };
|
||||
private static string getSortName(string unformattedName)
|
||||
public virtual object GetMemberValue(string memberName) => _memberValues[memberName]();
|
||||
public virtual IComparer GetMemberComparer(Type memberType) => _memberTypeComparers[memberType];
|
||||
|
||||
private static readonly string[] _sortPrefixIgnores = { "the", "a", "an" };
|
||||
private static string GetSortName(string unformattedName)
|
||||
{
|
||||
var sortName = unformattedName
|
||||
.Replace("|", "")
|
||||
|
|
@ -164,98 +145,71 @@ namespace LibationWinForms
|
|||
.ToLowerInvariant()
|
||||
.Trim();
|
||||
|
||||
if (sortPrefixIgnores.Any(prefix => sortName.StartsWith(prefix + " ")))
|
||||
if (_sortPrefixIgnores.Any(prefix => sortName.StartsWith(prefix + " ")))
|
||||
sortName = sortName.Substring(sortName.IndexOf(" ") + 1).TrimStart();
|
||||
|
||||
return sortName;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Static library display functions
|
||||
|
||||
|
||||
public static (string mouseoverText, Bitmap buttonImage) GetLiberateDisplay(LiberatedState liberatedStatus, PdfState pdfStatus)
|
||||
{
|
||||
string text;
|
||||
Bitmap image;
|
||||
|
||||
// get mouseover text
|
||||
(string libState, string image_lib) = liberatedStatus switch
|
||||
{
|
||||
var libState = liberatedStatus switch
|
||||
{
|
||||
LiberatedState.Liberated => "Liberated",
|
||||
LiberatedState.PartialDownload => "File has been at least\r\npartially downloaded",
|
||||
LiberatedState.NotDownloaded => "Book NOT downloaded",
|
||||
_ => throw new Exception("Unexpected liberation state")
|
||||
};
|
||||
LiberatedState.Liberated => ("Liberated", "green"),
|
||||
LiberatedState.PartialDownload => ("File has been at least\r\npartially downloaded", "yellow"),
|
||||
LiberatedState.NotDownloaded => ("Book NOT downloaded", "red"),
|
||||
_ => throw new Exception("Unexpected liberation state")
|
||||
};
|
||||
|
||||
var pdfState = pdfStatus switch
|
||||
{
|
||||
PdfState.Downloaded => "\r\nPDF downloaded",
|
||||
PdfState.NotDownloaded => "\r\nPDF NOT downloaded",
|
||||
PdfState.NoPdf => "",
|
||||
_ => throw new Exception("Unexpected PDF state")
|
||||
};
|
||||
|
||||
text = libState + pdfState;
|
||||
|
||||
if (liberatedStatus == LiberatedState.NotDownloaded ||
|
||||
liberatedStatus == LiberatedState.PartialDownload ||
|
||||
pdfStatus == PdfState.NotDownloaded)
|
||||
text += "\r\nClick to complete";
|
||||
|
||||
}
|
||||
|
||||
// get image
|
||||
(string pdfState, string image_pdf) = pdfStatus switch
|
||||
{
|
||||
var image_lib
|
||||
= liberatedStatus == LiberatedState.NotDownloaded ? "red"
|
||||
: liberatedStatus == LiberatedState.PartialDownload ? "yellow"
|
||||
: liberatedStatus == LiberatedState.Liberated ? "green"
|
||||
: throw new Exception("Unexpected liberation state");
|
||||
var image_pdf
|
||||
= pdfStatus == PdfState.NoPdf ? ""
|
||||
: pdfStatus == PdfState.NotDownloaded ? "_pdf_no"
|
||||
: pdfStatus == PdfState.Downloaded ? "_pdf_yes"
|
||||
: throw new Exception("Unexpected PDF state");
|
||||
PdfState.Downloaded => ("\r\nPDF downloaded", "_pdf_yes"),
|
||||
PdfState.NotDownloaded => ("\r\nPDF NOT downloaded", "_pdf_no"),
|
||||
PdfState.NoPdf => ("", ""),
|
||||
_ => throw new Exception("Unexpected PDF state")
|
||||
};
|
||||
|
||||
image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}");
|
||||
}
|
||||
var mouseoverText = libState + pdfState;
|
||||
|
||||
return (text, image);
|
||||
if (liberatedStatus == LiberatedState.NotDownloaded ||
|
||||
liberatedStatus == LiberatedState.PartialDownload ||
|
||||
pdfStatus == PdfState.NotDownloaded)
|
||||
mouseoverText += "\r\nClick to complete";
|
||||
|
||||
var buttonImage = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}");
|
||||
|
||||
return (mouseoverText, buttonImage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This information should not change during <see cref="GridEntry"/> lifetime, so call only once.
|
||||
/// </summary>
|
||||
private static string GetDescriptionDisplay(Book book)
|
||||
{
|
||||
{
|
||||
var doc = new HtmlAgilityPack.HtmlDocument();
|
||||
doc.LoadHtml(book.Description);
|
||||
var noHtml = doc.DocumentNode.InnerText;
|
||||
return
|
||||
noHtml.Length < 63?
|
||||
noHtml :
|
||||
return
|
||||
noHtml.Length < 63 ?
|
||||
noHtml :
|
||||
noHtml.Substring(0, 60) + "...";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This information should not change during <see cref="GridEntry"/> lifetime, so call only once.
|
||||
/// Maximum of 5 text rows will fit in 80-pixel row height.
|
||||
/// </summary>
|
||||
private static string GetMiscDisplay(LibraryBook libraryBook)
|
||||
{
|
||||
// max 5 text rows
|
||||
|
||||
var details = new List<string>();
|
||||
|
||||
var locale
|
||||
= string.IsNullOrWhiteSpace(libraryBook.Book.Locale)
|
||||
? "[unknown]"
|
||||
: libraryBook.Book.Locale;
|
||||
var acct
|
||||
= string.IsNullOrWhiteSpace(libraryBook.Account)
|
||||
? "[unknown]"
|
||||
: libraryBook.Account;
|
||||
var locale = ValueOrDefault(libraryBook.Book.Locale, "[unknown]");
|
||||
var acct = ValueOrDefault(libraryBook.Account, "[unknown]");
|
||||
|
||||
details.Add($"Account: {locale} - {acct}");
|
||||
|
||||
if (libraryBook.Book.HasPdf)
|
||||
|
|
@ -274,11 +228,10 @@ namespace LibationWinForms
|
|||
return string.Join("\r\n", details);
|
||||
}
|
||||
|
||||
private static string GetStarString(Rating rating)
|
||||
=> (rating?.FirstScore > 0f)
|
||||
? rating?.ToStarString()
|
||||
: "";
|
||||
//Maybe add to Dinah StringExtensions?
|
||||
private static string ValueOrDefault(string value, string defaultValue)
|
||||
=> string.IsNullOrWhiteSpace(value) ? defaultValue : value;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue