Use coroutines instead of rx for some MangaPresenter operations

This commit is contained in:
arkon 2020-12-31 17:45:59 -05:00
parent dcb6bfb18d
commit bf32bf28da

View File

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