Fix merged manga chapters and merged manga filtered scanlators

This commit is contained in:
Jobobby04 2023-12-27 16:30:56 -05:00
parent d82b2919f8
commit 25f94e4500
5 changed files with 65 additions and 6 deletions

View File

@ -21,4 +21,16 @@ class GetAvailableScanlators(
return repository.getScanlatorsByMangaIdAsFlow(mangaId) return repository.getScanlatorsByMangaIdAsFlow(mangaId)
.map { it.cleanupAvailableScanlators() } .map { it.cleanupAvailableScanlators() }
} }
// SY -->
suspend fun awaitMerge(mangaId: Long): Set<String> {
return repository.getScanlatorsByMergeId(mangaId)
.cleanupAvailableScanlators()
}
fun subscribeMerge(mangaId: Long): Flow<Set<String>> {
return repository.getScanlatorsByMergeIdAsFlow(mangaId)
.map { it.cleanupAvailableScanlators() }
}
// SY <--
} }

View File

@ -71,7 +71,10 @@ import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapConcat
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
@ -341,6 +344,20 @@ class MangaScreenModel(
screenModelScope.launchIO { screenModelScope.launchIO {
getAvailableScanlators.subscribe(mangaId) getAvailableScanlators.subscribe(mangaId)
.distinctUntilChanged() .distinctUntilChanged()
// SY -->
.combine(
state.map { (it as? State.Success)?.manga }
.distinctUntilChangedBy { it?.source }
.flatMapConcat {
if (it?.source == MERGED_SOURCE_ID) {
getAvailableScanlators.subscribeMerge(mangaId)
} else {
flowOf(emptySet())
}
}
) { mangaScanlators, mergeScanlators ->
mangaScanlators + mergeScanlators
} // SY <--
.collectLatest { availableScanlators -> .collectLatest { availableScanlators ->
updateSuccessState { updateSuccessState {
it.copy(availableScanlators = availableScanlators.toImmutableSet()) it.copy(availableScanlators = availableScanlators.toImmutableSet())
@ -381,7 +398,13 @@ class MangaScreenModel(
source = source, source = source,
isFromSource = isFromSource, isFromSource = isFromSource,
chapters = chapters, chapters = chapters,
availableScanlators = getAvailableScanlators.await(mangaId).toImmutableSet(), // SY -->
availableScanlators = if (manga.source == MERGED_SOURCE_ID) {
getAvailableScanlators.awaitMerge(mangaId)
} else {
getAvailableScanlators.await(mangaId)
}.toImmutableSet(),
// SY <--
excludedScanlators = getExcludedScanlators.await(mangaId).toImmutableSet(), excludedScanlators = getExcludedScanlators.await(mangaId).toImmutableSet(),
isRefreshingData = needRefreshInfo || needRefreshChapter, isRefreshingData = needRefreshInfo || needRefreshChapter,
dialog = null, dialog = null,

View File

@ -151,5 +151,17 @@ class ChapterRepositoryImpl(
) )
} }
} }
override suspend fun getScanlatorsByMergeId(mangaId: Long): List<String> {
return handler.awaitList {
chaptersQueries.getScanlatorsByMergeId(mangaId) { it.orEmpty() }
}
}
override fun getScanlatorsByMergeIdAsFlow(mangaId: Long): Flow<List<String>> {
return handler.subscribeToList {
chaptersQueries.getScanlatorsByMergeId(mangaId) { it.orEmpty() }
}
}
// SY <-- // SY <--
} }

View File

@ -71,20 +71,28 @@ AND manga_id = :mangaId;
getMergedChaptersByMangaId: getMergedChaptersByMangaId:
SELECT C.* SELECT C.*
FROM chapters C FROM (
JOIN ( SELECT manga_id,merge_id FROM merged WHERE merge_id = :mangaId
SELECT manga_id FROM merged WHERE merge_id = :mangaId
) AS M ) AS M
JOIN chapters C
ON C.manga_id = M.manga_id ON C.manga_id = M.manga_id
LEFT JOIN excluded_scanlators ES LEFT JOIN excluded_scanlators ES
ON C.manga_id = ES.manga_id ON M.merge_id = ES.manga_id
AND C.scanlator = ES.scanlator AND C.scanlator = ES.scanlator
WHERE C.manga_id = :mangaId WHERE M.merge_id = :mangaId
AND ( AND (
:applyScanlatorFilter = 0 :applyScanlatorFilter = 0
OR ES.scanlator IS NULL OR ES.scanlator IS NULL
); );
getScanlatorsByMergeId:
SELECT scanlator
FROM (
SELECT manga_id FROM merged WHERE merge_id = ?
) AS M
JOIN chapters
ON chapters.manga_id = M.manga_id;
removeChaptersWithIds: removeChaptersWithIds:
DELETE FROM chapters DELETE FROM chapters
WHERE _id IN :chapterIds; WHERE _id IN :chapterIds;

View File

@ -37,5 +37,9 @@ interface ChapterRepository {
mangaId: Long, mangaId: Long,
applyScanlatorFilter: Boolean = false, applyScanlatorFilter: Boolean = false,
): Flow<List<Chapter>> ): Flow<List<Chapter>>
suspend fun getScanlatorsByMergeId(mangaId: Long): List<String>
fun getScanlatorsByMergeIdAsFlow(mangaId: Long): Flow<List<String>>
// SY <-- // SY <--
} }