|
|
|
@ -11,10 +11,12 @@ import kotlinx.coroutines.flow.Flow
|
|
|
|
import kotlinx.coroutines.flow.flow
|
|
|
|
import kotlinx.coroutines.flow.flow
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
import org.koitharu.kotatsu.core.db.MangaDatabase
|
|
|
|
import org.koitharu.kotatsu.core.db.MangaDatabase
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.model.findById
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.findKeyByValue
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.findKeyByValue
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.sanitize
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.sanitize
|
|
|
|
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
|
|
|
import org.koitharu.kotatsu.parsers.model.Manga
|
|
|
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
|
|
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
|
|
|
import org.koitharu.kotatsu.scrobbling.common.data.ScrobblerRepository
|
|
|
|
import org.koitharu.kotatsu.scrobbling.common.data.ScrobblerRepository
|
|
|
|
import org.koitharu.kotatsu.scrobbling.common.data.ScrobblingEntity
|
|
|
|
import org.koitharu.kotatsu.scrobbling.common.data.ScrobblingEntity
|
|
|
|
@ -30,6 +32,7 @@ abstract class Scrobbler(
|
|
|
|
protected val db: MangaDatabase,
|
|
|
|
protected val db: MangaDatabase,
|
|
|
|
val scrobblerService: ScrobblerService,
|
|
|
|
val scrobblerService: ScrobblerService,
|
|
|
|
private val repository: ScrobblerRepository,
|
|
|
|
private val repository: ScrobblerRepository,
|
|
|
|
|
|
|
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
|
|
private val infoCache = LongSparseArray<ScrobblerMangaInfo>()
|
|
|
|
private val infoCache = LongSparseArray<ScrobblerMangaInfo>()
|
|
|
|
@ -68,9 +71,23 @@ abstract class Scrobbler(
|
|
|
|
repository.createRate(mangaId, targetId)
|
|
|
|
repository.createRate(mangaId, targetId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun scrobble(mangaId: Long, chapter: MangaChapter) {
|
|
|
|
suspend fun scrobble(manga: Manga, chapterId: Long) {
|
|
|
|
val entity = db.getScrobblingDao().find(scrobblerService.id, mangaId) ?: return
|
|
|
|
var chapters = manga.chapters
|
|
|
|
repository.updateRate(entity.id, entity.mangaId, chapter)
|
|
|
|
if (chapters.isNullOrEmpty()) {
|
|
|
|
|
|
|
|
chapters = mangaRepositoryFactory.create(manga.source).getDetails(manga).chapters
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
requireNotNull(chapters)
|
|
|
|
|
|
|
|
val chapter = checkNotNull(chapters.findById(chapterId)) {
|
|
|
|
|
|
|
|
"Chapter $chapterId not found in this manga"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
val number = if (chapter.number > 0f) {
|
|
|
|
|
|
|
|
chapter.number.toInt()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
chapters = chapters.filter { x -> x.branch == chapter.branch }
|
|
|
|
|
|
|
|
chapters.indexOf(chapter) + 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
val entity = db.getScrobblingDao().find(scrobblerService.id, manga.id) ?: return
|
|
|
|
|
|
|
|
repository.updateRate(entity.id, entity.mangaId, number)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun getScrobblingInfoOrNull(mangaId: Long): ScrobblingInfo? {
|
|
|
|
suspend fun getScrobblingInfoOrNull(mangaId: Long): ScrobblingInfo? {
|
|
|
|
@ -137,9 +154,9 @@ abstract class Scrobbler(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
suspend fun Scrobbler.tryScrobble(mangaId: Long, chapter: MangaChapter): Boolean {
|
|
|
|
suspend fun Scrobbler.tryScrobble(manga: Manga, chapterId: Long): Boolean {
|
|
|
|
return runCatchingCancellable {
|
|
|
|
return runCatchingCancellable {
|
|
|
|
scrobble(mangaId, chapter)
|
|
|
|
scrobble(manga, chapterId)
|
|
|
|
}.onFailure {
|
|
|
|
}.onFailure {
|
|
|
|
it.printStackTraceDebug()
|
|
|
|
it.printStackTraceDebug()
|
|
|
|
}.isSuccess
|
|
|
|
}.isSuccess
|
|
|
|
|