From 5cfda1b1bfe739c6e635817d4d72f4fa67677394 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 4 Jan 2021 15:08:36 -0500 Subject: [PATCH] Refactor bridged RxJava/coroutine calls in SearchPresenters --- .../migration/search/SearchPresenter.kt | 25 +++++++------------ .../globalsearch/GlobalSearchPresenter.kt | 25 +++++++++---------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt index 89b391fdae..e16eae4bb2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/migration/search/SearchPresenter.kt @@ -15,10 +15,8 @@ import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchCardItem import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchItem import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchPresenter import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource -import eu.kanade.tachiyomi.util.lang.runAsObservable -import rx.Observable -import rx.android.schedulers.AndroidSchedulers -import rx.schedulers.Schedulers +import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.launchUI import java.util.Date class SearchPresenter( @@ -57,19 +55,14 @@ class SearchPresenter( replacingMangaRelay.call(true) - Observable.defer { - runAsObservable({ - source.getChapterList(manga.toMangaInfo()) - .map { it.toSChapter() } - }) + launchIO { + val chapters = source.getChapterList(manga.toMangaInfo()) + .map { it.toSChapter() } + + migrateMangaInternal(source, chapters, prevManga, manga, replace) + }.invokeOnCompletion { + launchUI { replacingMangaRelay.call(false) } } - .onErrorReturn { emptyList() } - .doOnNext { migrateMangaInternal(source, it, prevManga, manga, replace) } - .onErrorReturn { emptyList() } - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .doOnUnsubscribe { replacingMangaRelay.call(false) } - .subscribe() } private fun migrateMangaInternal( diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt index a93b78c113..ecff818a32 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchPresenter.kt @@ -15,6 +15,7 @@ import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.toSManga import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter +import eu.kanade.tachiyomi.util.lang.await import eu.kanade.tachiyomi.util.lang.runAsObservable import rx.Observable import rx.Subscription @@ -225,9 +226,10 @@ open class GlobalSearchPresenter( fetchImageSubscription?.unsubscribe() fetchImageSubscription = fetchImageSubject.observeOn(Schedulers.io()) .flatMap { (first, source) -> - Observable.from(first).filter { it.thumbnail_url == null && !it.initialized } + Observable.from(first) + .filter { it.thumbnail_url == null && !it.initialized } .map { Pair(it, source) } - .concatMap { getMangaDetailsObservable(it.first, it.second) } + .concatMap { runAsObservable({ getMangaDetails(it.first, it.second) }) } .map { Pair(source as CatalogueSource, it) } } .onBackpressureBuffer() @@ -244,20 +246,17 @@ open class GlobalSearchPresenter( } /** - * Returns an observable of manga that initializes the given manga. + * Initializes the given manga. * * @param manga the manga to initialize. - * @return an observable of the manga to initialize + * @return The initialized manga. */ - private fun getMangaDetailsObservable(manga: Manga, source: Source): Observable { - return runAsObservable({ - val networkManga = source.getMangaDetails(manga.toMangaInfo()) - manga.copyFrom(networkManga.toSManga()) - manga.initialized = true - db.insertManga(manga).executeAsBlocking() - manga - }) - .onErrorResumeNext { Observable.just(manga) } + private suspend fun getMangaDetails(manga: Manga, source: Source): Manga { + val networkManga = source.getMangaDetails(manga.toMangaInfo()) + manga.copyFrom(networkManga.toSManga()) + manga.initialized = true + db.insertManga(manga).await() + return manga } /**