From 488d8ab8cfbee2cab6121154b546f4764852f088 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 29 Aug 2022 15:36:56 -0400 Subject: [PATCH] Update chapter progress upon registering tracker (closes #7209) Co-authored-by: Saud-97 --- .../tachiyomi/ui/manga/MangaPresenter.kt | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index 71a38fe5b3..deeac7c667 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -936,20 +936,31 @@ class MangaPresenter( item.manga_id = successState.manga.id launchIO { try { - val allChapters = successState.chapters - .map { it.chapter.toDbChapter() } + val allChapters = successState.chapters.map { it.chapter } val hasReadChapters = allChapters.any { it.read } service.bind(item, hasReadChapters) item.toDomainTrack(idRequired = false)?.let { track -> insertTrack.await(track) - (service as? EnhancedTrackService)?.let { _ -> - val chapters = successState.chapters - .map { it.chapter } + // Update chapter progress if newer chapters marked read locally + if (hasReadChapters) { + val latestLocalReadChapterNumber = allChapters + .sortedBy { it.chapterNumber } + .takeWhile { it.read } + .lastOrNull() + ?.chapterNumber?.toDouble() ?: -1.0 - syncChaptersWithTrackServiceTwoWay - .await(chapters, track, service) + if (latestLocalReadChapterNumber >= track.lastChapterRead) { + val updatedTrack = track.copy( + lastChapterRead = latestLocalReadChapterNumber, + ) + setTrackerLastChapterRead(TrackItem(updatedTrack.toDbTrack(), service), latestLocalReadChapterNumber.toInt()) + } + } + + if (service is EnhancedTrackService) { + syncChaptersWithTrackServiceTwoWay.await(allChapters, track, service) } } } catch (e: Throwable) {