Use coroutine job for fetching next source page

This commit is contained in:
arkon 2021-06-05 18:02:59 -04:00
parent a1e31549a2
commit e6f3cd03bb
5 changed files with 24 additions and 41 deletions

View File

@ -37,6 +37,7 @@ import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay
import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.launchIO
import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.lang.withUIContext
import eu.kanade.tachiyomi.util.removeCovers import eu.kanade.tachiyomi.util.removeCovers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
@ -44,7 +45,6 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import rx.Observable
import rx.Subscription import rx.Subscription
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers import rx.schedulers.Schedulers
@ -104,7 +104,7 @@ open class BrowseSourcePresenter(
/** /**
* Subscription for one request from the pager. * Subscription for one request from the pager.
*/ */
private var pageSubscription: Subscription? = null private var nextPageJob: Job? = null
private val loggedServices by lazy { Injekt.get<TrackManager>().services.filter { it.isLogged } } private val loggedServices by lazy { Injekt.get<TrackManager>().services.filter { it.isLogged } }
@ -175,14 +175,14 @@ open class BrowseSourcePresenter(
fun requestNext() { fun requestNext() {
if (!hasNextPage()) return if (!hasNextPage()) return
pageSubscription?.let { remove(it) } nextPageJob?.cancel()
pageSubscription = Observable.defer { pager.requestNext() } nextPageJob = launchIO {
.subscribeFirst( try {
{ _, _ -> pager.requestNextPage()
// Nothing to do when onNext is emitted. } catch (e: Throwable) {
}, view?.onAddPageError(e)
BrowseSourceController::onAddPageError }
) }
} }
/** /**

View File

@ -19,7 +19,7 @@ abstract class Pager(var currentPage: Int = 1) {
return results.asObservable() return results.asObservable()
} }
abstract fun requestNext(): Observable<MangasPage> abstract suspend fun requestNextPage()
fun onPageReceived(mangasPage: MangasPage) { fun onPageReceived(mangasPage: MangasPage) {
val page = currentPage val page = currentPage

View File

@ -2,14 +2,11 @@ package eu.kanade.tachiyomi.ui.browse.source.browse
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.util.lang.awaitSingle
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
open class SourcePager(val source: CatalogueSource, val query: String, val filters: FilterList) : Pager() { open class SourcePager(val source: CatalogueSource, val query: String, val filters: FilterList) : Pager() {
override fun requestNext(): Observable<MangasPage> { override suspend fun requestNextPage() {
val page = currentPage val page = currentPage
val observable = if (query.isBlank() && filters.isEmpty()) { val observable = if (query.isBlank() && filters.isEmpty()) {
@ -18,15 +15,12 @@ open class SourcePager(val source: CatalogueSource, val query: String, val filte
source.fetchSearchManga(page, query, filters) source.fetchSearchManga(page, query, filters)
} }
return observable val mangasPage = observable.awaitSingle()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) if (mangasPage.mangas.isNotEmpty()) {
.doOnNext { onPageReceived(mangasPage)
if (it.mangas.isNotEmpty()) {
onPageReceived(it)
} else { } else {
throw NoResultsException() throw NoResultsException()
} }
} }
} }
}

View File

@ -1,21 +1,13 @@
package eu.kanade.tachiyomi.ui.browse.source.latest package eu.kanade.tachiyomi.ui.browse.source.latest
import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.ui.browse.source.browse.Pager import eu.kanade.tachiyomi.ui.browse.source.browse.Pager
import rx.Observable import eu.kanade.tachiyomi.util.lang.awaitSingle
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
/**
* LatestUpdatesPager inherited from the general Pager.
*/
class LatestUpdatesPager(val source: CatalogueSource) : Pager() { class LatestUpdatesPager(val source: CatalogueSource) : Pager() {
override fun requestNext(): Observable<MangasPage> { override suspend fun requestNextPage() {
return source.fetchLatestUpdates(currentPage) val mangasPage = source.fetchLatestUpdates(currentPage).awaitSingle()
.subscribeOn(Schedulers.io()) onPageReceived(mangasPage)
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { onPageReceived(it) }
} }
} }

View File

@ -4,9 +4,6 @@ import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter
import eu.kanade.tachiyomi.ui.browse.source.browse.Pager import eu.kanade.tachiyomi.ui.browse.source.browse.Pager
/**
* Presenter of [LatestUpdatesController]. Inherit BrowseCataloguePresenter.
*/
class LatestUpdatesPresenter(sourceId: Long) : BrowseSourcePresenter(sourceId) { class LatestUpdatesPresenter(sourceId: Long) : BrowseSourcePresenter(sourceId) {
override fun createPager(query: String, filters: FilterList): Pager { override fun createPager(query: String, filters: FilterList): Pager {