ソースを参照

Refactor bridged RxJava/coroutine calls in SearchPresenters

arkon 4 年 前
コミット
5cfda1b1bf

+ 9 - 16
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(

+ 12 - 13
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<Manga> {
-        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
     }
 
     /**