Kaynağa Gözat

Use coroutines instead of rx for some MangaPresenter operations

arkon 4 yıl önce
ebeveyn
işleme
bf32bf28da

+ 30 - 33
app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt

@@ -21,8 +21,10 @@ import eu.kanade.tachiyomi.ui.manga.chapter.ChapterItem
 import eu.kanade.tachiyomi.util.chapter.ChapterSettingsHelper
 import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
 import eu.kanade.tachiyomi.util.isLocal
+import eu.kanade.tachiyomi.util.lang.await
 import eu.kanade.tachiyomi.util.lang.isNullOrUnsubscribed
 import eu.kanade.tachiyomi.util.lang.launchIO
+import eu.kanade.tachiyomi.util.lang.launchUI
 import eu.kanade.tachiyomi.util.prepUpdateCover
 import eu.kanade.tachiyomi.util.removeCovers
 import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
@@ -461,7 +463,7 @@ class MangaPresenter(
         }
 
         launchIO {
-            db.updateChaptersProgress(chapters).executeAsBlocking()
+            db.updateChaptersProgress(chapters).await()
 
             if (preferences.removeAfterMarkedAsRead()) {
                 deleteChapters(chapters)
@@ -482,14 +484,13 @@ class MangaPresenter(
      * @param selectedChapters the list of chapters to bookmark.
      */
     fun bookmarkChapters(selectedChapters: List<ChapterItem>, bookmarked: Boolean) {
-        Observable.from(selectedChapters)
-            .doOnNext { chapter ->
-                chapter.bookmark = bookmarked
-            }
-            .toList()
-            .flatMap { db.updateChaptersProgress(it).asRxObservable() }
-            .subscribeOn(Schedulers.io())
-            .subscribe()
+        launchIO {
+            selectedChapters
+                .forEach {
+                    it.bookmark = bookmarked
+                    db.updateChapterProgress(it).await()
+                }
+        }
     }
 
     /**
@@ -497,17 +498,26 @@ class MangaPresenter(
      * @param chapters the list of chapters to delete.
      */
     fun deleteChapters(chapters: List<ChapterItem>) {
-        Observable.just(chapters)
-            .doOnNext { deleteChaptersInternal(chapters) }
-            .doOnNext { if (onlyDownloaded() != State.IGNORE) refreshChapters() }
-            .subscribeOn(Schedulers.io())
-            .observeOn(AndroidSchedulers.mainThread())
-            .subscribeFirst(
-                { view, _ ->
-                    view.onChaptersDeleted(chapters)
-                },
-                MangaController::onChaptersDeletedError
-            )
+        launchIO {
+            try {
+                downloadManager.deleteChapters(chapters, manga, source).forEach {
+                    if (it is ChapterItem) {
+                        it.status = Download.State.NOT_DOWNLOADED
+                        it.download = null
+                    }
+                }
+
+                if (onlyDownloaded() != State.IGNORE) {
+                    refreshChapters()
+                }
+
+                launchUI {
+                    view?.onChaptersDeleted(chapters)
+                }
+            } catch (e: Throwable) {
+                view?.onChaptersDeletedError(e)
+            }
+        }
     }
 
     private fun downloadNewChapters(chapters: List<Chapter>) {
@@ -516,19 +526,6 @@ class MangaPresenter(
         downloadChapters(chapters)
     }
 
-    /**
-     * Deletes a list of chapters from disk. This method is called in a background thread.
-     * @param chapters the chapters to delete.
-     */
-    private fun deleteChaptersInternal(chapters: List<ChapterItem>) {
-        downloadManager.deleteChapters(chapters, manga, source).forEach {
-            if (it is ChapterItem) {
-                it.status = Download.State.NOT_DOWNLOADED
-                it.download = null
-            }
-        }
-    }
-
     /**
      * Reverses the sorting and requests an UI update.
      */