Refactor bridged RxJava/coroutine calls in SearchPresenters

This commit is contained in:
arkon 2021-01-04 15:08:36 -05:00
parent ac9bf1f3ff
commit 5cfda1b1bf
2 changed files with 21 additions and 29 deletions

View file

@ -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.GlobalSearchItem
import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchPresenter import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchPresenter
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.lang.runAsObservable import eu.kanade.tachiyomi.util.lang.launchIO
import rx.Observable import eu.kanade.tachiyomi.util.lang.launchUI
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.Date import java.util.Date
class SearchPresenter( class SearchPresenter(
@ -57,19 +55,14 @@ class SearchPresenter(
replacingMangaRelay.call(true) replacingMangaRelay.call(true)
Observable.defer { launchIO {
runAsObservable({ val chapters = source.getChapterList(manga.toMangaInfo())
source.getChapterList(manga.toMangaInfo()) .map { it.toSChapter() }
.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( private fun migrateMangaInternal(

View file

@ -15,6 +15,7 @@ import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.toSManga import eu.kanade.tachiyomi.source.model.toSManga
import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter import eu.kanade.tachiyomi.ui.browse.source.browse.BrowseSourcePresenter
import eu.kanade.tachiyomi.util.lang.await
import eu.kanade.tachiyomi.util.lang.runAsObservable import eu.kanade.tachiyomi.util.lang.runAsObservable
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
@ -225,9 +226,10 @@ open class GlobalSearchPresenter(
fetchImageSubscription?.unsubscribe() fetchImageSubscription?.unsubscribe()
fetchImageSubscription = fetchImageSubject.observeOn(Schedulers.io()) fetchImageSubscription = fetchImageSubject.observeOn(Schedulers.io())
.flatMap { (first, source) -> .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) } .map { Pair(it, source) }
.concatMap { getMangaDetailsObservable(it.first, it.second) } .concatMap { runAsObservable({ getMangaDetails(it.first, it.second) }) }
.map { Pair(source as CatalogueSource, it) } .map { Pair(source as CatalogueSource, it) }
} }
.onBackpressureBuffer() .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. * @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<Manga> { private suspend fun getMangaDetails(manga: Manga, source: Source): Manga {
return runAsObservable({ val networkManga = source.getMangaDetails(manga.toMangaInfo())
val networkManga = source.getMangaDetails(manga.toMangaInfo()) manga.copyFrom(networkManga.toSManga())
manga.copyFrom(networkManga.toSManga()) manga.initialized = true
manga.initialized = true db.insertManga(manga).await()
db.insertManga(manga).executeAsBlocking() return manga
manga
})
.onErrorResumeNext { Observable.just(manga) }
} }
/** /**