mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 22:37:56 +01:00 
			
		
		
		
	download new chapters changes (#3193)
* download new chapters changes
* move initialFetchChapters logic into onNextChapters
* refractor download new chapter logic to be more explicit
(cherry picked from commit ed029c52ae)
# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersPresenter.kt
			
			
This commit is contained in:
		| @@ -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 | ||||
| @@ -257,10 +258,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 | ||||
|  | ||||
| @@ -283,11 +280,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 | ||||
|                             } | ||||
| @@ -315,7 +308,7 @@ class LibraryUpdateService( | ||||
|  | ||||
|                 if (newUpdates.isNotEmpty()) { | ||||
|                     notifier.showUpdateNotifications(newUpdates) | ||||
|                     if (downloadNew && hasDownloads) { | ||||
|                     if (hasDownloads) { | ||||
|                         DownloadService.start(this) | ||||
|                     } | ||||
|                 } | ||||
| @@ -332,14 +325,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) | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|   | ||||
| @@ -101,7 +101,7 @@ class ChaptersController : | ||||
|         adapter?.fastScroller = binding.fastScroller | ||||
|  | ||||
|         binding.swipeRefresh.refreshes() | ||||
|             .onEach { fetchChaptersFromSource() } | ||||
|             .onEach { fetchChaptersFromSource(manualFetch = true) } | ||||
|             .launchIn(scope) | ||||
|  | ||||
|         binding.fab.clicks() | ||||
| @@ -265,10 +265,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 mangaController = parentController as MangaController | ||||
| @@ -299,16 +299,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() { | ||||
|   | ||||
| @@ -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 exh.EH_SOURCE_ID | ||||
| import exh.EXH_SOURCE_ID | ||||
| import exh.debug.DebugToggles | ||||
| @@ -189,13 +190,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, _ -> | ||||
| @@ -301,7 +307,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) | ||||
|     } | ||||
|  | ||||
| @@ -338,6 +344,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. | ||||
|   | ||||
| @@ -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() | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user