From ed029c52aefe8173ffa6da0e73104e51dd6506c8 Mon Sep 17 00:00:00 2001 From: MCAxiaz Date: Sun, 17 May 2020 14:33:26 -0700 Subject: [PATCH] download new chapters changes (#3193) * download new chapters changes * move initialFetchChapters logic into onNextChapters * refractor download new chapter logic to be more explicit --- .../data/library/LibraryUpdateService.kt | 20 ++++--------------- .../ui/manga/chapter/ChaptersController.kt | 19 ++++++------------ .../ui/manga/chapter/ChaptersPresenter.kt | 16 +++++++++++++-- .../kanade/tachiyomi/util/MangaExtensions.kt | 14 +++++++++++++ 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 27d15b712c..b32265593b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -23,6 +23,7 @@ import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.prepUpdateCover +import eu.kanade.tachiyomi.util.shouldDownloadNewChapters import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.system.acquireWakeLock import eu.kanade.tachiyomi.util.system.isServiceRunning @@ -256,10 +257,6 @@ class LibraryUpdateService( val newUpdates = ArrayList>>() // List containing failed updates val failedUpdates = ArrayList>() - // List containing categories that get included in downloads. - val categoriesToDownload = preferences.downloadNewCategories().get().map(String::toInt) - // Boolean to determine if user wants to automatically download new chapters. - val downloadNew = preferences.downloadNew().get() // Boolean to determine if DownloadManager has downloads var hasDownloads = false @@ -278,11 +275,7 @@ class LibraryUpdateService( // Filter out mangas without new chapters (or failed). .filter { pair -> pair.first.isNotEmpty() } .doOnNext { - if (downloadNew && ( - categoriesToDownload.isEmpty() || - manga.category in categoriesToDownload - ) - ) { + if (manga.shouldDownloadNewChapters(db, preferences)) { downloadChapters(manga, it.first) hasDownloads = true } @@ -309,7 +302,7 @@ class LibraryUpdateService( if (newUpdates.isNotEmpty()) { notifier.showUpdateNotifications(newUpdates) - if (downloadNew && hasDownloads) { + if (hasDownloads) { DownloadService.start(this) } } @@ -326,14 +319,9 @@ class LibraryUpdateService( } private fun downloadChapters(manga: Manga, chapters: List) { - // we need to get the chapters from the db so we have chapter ids - val mangaChapters = db.getChapters(manga).executeAsBlocking() - val dbChapters = chapters.map { - mangaChapters.find { mangaChapter -> mangaChapter.url == it.url }!! - } // We don't want to start downloading while the library is updating, because websites // may don't like it and they could ban the user. - downloadManager.downloadChapters(manga, dbChapters, false) + downloadManager.downloadChapters(manga, chapters, false) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt index b2aa4ba3c3..810e9064cf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt @@ -99,7 +99,7 @@ class ChaptersController : adapter?.fastScroller = binding.fastScroller binding.swipeRefresh.refreshes() - .onEach { fetchChaptersFromSource() } + .onEach { fetchChaptersFromSource(manualFetch = true) } .launchIn(scope) binding.fab.clicks() @@ -263,10 +263,10 @@ class ChaptersController : } fun onNextChapters(chapters: List) { - // If the list is empty, fetch chapters from source if the conditions are met + // If the list is empty and it hasn't requested previously, fetch chapters from source // We use presenter chapters instead because they are always unfiltered - if (presenter.chapters.isEmpty()) { - initialFetchChapters() + if (!presenter.hasRequested && presenter.chapters.isEmpty()) { + fetchChaptersFromSource() } val adapter = adapter ?: return @@ -285,16 +285,9 @@ class ChaptersController : } } - private fun initialFetchChapters() { - // Only fetch if this view is from the catalog and it hasn't requested previously - if ((parentController as MangaController).fromSource && !presenter.hasRequested) { - fetchChaptersFromSource() - } - } - - private fun fetchChaptersFromSource() { + private fun fetchChaptersFromSource(manualFetch: Boolean = false) { binding.swipeRefresh.isRefreshing = true - presenter.fetchChaptersFromSource() + presenter.fetchChaptersFromSource(manualFetch) } fun onFetchChaptersDone() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt index 1434e3d56a..e88ee02166 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt @@ -14,6 +14,7 @@ import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.isLocal import eu.kanade.tachiyomi.util.lang.isNullOrUnsubscribed +import eu.kanade.tachiyomi.util.shouldDownloadNewChapters import java.util.Date import rx.Observable import rx.Subscription @@ -153,13 +154,18 @@ class ChaptersPresenter( /** * Requests an updated list of chapters from the source. */ - fun fetchChaptersFromSource() { + fun fetchChaptersFromSource(manualFetch: Boolean = false) { hasRequested = true if (!fetchChaptersSubscription.isNullOrUnsubscribed()) return fetchChaptersSubscription = Observable.defer { source.fetchChapterList(manga) } .subscribeOn(Schedulers.io()) .map { syncChaptersWithSource(db, it, manga, source) } + .doOnNext { + if (manualFetch) { + downloadNewChapters(it.first) + } + } .observeOn(AndroidSchedulers.mainThread()) .subscribeFirst( { view, _ -> @@ -262,7 +268,7 @@ class ChaptersPresenter( * Downloads the given list of chapters with the manager. * @param chapters the list of chapters to download. */ - fun downloadChapters(chapters: List) { + fun downloadChapters(chapters: List) { downloadManager.downloadChapters(manga, chapters) } @@ -299,6 +305,12 @@ class ChaptersPresenter( ) } + private fun downloadNewChapters(chapters: List) { + if (chapters.isEmpty() || !manga.shouldDownloadNewChapters(db, preferences)) return + + downloadChapters(chapters) + } + /** * Deletes a list of chapters from disk. This method is called in a background thread. * @param chapters the chapters to delete. diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt index c56ba05334..9dcadcfec5 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/MangaExtensions.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.util import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.source.LocalSource import eu.kanade.tachiyomi.source.model.SManga import java.util.Date @@ -47,3 +48,16 @@ fun Manga.updateCoverLastModified(db: DatabaseHelper) { cover_last_modified = Date().time db.updateMangaCoverLastModified(this).executeAsBlocking() } + +fun Manga.shouldDownloadNewChapters(db: DatabaseHelper, prefs: PreferencesHelper): Boolean { + // Boolean to determine if user wants to automatically download new chapters. + val downloadNew = prefs.downloadNew().get() + if (!downloadNew) return false + + val categoriesToDownload = prefs.downloadNewCategories().get().map(String::toInt) + if (categoriesToDownload.isEmpty()) return true + + val categoriesForManga = db.getCategoriesForManga(this).executeAsBlocking().mapNotNull { it.id } + + return categoriesForManga.intersect(categoriesToDownload).isNotEmpty() +}