Move tracker logic into own class
parent
30c0fd600f
commit
3edfd0892a
@ -1,14 +1,41 @@
|
||||
package org.koitharu.kotatsu.core.model
|
||||
|
||||
import android.os.Parcelable
|
||||
import java.util.*
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
|
||||
data class MangaTracking(
|
||||
class MangaTracking(
|
||||
val manga: Manga,
|
||||
val knownChaptersCount: Int,
|
||||
val lastChapterId: Long,
|
||||
val lastNotifiedChapterId: Long,
|
||||
val lastCheck: Date?
|
||||
)
|
||||
val lastCheck: Date?,
|
||||
) {
|
||||
|
||||
fun isEmpty(): Boolean {
|
||||
return knownChaptersCount <= 0 || lastChapterId == 0L
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as MangaTracking
|
||||
|
||||
if (manga != other.manga) return false
|
||||
if (knownChaptersCount != other.knownChaptersCount) return false
|
||||
if (lastChapterId != other.lastChapterId) return false
|
||||
if (lastNotifiedChapterId != other.lastNotifiedChapterId) return false
|
||||
if (lastCheck != other.lastCheck) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = manga.hashCode()
|
||||
result = 31 * result + knownChaptersCount
|
||||
result = 31 * result + lastChapterId.hashCode()
|
||||
result = 31 * result + lastNotifiedChapterId.hashCode()
|
||||
result = 31 * result + (lastCheck?.hashCode() ?: 0)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,132 @@
|
||||
package org.koitharu.kotatsu.tracker.domain
|
||||
|
||||
import org.koitharu.kotatsu.core.model.MangaTracking
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
import org.koitharu.kotatsu.tracker.domain.model.MangaUpdates
|
||||
|
||||
class Tracker(
|
||||
private val repository: TrackingRepository,
|
||||
) {
|
||||
|
||||
suspend fun fetchUpdates(track: MangaTracking, commit: Boolean): MangaUpdates {
|
||||
val repo = MangaRepository(track.manga.source)
|
||||
val details = repo.getDetails(track.manga)
|
||||
val chapters = details.chapters.orEmpty()
|
||||
if (track.isEmpty()) {
|
||||
// first check or manga was empty on last check
|
||||
if (commit) {
|
||||
repository.storeTrackResult(
|
||||
mangaId = track.manga.id,
|
||||
knownChaptersCount = chapters.size,
|
||||
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
|
||||
previousTrackChapterId = 0L,
|
||||
newChapters = emptyList(),
|
||||
saveTrackLog = false,
|
||||
)
|
||||
}
|
||||
return MangaUpdates(
|
||||
manga = details,
|
||||
newChapters = emptyList(),
|
||||
)
|
||||
}
|
||||
val newChapters = details.getNewChapters(track.lastChapterId)
|
||||
if (newChapters.isEmpty()) {
|
||||
if (commit) {
|
||||
repository.storeTrackResult(
|
||||
mangaId = track.manga.id,
|
||||
knownChaptersCount = chapters.size,
|
||||
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
|
||||
previousTrackChapterId = 0L,
|
||||
newChapters = emptyList(),
|
||||
saveTrackLog = false,
|
||||
)
|
||||
}
|
||||
return MangaUpdates(
|
||||
manga = details,
|
||||
newChapters = emptyList(),
|
||||
)
|
||||
}
|
||||
return when {
|
||||
|
||||
// the same chapters count
|
||||
chapters.size == track.knownChaptersCount -> {
|
||||
if (chapters.lastOrNull()?.id == track.lastChapterId) {
|
||||
// manga was not updated. skip
|
||||
MangaUpdates(
|
||||
manga = details,
|
||||
newChapters = emptyList(),
|
||||
)
|
||||
} else {
|
||||
// number of chapters still the same, bu last chapter changed.
|
||||
// maybe some chapters are removed. we need to find last known chapter
|
||||
val knownChapter = chapters.indexOfLast { it.id == track.lastChapterId }
|
||||
if (knownChapter == -1) {
|
||||
// confuse. reset anything
|
||||
if (commit) {
|
||||
repository.storeTrackResult(
|
||||
mangaId = track.manga.id,
|
||||
knownChaptersCount = chapters.size,
|
||||
lastChapterId = chapters.lastOrNull()?.id ?: 0L,
|
||||
previousTrackChapterId = 0L,
|
||||
newChapters = emptyList(),
|
||||
saveTrackLog = false,
|
||||
)
|
||||
}
|
||||
MangaUpdates(
|
||||
manga = details,
|
||||
newChapters = emptyList(),
|
||||
)
|
||||
} else {
|
||||
val newChapters = chapters.takeLast(chapters.size - knownChapter + 1)
|
||||
if (commit) {
|
||||
repository.storeTrackResult(
|
||||
mangaId = track.manga.id,
|
||||
knownChaptersCount = knownChapter + 1,
|
||||
lastChapterId = track.lastChapterId,
|
||||
previousTrackChapterId = track.lastNotifiedChapterId,
|
||||
newChapters = newChapters,
|
||||
saveTrackLog = true,
|
||||
)
|
||||
}
|
||||
MangaUpdates(
|
||||
manga = details,
|
||||
newChapters = details.getNewChapters(track.lastNotifiedChapterId),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
val newChapters = chapters.takeLast(chapters.size - track.knownChaptersCount)
|
||||
if (commit) {
|
||||
repository.storeTrackResult(
|
||||
mangaId = track.manga.id,
|
||||
knownChaptersCount = track.knownChaptersCount,
|
||||
lastChapterId = track.lastChapterId,
|
||||
previousTrackChapterId = track.lastNotifiedChapterId,
|
||||
newChapters = newChapters,
|
||||
saveTrackLog = true,
|
||||
)
|
||||
}
|
||||
MangaUpdates(
|
||||
manga = details,
|
||||
newChapters = details.getNewChapters(track.lastNotifiedChapterId),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Manga.getNewChapters(lastChapterId: Long): List<MangaChapter> {
|
||||
val chapters = chapters ?: return emptyList()
|
||||
if (lastChapterId == 0L) {
|
||||
return emptyList()
|
||||
}
|
||||
val raw = chapters.takeLastWhile { x -> x.id != lastChapterId }
|
||||
return if (raw.isEmpty() || raw.size == chapters.size) {
|
||||
emptyList()
|
||||
} else {
|
||||
raw
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package org.koitharu.kotatsu.tracker.domain.model
|
||||
|
||||
import org.koitharu.kotatsu.parsers.model.Manga
|
||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||
|
||||
class MangaUpdates(
|
||||
val manga: Manga,
|
||||
val newChapters: List<MangaChapter>,
|
||||
)
|
||||
Loading…
Reference in New Issue