This commit is contained in:
Michael Bucari-Tovo 2022-06-08 08:39:59 -06:00
parent 9c6211e8e0
commit ee109ba67d
4 changed files with 39 additions and 41 deletions

View File

@ -17,7 +17,6 @@ namespace LibationWinForms.GridView
public string AudibleProductId => Book.AudibleProductId;
public LibraryBook LibraryBook { get; protected set; }
protected Book Book => LibraryBook.Book;
private Image _cover;
#region Model properties exposed to the view
public Image Cover
@ -50,13 +49,13 @@ namespace LibationWinForms.GridView
#region Sorting
public GridEntry() => _memberValues = CreateMemberValueDictionary();
private Dictionary<string, Func<object>> _memberValues { get; set; }
protected abstract Dictionary<string, Func<object>> CreateMemberValueDictionary();
// These methods are implementation of Dinah.Core.DataBinding.IMemberComparable
// Used by GridEntryBindingList for all sorting
public virtual object GetMemberValue(string memberName) => _memberValues[memberName]();
public IComparer GetMemberComparer(Type memberType) => _memberTypeComparers[memberType];
protected abstract Dictionary<string, Func<object>> CreateMemberValueDictionary();
private Dictionary<string, Func<object>> _memberValues { get; set; }
// Instantiate comparers for every exposed member object type.
private static readonly Dictionary<Type, IComparer> _memberTypeComparers = new()
@ -71,18 +70,19 @@ namespace LibationWinForms.GridView
#endregion
#region Cover Art
private Image _cover;
protected void LoadCover()
{
// Get cover art. If it's default, subscribe to PictureCached
{
(bool isDefault, byte[] picture) = PictureStorage.GetPicture(new PictureDefinition(Book.PictureId, PictureSize._80x80));
(bool isDefault, byte[] picture) = PictureStorage.GetPicture(new PictureDefinition(Book.PictureId, PictureSize._80x80));
if (isDefault)
PictureStorage.PictureCached += PictureStorage_PictureCached;
if (isDefault)
PictureStorage.PictureCached += PictureStorage_PictureCached;
// Mutable property. Set the field so PropertyChanged isn't fired.
_cover = ImageReader.ToImage(picture);
}
// Mutable property. Set the field so PropertyChanged isn't fired.
_cover = ImageReader.ToImage(picture);
}
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
@ -94,10 +94,12 @@ namespace LibationWinForms.GridView
}
}
#endregion
#region Static library display functions
/// <summary>
/// This information should not change during <see cref="LibraryBookEntry"/> lifetime, so call only once.
/// This information should not change during <see cref="GridEntry"/> lifetime, so call only once.
/// </summary>
protected static string GetDescriptionDisplay(Book book)
{
@ -116,7 +118,7 @@ namespace LibationWinForms.GridView
/// <summary>
/// This information should not change during <see cref="LibraryBookEntry"/> lifetime, so call only once.
/// 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>
protected static string GetMiscDisplay(LibraryBook libraryBook)

View File

@ -87,7 +87,7 @@ namespace LibationWinForms.GridView
try
{
// don't return early if lib size == 0. this will not update correctly if all books are removed
var lib = DbContexts.GetLibrary_Flat_NoTracking();
var lib = DbContexts.GetLibrary_Flat_NoTracking(includeParents: true);
if (!hasBeenDisplayed)
{
@ -114,7 +114,7 @@ namespace LibationWinForms.GridView
#endregion
internal List<LibraryBook> GetVisible() => productsGrid.GetVisible().Select(v => v.LibraryBook).ToList();
internal List<LibraryBook> GetVisible() => productsGrid.GetVisibleBooks().ToList();
private void productsGrid_VisibleCountChanged(object sender, int count)
{

View File

@ -25,9 +25,10 @@ namespace LibationWinForms.GridView
public new event EventHandler<ScrollEventArgs> Scroll;
private GridEntryBindingList bindingList;
internal IEnumerable<LibraryBookEntry> GetVisible()
internal IEnumerable<LibraryBook> GetVisibleBooks()
=> bindingList
.BookEntries();
.BookEntries()
.Select(lbe => lbe.LibraryBook);
public ProductsGrid()
{

View File

@ -14,52 +14,47 @@ namespace LibationWinForms.GridView
public override string DisplayTags { get; } = string.Empty;
public override LiberateButtonStatus Liberate { get; }
private SeriesEntry()
private SeriesEntry(LibraryBook parent)
{
Liberate = new LiberateButtonStatus { IsSeries = true };
SeriesIndex = -1;
LibraryBook = parent;
LoadCover();
}
public SeriesEntry(LibraryBook parent, IEnumerable<LibraryBook> children) : this()
public SeriesEntry(LibraryBook parent, IEnumerable<LibraryBook> children) : this(parent)
{
Children = children
.Select(c => new LibraryBookEntry(c) { Parent = this })
.OrderBy(c => c.SeriesIndex)
.ToList();
UpdateSeries(parent);
LoadCover();
}
public SeriesEntry(LibraryBook parent, LibraryBook child) : this()
public SeriesEntry(LibraryBook parent, LibraryBook child) : this(parent)
{
Children = new() { new LibraryBookEntry(child) { Parent = this } };
UpdateSeries(parent);
LoadCover();
}
public void UpdateSeries(LibraryBook libraryBook)
public void UpdateSeries(LibraryBook parent)
{
LibraryBook = libraryBook;
LibraryBook = parent;
// Immutable properties
{
Title = Book.Title;
Series = Book.SeriesNames();
MyRating = Book.UserDefinedItem.Rating?.ToStarString()?.DefaultIfNullOrWhiteSpace("");
PurchaseDate = Children.Min(c => c.LibraryBook.DateAdded).ToString("d");
ProductRating = Book.Rating?.ToStarString()?.DefaultIfNullOrWhiteSpace("");
Authors = Book.AuthorNames();
Narrators = Book.NarratorNames();
Category = string.Join(" > ", Book.CategoriesNames());
Misc = GetMiscDisplay(libraryBook);
LongDescription = GetDescriptionDisplay(Book);
Description = TrimTextToWord(LongDescription, 62);
Title = Book.Title;
Series = Book.SeriesNames();
MyRating = Book.UserDefinedItem.Rating?.ToStarString()?.DefaultIfNullOrWhiteSpace("");
PurchaseDate = Children.Min(c => c.LibraryBook.DateAdded).ToString("d");
ProductRating = Book.Rating?.ToStarString()?.DefaultIfNullOrWhiteSpace("");
Authors = Book.AuthorNames();
Narrators = Book.NarratorNames();
Category = string.Join(" > ", Book.CategoriesNames());
Misc = GetMiscDisplay(LibraryBook);
LongDescription = GetDescriptionDisplay(Book);
Description = TrimTextToWord(LongDescription, 62);
int bookLenMins = Children.Sum(c => c.LibraryBook.Book.LengthInMinutes);
Length = bookLenMins == 0 ? "" : $"{bookLenMins / 60} hr {bookLenMins % 60} min";
}
int bookLenMins = Children.Sum(c => c.LibraryBook.Book.LengthInMinutes);
Length = bookLenMins == 0 ? "" : $"{bookLenMins / 60} hr {bookLenMins % 60} min";
NotifyPropertyChanged();
}