Major UI refactoring.
This commit is contained in:
parent
af48641281
commit
d48bd5ad07
13 changed files with 769 additions and 653 deletions
|
|
@ -1,86 +1,159 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ApplicationServices;
|
||||
using DataLayer;
|
||||
using Dinah.Core.Drawing;
|
||||
|
||||
namespace LibationWinForms
|
||||
{
|
||||
internal class GridEntry
|
||||
internal class GridEntry : INotifyPropertyChanged, IObjectMemberComparable
|
||||
{
|
||||
private LibraryBook libraryBook { get; }
|
||||
private Book book => libraryBook.Book;
|
||||
public const string LIBERATE_COLUMN_NAME = "Liberate";
|
||||
public const string EDIT_TAGS_COLUMN_NAME = "DisplayTags";
|
||||
|
||||
public Book GetBook() => book;
|
||||
public LibraryBook GetLibraryBook() => libraryBook;
|
||||
|
||||
public GridEntry(LibraryBook libraryBook) => this.libraryBook = libraryBook;
|
||||
|
||||
// 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)]
|
||||
public string AudibleProductId => Book.AudibleProductId;
|
||||
[Browsable(false)]
|
||||
public string AudibleProductId => book.AudibleProductId;
|
||||
public string Tags => Book.UserDefinedItem.Tags;
|
||||
[Browsable(false)]
|
||||
public string Tags => book.UserDefinedItem.Tags;
|
||||
public IEnumerable<string> TagsEnumerated => Book.UserDefinedItem.TagsEnumerated;
|
||||
[Browsable(false)]
|
||||
public IEnumerable<string> TagsEnumerated => book.UserDefinedItem.TagsEnumerated;
|
||||
[Browsable(false)]
|
||||
public string PictureId => book.PictureId;
|
||||
[Browsable(false)]
|
||||
public LiberatedState Liberated_Status => LibraryCommands.Liberated_Status(book);
|
||||
[Browsable(false)]
|
||||
public PdfState Pdf_Status => LibraryCommands.Pdf_Status(book);
|
||||
public LibraryBook LibraryBook { get; }
|
||||
|
||||
// displayValues is what gets displayed
|
||||
// the value that gets returned from the property is the cell's value
|
||||
// this allows for the value to be sorted one way and displayed another
|
||||
// eg:
|
||||
// orig title: The Computer
|
||||
// formatReplacement: The Computer
|
||||
// value for sorting: Computer
|
||||
private Dictionary<string, string> displayValues { get; } = new Dictionary<string, string>();
|
||||
public bool TryDisplayValue(string key, out string value) => displayValues.TryGetValue(key, out value);
|
||||
#endregion
|
||||
|
||||
public Image Cover =>
|
||||
WindowsDesktopUtilities.WinAudibleImageServer.GetImage(book.PictureId, FileManager.PictureSize._80x80);
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
private Book Book => LibraryBook.Book;
|
||||
private Image _cover;
|
||||
|
||||
public string Title
|
||||
public GridEntry(LibraryBook libraryBook)
|
||||
{
|
||||
get
|
||||
LibraryBook = libraryBook;
|
||||
|
||||
_compareValues = CreatePropertyValueDictionary();
|
||||
|
||||
//Get cover art. If it's default, subscribe to PictureCached
|
||||
var picDef = new FileManager.PictureDefinition(Book.PictureId, FileManager.PictureSize._80x80);
|
||||
(bool isDefault, byte[] picture) = FileManager.PictureStorage.GetPicture(picDef);
|
||||
|
||||
if (isDefault)
|
||||
FileManager.PictureStorage.PictureCached += PictureStorage_PictureCached;
|
||||
|
||||
//Mutable property. Set the field so PropertyChanged doesn't fire.
|
||||
_cover = ImageReader.ToImage(picture);
|
||||
|
||||
//Immutable properties
|
||||
{
|
||||
displayValues[nameof(Title)] = book.Title;
|
||||
return getSortName(book.Title);
|
||||
Title = Book.Title;
|
||||
Series = Book.SeriesNames;
|
||||
Length = Book.LengthInMinutes == 0 ? "" : $"{Book.LengthInMinutes / 60} hr {Book.LengthInMinutes % 60} min";
|
||||
MyRating = GetStarString(Book.UserDefinedItem.Rating);
|
||||
PurchaseDate = libraryBook.DateAdded.ToString("d");
|
||||
ProductRating = GetStarString(Book.Rating);
|
||||
Authors = Book.AuthorNames;
|
||||
Narrators = Book.NarratorNames;
|
||||
Category = string.Join(" > ", Book.CategoriesNames);
|
||||
Misc = GetMiscDisplay(libraryBook);
|
||||
Description = GetDescriptionDisplay(Book);
|
||||
}
|
||||
|
||||
//DisplayTags and Liberate are live.
|
||||
}
|
||||
|
||||
private void PictureStorage_PictureCached(object sender, string pictureId)
|
||||
{
|
||||
if (pictureId == Book.PictureId)
|
||||
{
|
||||
Cover = WindowsDesktopUtilities.WinAudibleImageServer.GetImage(pictureId, FileManager.PictureSize._80x80);
|
||||
FileManager.PictureStorage.PictureCached -= PictureStorage_PictureCached;
|
||||
}
|
||||
}
|
||||
|
||||
public string Authors => book.AuthorNames;
|
||||
public string Narrators => book.NarratorNames;
|
||||
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
|
||||
public int Length
|
||||
#region Data Source properties
|
||||
public Image Cover
|
||||
{
|
||||
get
|
||||
{
|
||||
displayValues[nameof(Length)]
|
||||
= book.LengthInMinutes == 0
|
||||
? ""
|
||||
: $"{book.LengthInMinutes / 60} hr {book.LengthInMinutes % 60} min";
|
||||
|
||||
return book.LengthInMinutes;
|
||||
return _cover;
|
||||
}
|
||||
}
|
||||
|
||||
public string Series
|
||||
{
|
||||
get
|
||||
private set
|
||||
{
|
||||
displayValues[nameof(Series)] = book.SeriesNames;
|
||||
return getSortName(book.SeriesNames);
|
||||
_cover = value;
|
||||
NotifyPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private static string[] sortPrefixIgnores { get; } = new[] { "the", "a", "an" };
|
||||
public string ProductRating { get; }
|
||||
public string PurchaseDate { get; }
|
||||
public string MyRating { get; }
|
||||
public string Series { get; }
|
||||
public string Title { get; }
|
||||
public string Length { get; }
|
||||
public string Authors { get; }
|
||||
public string Narrators { get; }
|
||||
public string Category { get; }
|
||||
public string Misc { get; }
|
||||
public string Description { get; }
|
||||
|
||||
public string DisplayTags => string.Join("\r\n", TagsEnumerated);
|
||||
public (LiberatedState, PdfState) Liberate => (LibraryCommands.Liberated_Status(Book), LibraryCommands.Pdf_Status(Book));
|
||||
|
||||
#endregion
|
||||
|
||||
#region Data Sorting
|
||||
|
||||
private Dictionary<string, Func<object>> _compareValues { get; }
|
||||
private static Dictionary<Type, IComparer> _objectComparers;
|
||||
|
||||
public object GetMemberValue(string propertyName) => _compareValues[propertyName]();
|
||||
public IComparer GetComparer(Type propertyType) => _objectComparers[propertyType];
|
||||
|
||||
/// <summary>
|
||||
/// Instantiate comparers for every type needed to sort columns.
|
||||
/// </summary>
|
||||
static GridEntry()
|
||||
{
|
||||
_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>() },
|
||||
};
|
||||
}
|
||||
/// <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}
|
||||
};
|
||||
|
||||
private static readonly string[] sortPrefixIgnores = { "the", "a", "an" };
|
||||
private static string getSortName(string unformattedName)
|
||||
{
|
||||
var sortName = unformattedName
|
||||
|
|
@ -95,104 +168,115 @@ namespace LibationWinForms
|
|||
return sortName;
|
||||
}
|
||||
|
||||
private string descriptionCache = null;
|
||||
public string Description
|
||||
#endregion
|
||||
|
||||
#region Static library display functions
|
||||
|
||||
public static (string mouseoverText, Bitmap buttonImage) GetLiberateDisplay(LiberatedState liberatedStatus, PdfState pdfStatus)
|
||||
{
|
||||
get
|
||||
string text;
|
||||
Bitmap image;
|
||||
|
||||
// get mouseover text
|
||||
{
|
||||
// HtmlAgilityPack is expensive. cache results
|
||||
if (descriptionCache is null)
|
||||
var libState = liberatedStatus switch
|
||||
{
|
||||
if (book.Description is null)
|
||||
descriptionCache = "";
|
||||
else
|
||||
{
|
||||
var doc = new HtmlAgilityPack.HtmlDocument();
|
||||
doc.LoadHtml(book.Description);
|
||||
var noHtml = doc.DocumentNode.InnerText;
|
||||
descriptionCache
|
||||
= noHtml.Length < 63
|
||||
? noHtml
|
||||
: noHtml.Substring(0, 60) + "...";
|
||||
}
|
||||
}
|
||||
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
|
||||
{
|
||||
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";
|
||||
|
||||
return descriptionCache;
|
||||
}
|
||||
}
|
||||
|
||||
public string Category => string.Join(" > ", book.CategoriesNames);
|
||||
|
||||
// star ratings retain numeric value but display star text. this is needed because just using star text doesn't sort correctly:
|
||||
// - star
|
||||
// - star star
|
||||
// - star 1/2
|
||||
|
||||
public string Product_Rating
|
||||
{
|
||||
get
|
||||
// get image
|
||||
{
|
||||
displayValues[nameof(Product_Rating)] = starString(book.Rating);
|
||||
return firstScore(book.Rating);
|
||||
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");
|
||||
|
||||
image = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}");
|
||||
}
|
||||
|
||||
return (text, image);
|
||||
}
|
||||
|
||||
public string Purchase_Date
|
||||
/// <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 :
|
||||
noHtml.Substring(0, 60) + "...";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This information should not change during <see cref="GridEntry"/> lifetime, so call only once.
|
||||
/// </summary>
|
||||
private static string GetMiscDisplay(LibraryBook libraryBook)
|
||||
{
|
||||
get
|
||||
{
|
||||
displayValues[nameof(Purchase_Date)] = libraryBook.DateAdded.ToString("d");
|
||||
return libraryBook.DateAdded.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
// 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;
|
||||
details.Add($"Account: {locale} - {acct}");
|
||||
|
||||
if (libraryBook.Book.HasPdf)
|
||||
details.Add("Has PDF");
|
||||
if (libraryBook.Book.IsAbridged)
|
||||
details.Add("Abridged");
|
||||
if (libraryBook.Book.DatePublished.HasValue)
|
||||
details.Add($"Date pub'd: {libraryBook.Book.DatePublished.Value:MM/dd/yyyy}");
|
||||
// this goes last since it's most likely to have a line-break
|
||||
if (!string.IsNullOrWhiteSpace(libraryBook.Book.Publisher))
|
||||
details.Add($"Pub: {libraryBook.Book.Publisher.Trim()}");
|
||||
|
||||
if (!details.Any())
|
||||
return "[details not imported]";
|
||||
|
||||
return string.Join("\r\n", details);
|
||||
}
|
||||
|
||||
public string My_Rating
|
||||
{
|
||||
get
|
||||
{
|
||||
displayValues[nameof(My_Rating)] = starString(book.UserDefinedItem.Rating);
|
||||
return firstScore(book.UserDefinedItem.Rating);
|
||||
}
|
||||
}
|
||||
private static string GetStarString(Rating rating)
|
||||
=> (rating?.FirstScore != null && rating?.FirstScore > 0f)
|
||||
? rating?.ToStarString()
|
||||
: "";
|
||||
|
||||
private string starString(Rating rating)
|
||||
=> (rating?.FirstScore != null && rating?.FirstScore > 0f)
|
||||
? rating?.ToStarString()
|
||||
: "";
|
||||
private string firstScore(Rating rating) => rating?.FirstScore.ToString("0.0");
|
||||
|
||||
// max 5 text rows
|
||||
public string Misc
|
||||
{
|
||||
get
|
||||
{
|
||||
var details = new List<string>();
|
||||
|
||||
var locale
|
||||
= string.IsNullOrWhiteSpace(book.Locale)
|
||||
? "[unknown]"
|
||||
: book.Locale;
|
||||
var acct
|
||||
= string.IsNullOrWhiteSpace(libraryBook.Account)
|
||||
? "[unknown]"
|
||||
: libraryBook.Account;
|
||||
details.Add($"Account: {locale} - {acct}");
|
||||
|
||||
if (book.HasPdf)
|
||||
details.Add("Has PDF");
|
||||
if (book.IsAbridged)
|
||||
details.Add("Abridged");
|
||||
if (book.DatePublished.HasValue)
|
||||
details.Add($"Date pub'd: {book.DatePublished.Value:MM/dd/yyyy}");
|
||||
// this goes last since it's most likely to have a line-break
|
||||
if (!string.IsNullOrWhiteSpace(book.Publisher))
|
||||
details.Add($"Pub: {book.Publisher.Trim()}");
|
||||
|
||||
if (!details.Any())
|
||||
return "[details not imported]";
|
||||
|
||||
return string.Join("\r\n", details);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue