Refactor
This commit is contained in:
parent
9c6211e8e0
commit
ee109ba67d
@ -17,7 +17,6 @@ namespace LibationWinForms.GridView
|
|||||||
public string AudibleProductId => Book.AudibleProductId;
|
public string AudibleProductId => Book.AudibleProductId;
|
||||||
public LibraryBook LibraryBook { get; protected set; }
|
public LibraryBook LibraryBook { get; protected set; }
|
||||||
protected Book Book => LibraryBook.Book;
|
protected Book Book => LibraryBook.Book;
|
||||||
private Image _cover;
|
|
||||||
|
|
||||||
#region Model properties exposed to the view
|
#region Model properties exposed to the view
|
||||||
public Image Cover
|
public Image Cover
|
||||||
@ -50,13 +49,13 @@ namespace LibationWinForms.GridView
|
|||||||
#region Sorting
|
#region Sorting
|
||||||
|
|
||||||
public GridEntry() => _memberValues = CreateMemberValueDictionary();
|
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
|
// These methods are implementation of Dinah.Core.DataBinding.IMemberComparable
|
||||||
// Used by GridEntryBindingList for all sorting
|
// Used by GridEntryBindingList for all sorting
|
||||||
public virtual object GetMemberValue(string memberName) => _memberValues[memberName]();
|
public virtual object GetMemberValue(string memberName) => _memberValues[memberName]();
|
||||||
public IComparer GetMemberComparer(Type memberType) => _memberTypeComparers[memberType];
|
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.
|
// Instantiate comparers for every exposed member object type.
|
||||||
private static readonly Dictionary<Type, IComparer> _memberTypeComparers = new()
|
private static readonly Dictionary<Type, IComparer> _memberTypeComparers = new()
|
||||||
@ -71,10 +70,12 @@ namespace LibationWinForms.GridView
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Cover Art
|
||||||
|
|
||||||
|
private Image _cover;
|
||||||
protected void LoadCover()
|
protected void LoadCover()
|
||||||
{
|
{
|
||||||
// Get cover art. If it's default, subscribe to PictureCached
|
// 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)
|
if (isDefault)
|
||||||
@ -83,7 +84,6 @@ namespace LibationWinForms.GridView
|
|||||||
// Mutable property. Set the field so PropertyChanged isn't fired.
|
// Mutable property. Set the field so PropertyChanged isn't fired.
|
||||||
_cover = ImageReader.ToImage(picture);
|
_cover = ImageReader.ToImage(picture);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
|
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
|
||||||
{
|
{
|
||||||
@ -94,10 +94,12 @@ namespace LibationWinForms.GridView
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Static library display functions
|
#region Static library display functions
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
protected static string GetDescriptionDisplay(Book book)
|
protected static string GetDescriptionDisplay(Book book)
|
||||||
{
|
{
|
||||||
@ -116,7 +118,7 @@ namespace LibationWinForms.GridView
|
|||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <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.
|
/// Maximum of 5 text rows will fit in 80-pixel row height.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static string GetMiscDisplay(LibraryBook libraryBook)
|
protected static string GetMiscDisplay(LibraryBook libraryBook)
|
||||||
|
|||||||
@ -87,7 +87,7 @@ namespace LibationWinForms.GridView
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// don't return early if lib size == 0. this will not update correctly if all books are removed
|
// 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)
|
if (!hasBeenDisplayed)
|
||||||
{
|
{
|
||||||
@ -114,7 +114,7 @@ namespace LibationWinForms.GridView
|
|||||||
|
|
||||||
#endregion
|
#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)
|
private void productsGrid_VisibleCountChanged(object sender, int count)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -25,9 +25,10 @@ namespace LibationWinForms.GridView
|
|||||||
public new event EventHandler<ScrollEventArgs> Scroll;
|
public new event EventHandler<ScrollEventArgs> Scroll;
|
||||||
|
|
||||||
private GridEntryBindingList bindingList;
|
private GridEntryBindingList bindingList;
|
||||||
internal IEnumerable<LibraryBookEntry> GetVisible()
|
internal IEnumerable<LibraryBook> GetVisibleBooks()
|
||||||
=> bindingList
|
=> bindingList
|
||||||
.BookEntries();
|
.BookEntries()
|
||||||
|
.Select(lbe => lbe.LibraryBook);
|
||||||
|
|
||||||
public ProductsGrid()
|
public ProductsGrid()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -14,37 +14,33 @@ namespace LibationWinForms.GridView
|
|||||||
public override string DisplayTags { get; } = string.Empty;
|
public override string DisplayTags { get; } = string.Empty;
|
||||||
public override LiberateButtonStatus Liberate { get; }
|
public override LiberateButtonStatus Liberate { get; }
|
||||||
|
|
||||||
private SeriesEntry()
|
private SeriesEntry(LibraryBook parent)
|
||||||
{
|
{
|
||||||
Liberate = new LiberateButtonStatus { IsSeries = true };
|
Liberate = new LiberateButtonStatus { IsSeries = true };
|
||||||
SeriesIndex = -1;
|
SeriesIndex = -1;
|
||||||
|
LibraryBook = parent;
|
||||||
|
LoadCover();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SeriesEntry(LibraryBook parent, IEnumerable<LibraryBook> children) : this()
|
public SeriesEntry(LibraryBook parent, IEnumerable<LibraryBook> children) : this(parent)
|
||||||
{
|
{
|
||||||
Children = children
|
Children = children
|
||||||
.Select(c => new LibraryBookEntry(c) { Parent = this })
|
.Select(c => new LibraryBookEntry(c) { Parent = this })
|
||||||
.OrderBy(c => c.SeriesIndex)
|
.OrderBy(c => c.SeriesIndex)
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
UpdateSeries(parent);
|
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 } };
|
Children = new() { new LibraryBookEntry(child) { Parent = this } };
|
||||||
|
|
||||||
UpdateSeries(parent);
|
UpdateSeries(parent);
|
||||||
LoadCover();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateSeries(LibraryBook libraryBook)
|
public void UpdateSeries(LibraryBook parent)
|
||||||
{
|
{
|
||||||
LibraryBook = libraryBook;
|
LibraryBook = parent;
|
||||||
|
|
||||||
// Immutable properties
|
|
||||||
{
|
|
||||||
Title = Book.Title;
|
Title = Book.Title;
|
||||||
Series = Book.SeriesNames();
|
Series = Book.SeriesNames();
|
||||||
MyRating = Book.UserDefinedItem.Rating?.ToStarString()?.DefaultIfNullOrWhiteSpace("");
|
MyRating = Book.UserDefinedItem.Rating?.ToStarString()?.DefaultIfNullOrWhiteSpace("");
|
||||||
@ -53,13 +49,12 @@ namespace LibationWinForms.GridView
|
|||||||
Authors = Book.AuthorNames();
|
Authors = Book.AuthorNames();
|
||||||
Narrators = Book.NarratorNames();
|
Narrators = Book.NarratorNames();
|
||||||
Category = string.Join(" > ", Book.CategoriesNames());
|
Category = string.Join(" > ", Book.CategoriesNames());
|
||||||
Misc = GetMiscDisplay(libraryBook);
|
Misc = GetMiscDisplay(LibraryBook);
|
||||||
LongDescription = GetDescriptionDisplay(Book);
|
LongDescription = GetDescriptionDisplay(Book);
|
||||||
Description = TrimTextToWord(LongDescription, 62);
|
Description = TrimTextToWord(LongDescription, 62);
|
||||||
|
|
||||||
int bookLenMins = Children.Sum(c => c.LibraryBook.Book.LengthInMinutes);
|
int bookLenMins = Children.Sum(c => c.LibraryBook.Book.LengthInMinutes);
|
||||||
Length = bookLenMins == 0 ? "" : $"{bookLenMins / 60} hr {bookLenMins % 60} min";
|
Length = bookLenMins == 0 ? "" : $"{bookLenMins / 60} hr {bookLenMins % 60} min";
|
||||||
}
|
|
||||||
|
|
||||||
NotifyPropertyChanged();
|
NotifyPropertyChanged();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user