From bf32bf28dabd6fbe4388e6d103cdac8415e74623 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 31 Dec 2020 17:45:59 -0500 Subject: [PATCH] Use coroutines instead of rx for some MangaPresenter operations --- .../tachiyomi/ui/manga/MangaPresenter.kt | 63 +++++++++---------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index 9ae82d5faf..ece062a2c5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/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, 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) { - 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) { @@ -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) { - 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. */