From e6f3cd03bbc8b9e0ea2203f981cda94062d5186c Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 5 Jun 2021 18:02:59 -0400 Subject: [PATCH] Use coroutine job for fetching next source page --- .../source/browse/BrowseSourcePresenter.kt | 20 ++++++++-------- .../ui/browse/source/browse/Pager.kt | 2 +- .../ui/browse/source/browse/SourcePager.kt | 24 +++++++------------ .../source/latest/LatestUpdatesPager.kt | 16 ++++--------- .../source/latest/LatestUpdatesPresenter.kt | 3 --- 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index a4646b7eb..6f0d338cb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -37,6 +37,7 @@ import eu.kanade.tachiyomi.util.chapter.syncChaptersWithTrackServiceTwoWay import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.removeCovers +import kotlinx.coroutines.Job import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.catch @@ -44,7 +45,6 @@ import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import rx.Observable import rx.Subscription import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers @@ -104,7 +104,7 @@ open class BrowseSourcePresenter( /** * Subscription for one request from the pager. */ - private var pageSubscription: Subscription? = null + private var nextPageJob: Job? = null private val loggedServices by lazy { Injekt.get().services.filter { it.isLogged } } @@ -175,14 +175,14 @@ open class BrowseSourcePresenter( fun requestNext() { if (!hasNextPage()) return - pageSubscription?.let { remove(it) } - pageSubscription = Observable.defer { pager.requestNext() } - .subscribeFirst( - { _, _ -> - // Nothing to do when onNext is emitted. - }, - BrowseSourceController::onAddPageError - ) + nextPageJob?.cancel() + nextPageJob = launchIO { + try { + pager.requestNextPage() + } catch (e: Throwable) { + view?.onAddPageError(e) + } + } } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/Pager.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/Pager.kt index 77284e5a8..0c3d9e858 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/Pager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/Pager.kt @@ -19,7 +19,7 @@ abstract class Pager(var currentPage: Int = 1) { return results.asObservable() } - abstract fun requestNext(): Observable + abstract suspend fun requestNextPage() fun onPageReceived(mangasPage: MangasPage) { val page = currentPage diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourcePager.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourcePager.kt index d771098c2..3d7970172 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourcePager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourcePager.kt @@ -2,14 +2,11 @@ package eu.kanade.tachiyomi.ui.browse.source.browse import eu.kanade.tachiyomi.source.CatalogueSource import eu.kanade.tachiyomi.source.model.FilterList -import eu.kanade.tachiyomi.source.model.MangasPage -import rx.Observable -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers +import eu.kanade.tachiyomi.util.lang.awaitSingle open class SourcePager(val source: CatalogueSource, val query: String, val filters: FilterList) : Pager() { - override fun requestNext(): Observable { + override suspend fun requestNextPage() { val page = currentPage 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) } - return observable - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnNext { - if (it.mangas.isNotEmpty()) { - onPageReceived(it) - } else { - throw NoResultsException() - } - } + val mangasPage = observable.awaitSingle() + + if (mangasPage.mangas.isNotEmpty()) { + onPageReceived(mangasPage) + } else { + throw NoResultsException() + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPager.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPager.kt index 97eccfc14..fa07a3462 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPager.kt @@ -1,21 +1,13 @@ package eu.kanade.tachiyomi.ui.browse.source.latest import eu.kanade.tachiyomi.source.CatalogueSource -import eu.kanade.tachiyomi.source.model.MangasPage import eu.kanade.tachiyomi.ui.browse.source.browse.Pager -import rx.Observable -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers +import eu.kanade.tachiyomi.util.lang.awaitSingle -/** - * LatestUpdatesPager inherited from the general Pager. - */ class LatestUpdatesPager(val source: CatalogueSource) : Pager() { - override fun requestNext(): Observable { - return source.fetchLatestUpdates(currentPage) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnNext { onPageReceived(it) } + override suspend fun requestNextPage() { + val mangasPage = source.fetchLatestUpdates(currentPage).awaitSingle() + onPageReceived(mangasPage) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPresenter.kt index 964c0b3c6..968d3635d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/latest/LatestUpdatesPresenter.kt @@ -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.Pager -/** - * Presenter of [LatestUpdatesController]. Inherit BrowseCataloguePresenter. - */ class LatestUpdatesPresenter(sourceId: Long) : BrowseSourcePresenter(sourceId) { override fun createPager(query: String, filters: FilterList): Pager {