download new chapters changes (#3193)

* download new chapters changes

* move initialFetchChapters logic into onNextChapters

* refractor download new chapter logic to be more explicit
This commit is contained in:
MCAxiaz 2020-05-17 14:33:26 -07:00 committed by GitHub
parent 102a372df9
commit ed029c52ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 31 deletions

View file

@ -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<Pair<LibraryManga, Array<Chapter>>>()
// List containing failed updates
val failedUpdates = ArrayList<Pair<Manga, String?>>()
// 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<Chapter>) {
// 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)
}
/**

View file

@ -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<ChapterItem>) {
// 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() {

View file

@ -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<ChapterItem>) {
fun downloadChapters(chapters: List<Chapter>) {
downloadManager.downloadChapters(manga, chapters)
}
@ -299,6 +305,12 @@ class ChaptersPresenter(
)
}
private fun downloadNewChapters(chapters: List<Chapter>) {
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.

View file

@ -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()
}