From d986c808e3a668aab419eb42a506c0dc7fbd9ea7 Mon Sep 17 00:00:00 2001 From: glax Date: Wed, 3 Jan 2024 18:37:12 +0100 Subject: [PATCH] Chapter as Comparable --- Tranga/Chapter.cs | 29 +++++++++++++++++++++++- Tranga/MangaConnectors/Bato.cs | 2 +- Tranga/MangaConnectors/MangaConnector.cs | 2 +- Tranga/MangaConnectors/MangaDex.cs | 2 +- Tranga/MangaConnectors/MangaKatana.cs | 2 +- Tranga/MangaConnectors/MangaLife.cs | 2 +- Tranga/MangaConnectors/Manganato.cs | 7 +----- Tranga/MangaConnectors/Mangasee.cs | 2 +- Tranga/MangaConnectors/Mangaworld.cs | 2 +- 9 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Tranga/Chapter.cs b/Tranga/Chapter.cs index 4af7722..ed45811 100644 --- a/Tranga/Chapter.cs +++ b/Tranga/Chapter.cs @@ -7,7 +7,7 @@ namespace Tranga; /// Has to be Part of a publication /// Includes the Chapter-Name, -VolumeNumber, -ChapterNumber, the location of the chapter on the internet and the saveName of the local file. /// -public readonly struct Chapter +public readonly struct Chapter : IComparable { // ReSharper disable once MemberCanBePrivate.Global public Manga parentManga { get; } @@ -41,6 +41,33 @@ public readonly struct Chapter return $"Chapter {parentManga.sortName} {parentManga.internalId} {chapterNumber} {name}"; } + public int CompareTo(object? obj) + { + if (obj is Chapter otherChapter) + { + if (float.TryParse(volumeNumber, GlobalBase.numberFormatDecimalPoint, out float volumeNumberFloat) && + float.TryParse(chapterNumber, GlobalBase.numberFormatDecimalPoint, out float chapterNumberFloat) && + float.TryParse(otherChapter.volumeNumber, GlobalBase.numberFormatDecimalPoint, + out float otherVolumeNumberFloat) && + float.TryParse(otherChapter.chapterNumber, GlobalBase.numberFormatDecimalPoint, + out float otherChapterNumberFloat)) + { + + switch (volumeNumberFloat.CompareTo(otherVolumeNumberFloat)) + { + case < 0: + return -1; + case > 0: + return 1; + default: + return chapterNumberFloat.CompareTo(otherChapterNumberFloat); + } + } + else throw new FormatException($"Value could not be parsed"); + } + throw new ArgumentException($"{obj} can not be compared to {this}"); + } + /// /// Checks if a chapter-archive is already present /// diff --git a/Tranga/MangaConnectors/Bato.cs b/Tranga/MangaConnectors/Bato.cs index 1614990..ff29f18 100644 --- a/Tranga/MangaConnectors/Bato.cs +++ b/Tranga/MangaConnectors/Bato.cs @@ -136,7 +136,7 @@ public class Bato : MangaConnector //Return Chapters ordered by Chapter-Number List chapters = ParseChaptersFromHtml(manga, requestUrl); Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); + return chapters.Order().ToArray(); } private List ParseChaptersFromHtml(Manga manga, string mangaUrl) diff --git a/Tranga/MangaConnectors/MangaConnector.cs b/Tranga/MangaConnectors/MangaConnector.cs index cfd62a6..52f80f6 100644 --- a/Tranga/MangaConnectors/MangaConnector.cs +++ b/Tranga/MangaConnectors/MangaConnector.cs @@ -70,7 +70,7 @@ public abstract class MangaConnector : GlobalBase try { Chapter latestChapterAvailable = - allChapters.MaxBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)); + allChapters.Max(); manga.latestChapterAvailable = Convert.ToSingle(latestChapterAvailable.chapterNumber, numberFormatDecimalPoint); } diff --git a/Tranga/MangaConnectors/MangaDex.cs b/Tranga/MangaConnectors/MangaDex.cs index 27a0403..eb12f8b 100644 --- a/Tranga/MangaConnectors/MangaDex.cs +++ b/Tranga/MangaConnectors/MangaDex.cs @@ -253,7 +253,7 @@ public class MangaDex : MangaConnector //Return Chapters ordered by Chapter-Number Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); + return chapters.Order().ToArray(); } public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? progressToken = null) diff --git a/Tranga/MangaConnectors/MangaKatana.cs b/Tranga/MangaConnectors/MangaKatana.cs index 58a7529..9703934 100644 --- a/Tranga/MangaConnectors/MangaKatana.cs +++ b/Tranga/MangaConnectors/MangaKatana.cs @@ -165,7 +165,7 @@ public class MangaKatana : MangaConnector //Return Chapters ordered by Chapter-Number List chapters = ParseChaptersFromHtml(manga, requestUrl); Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); + return chapters.Order().ToArray(); } private List ParseChaptersFromHtml(Manga manga, string mangaUrl) diff --git a/Tranga/MangaConnectors/MangaLife.cs b/Tranga/MangaConnectors/MangaLife.cs index 3d0d26e..6eb2cca 100644 --- a/Tranga/MangaConnectors/MangaLife.cs +++ b/Tranga/MangaConnectors/MangaLife.cs @@ -159,7 +159,7 @@ public class MangaLife : MangaConnector } //Return Chapters ordered by Chapter-Number Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); + return chapters.Order().ToArray(); } public override HttpStatusCode DownloadChapter(Chapter chapter, ProgressToken? progressToken = null) diff --git a/Tranga/MangaConnectors/Manganato.cs b/Tranga/MangaConnectors/Manganato.cs index 9db2d11..ff301b9 100644 --- a/Tranga/MangaConnectors/Manganato.cs +++ b/Tranga/MangaConnectors/Manganato.cs @@ -152,12 +152,7 @@ public class Manganato : MangaConnector return Array.Empty(); List chapters = ParseChaptersFromHtml(manga, requestResult.htmlDocument); Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => - { - if (float.TryParse(chapter.chapterNumber, numberFormatDecimalPoint, out float chapterNumber)) - return chapterNumber; - else return 0; - }).ToArray(); + return chapters.Order().ToArray(); } private List ParseChaptersFromHtml(Manga manga, HtmlDocument document) diff --git a/Tranga/MangaConnectors/Mangasee.cs b/Tranga/MangaConnectors/Mangasee.cs index ed92f12..a3d0955 100644 --- a/Tranga/MangaConnectors/Mangasee.cs +++ b/Tranga/MangaConnectors/Mangasee.cs @@ -206,7 +206,7 @@ public class Mangasee : MangaConnector //Return Chapters ordered by Chapter-Number Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); + return chapters.Order().ToArray(); } catch (HttpRequestException e) { diff --git a/Tranga/MangaConnectors/Mangaworld.cs b/Tranga/MangaConnectors/Mangaworld.cs index a64a656..a669072 100644 --- a/Tranga/MangaConnectors/Mangaworld.cs +++ b/Tranga/MangaConnectors/Mangaworld.cs @@ -141,7 +141,7 @@ public class Mangaworld: MangaConnector return Array.Empty(); List chapters = ParseChaptersFromHtml(manga, requestResult.htmlDocument); Log($"Got {chapters.Count} chapters. {manga}"); - return chapters.OrderBy(chapter => Convert.ToSingle(chapter.chapterNumber, numberFormatDecimalPoint)).ToArray(); + return chapters.Order().ToArray(); } private List ParseChaptersFromHtml(Manga manga, HtmlDocument document)