Simplify EHentaiUpdateHelper

This commit is contained in:
Jobobby04 2022-12-15 21:06:24 -05:00
parent 755f3377ad
commit a280a5ed77
3 changed files with 92 additions and 102 deletions

View File

@ -263,8 +263,9 @@ class MangaInfoScreenModel(
if (chapters.isNotEmpty() && manga.isEhBasedManga() && DebugToggles.ENABLE_EXH_ROOT_REDIRECT.enabled) {
// Check for gallery in library and accept manga with lowest id
// Find chapters sharing same root
updateHelper.findAcceptedRootAndDiscardOthers(manga.source, chapters)
.onEach { (acceptedChain, _) ->
launchIO {
try {
val (acceptedChain) = updateHelper.findAcceptedRootAndDiscardOthers(manga.source, chapters)
// Redirect if we are not the accepted root
if (manga.id != acceptedChain.manga.id && acceptedChain.manga.favorite) {
// Update if any of our chapters are not in accepted manga's chapters
@ -273,7 +274,10 @@ class MangaInfoScreenModel(
EXHRedirect(acceptedChain.manga.id),
)
}
}.launchIn(coroutineScope)
} catch (e: Exception) {
logcat(LogPriority.ERROR, e) { "Error loading accepted chapter chain" }
}
}
}
}
.combine(

View File

@ -20,9 +20,6 @@ import eu.kanade.domain.manga.model.MangaUpdate
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import uy.kohesive.injekt.injectLazy
import java.io.File
@ -50,16 +47,13 @@ class EHentaiUpdateHelper(context: Context) {
*
* @return Triple<Accepted, Discarded, HasNew>
*/
fun findAcceptedRootAndDiscardOthers(sourceId: Long, chapters: List<Chapter>): Flow<Triple<ChapterChain, List<ChapterChain>, Boolean>> {
suspend fun findAcceptedRootAndDiscardOthers(sourceId: Long, chapters: List<Chapter>): Triple<ChapterChain, List<ChapterChain>, Boolean> {
// Find other chains
val chainsFlow = flowOf(chapters)
.map { chapterList ->
chapterList.flatMap { chapter ->
val chains = chapters
.flatMap { chapter ->
getChapterByUrl.await(chapter.url).map { it.mangaId }
}.distinct()
}
.map { mangaIds ->
mangaIds
.distinct()
.mapNotNull { mangaId ->
coroutineScope {
val manga = async(Dispatchers.IO) {
@ -79,23 +73,17 @@ class EHentaiUpdateHelper(context: Context) {
}
}
.filter { it.manga.source == sourceId }
}
// Accept oldest chain
val chainsWithAccepted = chainsFlow.map { chains ->
val acceptedChain = chains.minBy { it.manga.id }
val accepted = chains.minBy { it.manga.id }
acceptedChain to chains
}
return chainsWithAccepted.map { (accepted, chains) ->
val toDiscard = chains.filter { it.manga.favorite && it.manga.id != accepted.manga.id }
val mangaUpdates = mutableListOf<MangaUpdate>()
val chainsAsChapters = chains.flatMap { it.chapters }
val chainsAsHistory = chains.flatMap { it.history }
if (toDiscard.isNotEmpty()) {
return if (toDiscard.isNotEmpty()) {
// Copy chain chapters to curChapters
val (chapterUpdates, newChapters, new) = getChapterList(accepted, toDiscard, chainsAsChapters)
@ -156,7 +144,6 @@ class EHentaiUpdateHelper(context: Context) {
Triple(accepted, emptyList(), false)
}
}
}
fun getHistory(
currentChapters: List<Chapter>,

View File

@ -33,7 +33,6 @@ import exh.metadata.metadata.EHentaiSearchMetadata
import exh.util.cancellable
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.flow.toList
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
@ -165,7 +164,7 @@ class EHentaiUpdateWorker(private val context: Context, workerParams: WorkerPara
// Find accepted root and discard others
val (acceptedRoot, discardedRoots, hasNew) =
updateHelper.findAcceptedRootAndDiscardOthers(manga.source, chapters).single()
updateHelper.findAcceptedRootAndDiscardOthers(manga.source, chapters)
if ((new.isNotEmpty() && manga.id == acceptedRoot.manga.id) ||
(hasNew && updatedManga.none { it.first.id == acceptedRoot.manga.id })