Skip to content

Commit

Permalink
Fix checking for new chapters in some cases (#1212, #1195, #1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Dec 18, 2024
1 parent e75035b commit 734846a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.koitharu.kotatsu.tracker.domain

import android.util.Log
import coil3.request.CachePolicy
import org.koitharu.kotatsu.BuildConfig
import org.koitharu.kotatsu.core.model.getPreferredBranch
import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.core.parser.CachingMangaRepository
Expand All @@ -11,6 +13,7 @@ import org.koitharu.kotatsu.core.util.ext.toInstantOrNull
import org.koitharu.kotatsu.history.data.HistoryRepository
import org.koitharu.kotatsu.local.data.LocalMangaRepository
import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.util.findById
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import org.koitharu.kotatsu.tracker.domain.model.MangaTracking
import org.koitharu.kotatsu.tracker.domain.model.MangaUpdates
Expand Down Expand Up @@ -45,8 +48,9 @@ class CheckNewChaptersUseCase @Inject constructor(
runCatchingCancellable {
repository.updateTracks()
val details = getFullManga(manga)
val chapters = details.chapters ?: return@withLock
val track = repository.getTrackOrNull(manga) ?: return@withLock
val branch = checkNotNull(details.chapters?.findById(currentChapterId)).branch
val chapters = details.getChapters(branch)
val chapterIndex = chapters.indexOfFirst { x -> x.id == currentChapterId }
val lastNewChapterIndex = chapters.size - track.newChapters
val lastChapter = chapters.lastOrNull()
Expand All @@ -70,7 +74,7 @@ class CheckNewChaptersUseCase @Inject constructor(

private suspend fun invokeImpl(track: MangaTracking): MangaUpdates = runCatchingCancellable {
val details = getFullManga(track.manga)
compare(track, details, getBranch(details))
compare(track, details, getBranch(details, track.lastChapterId))
}.getOrElse { error ->
MangaUpdates.Failure(
manga = track.manga,
Expand All @@ -80,9 +84,17 @@ class CheckNewChaptersUseCase @Inject constructor(
repository.saveUpdates(updates)
}

private suspend fun getBranch(manga: Manga): String? {
val history = historyRepository.getOne(manga)
return manga.getPreferredBranch(history)
private suspend fun getBranch(manga: Manga, trackChapterId: Long): String? {
historyRepository.getOne(manga)?.let {
manga.chapters?.findById(it.chapterId)
}?.let {
return it.branch
}
manga.chapters?.findById(trackChapterId)?.let {
return it.branch
}
// fallback
return manga.getPreferredBranch(null)
}

private suspend fun getFullManga(manga: Manga): Manga = when {
Expand Down Expand Up @@ -111,25 +123,29 @@ class CheckNewChaptersUseCase @Inject constructor(
private fun compare(track: MangaTracking, manga: Manga, branch: String?): MangaUpdates.Success {
if (track.isEmpty()) {
// first check or manga was empty on last check
return MangaUpdates.Success(manga, emptyList(), isValid = false)
return MangaUpdates.Success(manga, branch, emptyList(), isValid = false)
}
val chapters = requireNotNull(manga.getChapters(branch))
if (BuildConfig.DEBUG && chapters.findById(track.lastChapterId) == null) {
Log.e("Tracker", "Chapter ${track.lastChapterId} not found")
}
val newChapters = chapters.takeLastWhile { x -> x.id != track.lastChapterId }
return when {
newChapters.isEmpty() -> {
MangaUpdates.Success(
manga = manga,
branch = branch,
newChapters = emptyList(),
isValid = chapters.lastOrNull()?.id == track.lastChapterId,
)
}

newChapters.size == chapters.size -> {
MangaUpdates.Success(manga, emptyList(), isValid = false)
MangaUpdates.Success(manga, branch, emptyList(), isValid = false)
}

else -> {
MangaUpdates.Success(manga, newChapters, isValid = true)
MangaUpdates.Success(manga, branch, newChapters, isValid = true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ class TrackingRepository @Inject constructor(
}

private fun TrackEntity.mergeWith(updates: MangaUpdates): TrackEntity {
val chapters = updates.manga.chapters.orEmpty()
return when (updates) {
is MangaUpdates.Failure -> TrackEntity(
mangaId = mangaId,
Expand All @@ -230,7 +229,7 @@ class TrackingRepository @Inject constructor(

is MangaUpdates.Success -> TrackEntity(
mangaId = mangaId,
lastChapterId = chapters.lastOrNull()?.id ?: NO_ID,
lastChapterId = updates.manga.getChapters(updates.branch).lastOrNull()?.id ?: NO_ID,
newChapters = if (updates.isValid) newChapters + updates.newChapters.size else 0,
lastCheckTime = System.currentTimeMillis(),
lastChapterDate = updates.lastChapterDate().ifZero { lastChapterDate },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sealed interface MangaUpdates {

data class Success(
override val manga: Manga,
val branch: String?,
val newChapters: List<MangaChapter>,
val isValid: Boolean,
) : MangaUpdates {
Expand Down

0 comments on commit 734846a

Please sign in to comment.