Fetching related mangas from Source

This commit is contained in:
Cuong M. Tran 2024-04-25 11:25:06 +07:00
parent 0df6a7fb09
commit 89f8723d7e
No known key found for this signature in database
GPG key ID: 733AA7624B9315C2
4 changed files with 155 additions and 1 deletions

View file

@ -24,6 +24,7 @@ import eu.kanade.domain.manga.model.PagePreview
import eu.kanade.domain.manga.model.chaptersFiltered
import eu.kanade.domain.manga.model.copyFrom
import eu.kanade.domain.manga.model.downloadedFilter
import eu.kanade.domain.manga.model.toDomainManga
import eu.kanade.domain.manga.model.toSManga
import eu.kanade.domain.source.service.SourcePreferences
import eu.kanade.domain.track.interactor.AddTracks
@ -395,6 +396,9 @@ class MangaScreenModel(
val needRefreshInfo = !manga.initialized
val needRefreshChapter = chapters.isEmpty()
// KMK -->
val needRefreshRelatedMangas = relatedMangas.isEmpty()
// KMK <--
// Show what we have earlier
mutableState.update {
@ -412,7 +416,7 @@ class MangaScreenModel(
}.toImmutableSet(),
// SY <--
excludedScanlators = getExcludedScanlators.await(mangaId).toImmutableSet(),
isRefreshingData = needRefreshInfo || needRefreshChapter,
isRefreshingData = needRefreshInfo || needRefreshChapter /* KMK --> */|| needRefreshRelatedMangas,/* KMK <-- */
dialog = null,
// SY -->
showRecommendationsInOverflow = uiPreferences.recommendsInOverflow().get(),
@ -440,6 +444,9 @@ class MangaScreenModel(
val fetchFromSourceTasks = listOf(
async { if (needRefreshInfo) fetchMangaFromSource() },
async { if (needRefreshChapter) fetchChaptersFromSource() },
// KMK -->
async { if (needRefreshRelatedMangas) fetchRelatedMangasFromSource() },
// KMK <--
)
fetchFromSourceTasks.awaitAll()
}
@ -455,6 +462,9 @@ class MangaScreenModel(
val fetchFromSourceTasks = listOf(
async { fetchMangaFromSource(manualFetch) },
async { fetchChaptersFromSource(manualFetch) },
// KMK -->
async { fetchRelatedMangasFromSource() },
// KMK <--
)
fetchFromSourceTasks.awaitAll()
updateSuccessState { it.copy(isRefreshingData = false) }
@ -1090,6 +1100,41 @@ class MangaScreenModel(
}
}
// KMK -->
/**
* Requests an list of related mangas from the source.
*/
private suspend fun fetchRelatedMangasFromSource() {
val state = successState ?: return
try {
withIOContext {
if (state.source !is MergedSource) {
val relatedMangas = if (state.source.supportsRelatedMangas)
withIOContext {
state.source.getRelatedMangaList(state.manga.toSManga())
.map {
networkToLocalManga.await(it.toDomainManga(state.source.id))
}
}
else null
updateSuccessState { it.copy(relatedMangas = relatedMangas, isRefreshingData = false) }
} else {
throw UnsupportedOperationException("Fetching related titles for merged entry is not supported")
}
}
} catch (e: Throwable) {
logcat(LogPriority.ERROR, e)
val message = with(context) { e.formattedMessage }
screenModelScope.launch {
snackbarHostState.showSnackbar(message = message)
}
val newManga = mangaRepository.getMangaById(mangaId)
updateSuccessState { it.copy(manga = newManga, isRefreshingData = false) }
}
}
// KMK <--
/**
* @throws IllegalStateException if the swipe action is [LibraryPreferences.ChapterSwipeAction.Disabled]
*/

View file

@ -48,6 +48,27 @@ interface Source {
return fetchChapterList(manga).awaitSingle()
}
// KMK -->
/**
* Whether the source has support for related mangas.
*
* @since extensions-lib 1.6
*/
val supportsRelatedMangas: Boolean get() = false
/**
* Get all the available related mangas for a manga.
*
* @since extensions-lib 1.6
* @param manga the current manga to get related mangas.
* @return the related mangas for the current manga.
*/
@Suppress("DEPRECATION")
suspend fun getRelatedMangaList(manga: SManga): List<SManga> {
return fetchRelatedMangaList(manga).awaitSingle()
}
// KMK <--
/**
* Get the list of pages a chapter has. Pages should be returned
* in the expected order; the index is ignored.
@ -75,6 +96,22 @@ interface Source {
fun fetchChapterList(manga: SManga): Observable<List<SChapter>> =
throw IllegalStateException("Not used")
// KMK -->
/**
* Get all the available related mangas for a manga.
*
* @since extensions-lib 1.6
* @param manga the current manga to get related mangas.
* @return the related mangas for the current manga.
*/
@Deprecated(
"Use the non-RxJava API instead",
ReplaceWith("getRelatedMangaList"),
)
fun fetchRelatedMangaList(manga: SManga): Observable<List<SManga>> =
throw IllegalStateException("Not used")
// KMK <--
@Deprecated(
"Use the non-RxJava API instead",
ReplaceWith("getPageList"),

View file

@ -274,6 +274,54 @@ abstract class HttpSource : CatalogueSource {
*/
protected abstract fun mangaDetailsParse(response: Response): SManga
// KMK -->
/**
* Get all the available related mangas for a manga.
* Normally it's not needed to override this method.
*
* @param manga the current manga to get related mangas.
* @return the related mangas for the current manga.
* @throws UnsupportedOperationException if a source doesn't support related mangas.
*/
@Suppress("DEPRECATION")
override suspend fun getRelatedMangaList(manga: SManga): List<SManga> {
return fetchRelatedMangaList(manga).awaitSingle()
}
@Deprecated("Use the non-RxJava API instead", replaceWith = ReplaceWith("getRelatedMangaList"))
override fun fetchRelatedMangaList(manga: SManga): Observable<List<SManga>> {
return client.newCall(relatedMangaListRequest(manga))
.asObservableSuccess()
.map { response ->
relatedMangaListParse(response)
}
}
/**
* Returns the request for get related manga list. Override only if it's needed to override
* the url, send different headers or request method like POST.
*
* @param manga the manga to look for related mangas.
*/
protected open fun relatedMangaListRequest(manga: SManga): Request {
return GET(baseUrl + manga.url, headers)
}
/**
* Parses the response from the site and returns a list of related mangas.
*
* @param response the response from the site.
*/
protected open fun relatedMangaListParse(response: Response): List<SManga> = throw UnsupportedOperationException()
/**
* Parses the response from the site and returns a SManga Object.
*
* @param response the response from the site.
*/
protected open fun relatedMangaPageParse(response: Response): SManga = throw UnsupportedOperationException()
// KMK <--
/**
* Get all the available chapters for a manga.
* Normally it's not needed to override this method.

View file

@ -145,6 +145,30 @@ abstract class ParsedHttpSource : HttpSource() {
*/
protected abstract fun mangaDetailsParse(document: Document): SManga
// KMK -->
/**
* Parses the response from the site and returns a list of related mangas.
*
* @param response the response from the site.
*/
override fun relatedMangaListParse(response: Response): List<SManga> {
val document = response.asJsoup()
return document.select(relatedMangaListSelector()).map { relatedMangaFromElement(it) }
}
/**
* Returns the Jsoup selector that returns a list of [Element] corresponding to each related mangas.
*/
protected open fun relatedMangaListSelector(): String = popularMangaSelector()
/**
* Returns a manga from the given element.
*
* @param element an element obtained from [relatedMangaListSelector].
*/
protected open fun relatedMangaFromElement(element: Element): SManga = popularMangaFromElement(element)
// KMK <--
/**
* Parses the response from the site and returns a list of chapters.
*