Kavita/API/Extensions/ChapterListExtensions.cs
Joe Milazzo fd6ee42f5f
Reading List Polish (#1879)
* Use Reading Order to count epub pages rather than raw HTML files.

* Send email on background thread for initial invite flow.

* Reorder default writing style for new users so Horizontal is default

* Changed reading activity to use average hours read rather than events to bring more meaningful data.

* added ability to start reading incognito from the top of series detail, needs a bit of styling help though.

* Refactored extensions out into their own package, added new fields for reading list to cover total run, cbl import now takes those dates and overrides on import. Replaced many instances of numbers to be comma separated.

* Added ability to edit reading list run start and end year/month. Refactored some code for valid month/year into a helper method.

* Added a way to see the reading list's release years.

* Added some merged image code, but had to remove due to cover dimensions not fixed.

* tweaked style for accessibility mode on reading list items

* Tweaked css for non virtualized and virtualized containers

* Fixed release updates failing

* Commented out the merge code.

* Typo on words read per year

* Fixed unit tests

* Fixed virtualized scroll

* Cleanup CSS
2023-03-14 06:45:22 -07:00

46 lines
1.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using API.Entities;
using API.Helpers;
using API.Parser;
namespace API.Extensions;
public static class ChapterListExtensions
{
/// <summary>
/// Returns first chapter in the list with at least one file
/// </summary>
/// <param name="chapters"></param>
/// <returns></returns>
public static Chapter? GetFirstChapterWithFiles(this IEnumerable<Chapter> chapters)
{
return chapters.FirstOrDefault(c => c.Files.Any());
}
/// <summary>
/// Gets a single chapter (or null if doesn't exist) where Range matches the info.Chapters property. If the info
/// is <see cref="ParserInfo.IsSpecial"/> then, the filename is used to search against Range or if filename exists within Files of said Chapter.
/// </summary>
/// <param name="chapters"></param>
/// <param name="info"></param>
/// <returns></returns>
public static Chapter? GetChapterByRange(this IEnumerable<Chapter> chapters, ParserInfo info)
{
var specialTreatment = info.IsSpecialInfo();
return specialTreatment
? chapters.FirstOrDefault(c => c.Range == info.Filename || (c.Files.Select(f => f.FilePath).Contains(info.FullFilePath)))
: chapters.FirstOrDefault(c => c.Range == info.Chapters);
}
/// <summary>
/// Returns the minimum Release Year from all Chapters that meets the year requirement (>= 1000)
/// </summary>
/// <param name="chapters"></param>
/// <returns></returns>
public static int MinimumReleaseYear(this IList<Chapter> chapters)
{
return chapters.Select(v => v.ReleaseDate.Year).Where(y => NumberHelper.IsValidYear(y)).DefaultIfEmpty().Min();
}
}