Update chapter progress upon registering tracker (closes #7209)

Co-authored-by: Saud-97 <Saud-97@users.noreply.github.com>
This commit is contained in:
arkon 2022-08-29 15:36:56 -04:00
parent 8efb20439a
commit 488d8ab8cf

View File

@ -936,20 +936,31 @@ class MangaPresenter(
item.manga_id = successState.manga.id item.manga_id = successState.manga.id
launchIO { launchIO {
try { try {
val allChapters = successState.chapters val allChapters = successState.chapters.map { it.chapter }
.map { it.chapter.toDbChapter() }
val hasReadChapters = allChapters.any { it.read } val hasReadChapters = allChapters.any { it.read }
service.bind(item, hasReadChapters) service.bind(item, hasReadChapters)
item.toDomainTrack(idRequired = false)?.let { track -> item.toDomainTrack(idRequired = false)?.let { track ->
insertTrack.await(track) insertTrack.await(track)
(service as? EnhancedTrackService)?.let { _ -> // Update chapter progress if newer chapters marked read locally
val chapters = successState.chapters if (hasReadChapters) {
.map { it.chapter } val latestLocalReadChapterNumber = allChapters
.sortedBy { it.chapterNumber }
.takeWhile { it.read }
.lastOrNull()
?.chapterNumber?.toDouble() ?: -1.0
syncChaptersWithTrackServiceTwoWay if (latestLocalReadChapterNumber >= track.lastChapterRead) {
.await(chapters, track, service) 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) { } catch (e: Throwable) {