only sync back read chapters from Trackers if chapter list is incremental

also click Tracker button won't sync back if Auto sync is not turned on
This commit is contained in:
Cuong M. Tran 2024-05-30 16:47:17 +07:00
parent 6c844e9ecb
commit acfaab6102
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2

View file

@ -1,6 +1,7 @@
package eu.kanade.domain.track.interactor
import eu.kanade.domain.track.model.toDbTrack
import eu.kanade.domain.track.service.TrackPreferences
import eu.kanade.tachiyomi.data.track.Tracker
import logcat.LogPriority
import tachiyomi.core.common.util.system.logcat
@ -9,13 +10,15 @@ import tachiyomi.domain.chapter.interactor.UpdateChapter
import tachiyomi.domain.chapter.model.toChapterUpdate
import tachiyomi.domain.track.interactor.InsertTrack
import tachiyomi.domain.track.model.Track
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class SyncChapterProgressWithTrack(
private val updateChapter: UpdateChapter,
private val insertTrack: InsertTrack,
private val getChaptersByMangaId: GetChaptersByMangaId,
) {
val trackPreferences: TrackPreferences = Injekt.get()
suspend fun await(
mangaId: Long,
remoteTrack: Track,
@ -28,13 +31,24 @@ class SyncChapterProgressWithTrack(
// <-- KKM
// Current chapters in database
val sortedChapters = getChaptersByMangaId.await(mangaId)
.sortedBy { it.chapterNumber }
val dbChapters = getChaptersByMangaId.await(mangaId)
.reversed()
.filter { it.isRecognizedNumber }
// Chapters to update to follow tracker
val chapterUpdates = sortedChapters
.filter { chapter -> chapter.chapterNumber <= remoteTrack.lastChapterRead && !chapter.read }
val sortedChapters = dbChapters
.sortedBy { it.chapterNumber }
// Chapters to update to follow tracker: only continuous incremental chapters
// any abnormal chapter number will stop it from updating read status further
var lastCheckChapter: Double
var checkingChapter = 0.0
val chapterUpdates = dbChapters
.takeWhile { chapter ->
lastCheckChapter = checkingChapter
checkingChapter = chapter.chapterNumber
chapter.chapterNumber >= lastCheckChapter && chapter.chapterNumber <= remoteTrack.lastChapterRead
}
.filter { chapter -> !chapter.read }
.map { it.copy(read = true).toChapterUpdate() }
// only take into account continuous reading
@ -50,8 +64,9 @@ class SyncChapterProgressWithTrack(
insertTrack.await(updatedTrack)
}
// Update local chapters following Tracker
if (!tracker.hasNotStartedReading(remoteTrack.status))
if (trackPreferences.autoSyncReadChapters().get() && !tracker.hasNotStartedReading(remoteTrack.status)) {
updateChapter.awaitAll(chapterUpdates)
}
} catch (e: Throwable) {
logcat(LogPriority.WARN, e)
}