mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-07-02 05:26:05 +02:00
cc6aef693e
* Merge Latest and Browse into one * Add back Latest button * Change context to IO instead of launching a job * Use loading screen when loading initial page
63 lines
2.2 KiB
Kotlin
63 lines
2.2 KiB
Kotlin
package eu.kanade.data.source
|
|
|
|
import androidx.paging.PagingState
|
|
import eu.kanade.domain.source.model.SourcePagingSourceType
|
|
import eu.kanade.tachiyomi.source.CatalogueSource
|
|
import eu.kanade.tachiyomi.source.model.FilterList
|
|
import eu.kanade.tachiyomi.source.model.MangasPage
|
|
import eu.kanade.tachiyomi.source.model.SManga
|
|
import eu.kanade.tachiyomi.util.lang.awaitSingle
|
|
import eu.kanade.tachiyomi.util.lang.withIOContext
|
|
|
|
abstract class SourcePagingSource(
|
|
protected val source: CatalogueSource,
|
|
) : SourcePagingSourceType() {
|
|
|
|
abstract suspend fun requestNextPage(currentPage: Int): MangasPage
|
|
|
|
override suspend fun load(params: LoadParams<Long>): LoadResult<Long, SManga> {
|
|
val page = params.key ?: 1
|
|
|
|
val mangasPage = try {
|
|
withIOContext {
|
|
requestNextPage(page.toInt())
|
|
.takeIf { it.mangas.isNotEmpty() }
|
|
?: throw NoResultsException()
|
|
}
|
|
} catch (e: Exception) {
|
|
return LoadResult.Error(e)
|
|
}
|
|
|
|
return LoadResult.Page(
|
|
data = mangasPage.mangas,
|
|
prevKey = null,
|
|
nextKey = if (mangasPage.hasNextPage) page + 1 else null,
|
|
)
|
|
}
|
|
|
|
override fun getRefreshKey(state: PagingState<Long, SManga>): Long? {
|
|
return state.anchorPosition?.let { anchorPosition ->
|
|
val anchorPage = state.closestPageToPosition(anchorPosition)
|
|
anchorPage?.prevKey ?: anchorPage?.nextKey
|
|
}
|
|
}
|
|
}
|
|
|
|
class SourceSearchPagingSource(source: CatalogueSource, val query: String, val filters: FilterList) : SourcePagingSource(source) {
|
|
override suspend fun requestNextPage(currentPage: Int): MangasPage {
|
|
return source.fetchSearchManga(currentPage, query, filters).awaitSingle()
|
|
}
|
|
}
|
|
|
|
class SourcePopularPagingSource(source: CatalogueSource) : SourcePagingSource(source) {
|
|
override suspend fun requestNextPage(currentPage: Int): MangasPage {
|
|
return source.fetchPopularManga(currentPage).awaitSingle()
|
|
}
|
|
}
|
|
|
|
class SourceLatestPagingSource(source: CatalogueSource) : SourcePagingSource(source) {
|
|
override suspend fun requestNextPage(currentPage: Int): MangasPage {
|
|
return source.fetchLatestUpdates(currentPage).awaitSingle()
|
|
}
|
|
}
|