From 8b60d5bfcb77e4bcd7007ca7e40fe9314372b6d6 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 6 Dec 2016 17:22:03 +0100 Subject: [PATCH] Add optional to automatically download new chapers (#538) * Add optional to automatically download new chapers * Only trigger download once --- .../data/library/LibraryUpdateService.kt | 28 +++++++++++++++++-- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 2 ++ .../tachiyomi/util/ChapterSourceSync.kt | 21 +++++++------- app/src/main/res/values/keys.xml | 2 ++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_downloads.xml | 9 ++++++ 7 files changed, 51 insertions(+), 14 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 cbe8e7d7b..90a51f594 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 @@ -13,7 +13,10 @@ import eu.kanade.tachiyomi.Constants import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category +import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.download.DownloadManager +import eu.kanade.tachiyomi.data.download.DownloadService import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Companion.start import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault @@ -53,6 +56,8 @@ class LibraryUpdateService : Service() { */ val preferences: PreferencesHelper by injectLazy() + val downloadManager: DownloadManager by injectLazy() + /** * Wake lock that will be held until the service is destroyed. */ @@ -243,10 +248,15 @@ class LibraryUpdateService : Service() { // If there's any error, return empty update and continue. .onErrorReturn { failedUpdates.add(manga) - Pair(0, 0) + Pair(emptyList(), emptyList()) } // Filter out mangas without new chapters (or failed). - .filter { pair -> pair.first > 0 } + .filter { pair -> pair.first.size > 0 } + .doOnNext { + if (preferences.downloadNew()) { + downloadChapters(manga, it.first) + } + } // Convert to the manga that contains new chapters. .map { manga } } @@ -263,18 +273,30 @@ class LibraryUpdateService : Service() { if (newUpdates.isEmpty()) { cancelNotification() } else { + if (preferences.downloadNew()) { + DownloadService.start(this) + } showResultNotification(newUpdates, failedUpdates) } } } + 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 }!! + } + downloadManager.downloadChapters(manga, dbChapters) + } + /** * Updates the chapters for the given manga and adds them to the database. * * @param manga the manga to update. * @return a pair of the inserted and removed chapters. */ - fun updateManga(manga: Manga): Observable> { + fun updateManga(manga: Manga): Observable, List>> { val source = sourceManager.get(manga.source) as? OnlineSource ?: return Observable.empty() return source.fetchChapterList(manga) .map { syncChaptersWithSource(db, it, manga, source) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 0aff5895b..4e23025f3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -89,6 +89,8 @@ class PreferenceKeys(context: Context) { val startScreen = context.getString(R.string.pref_start_screen_key) + val downloadNew = context.getString(R.string.pref_download_new_key) + fun sourceUsername(sourceId: Int) = "pref_source_username_$sourceId" fun sourcePassword(sourceId: Int) = "pref_source_password_$sourceId" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index 5301ae60e..2163dd778 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -134,4 +134,6 @@ class PreferencesHelper(context: Context) { fun hiddenCatalogues() = rxPrefs.getStringSet("hidden_catalogues", emptySet()) + fun downloadNew() = prefs.getBoolean(keys.downloadNew, false) + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/ChapterSourceSync.kt b/app/src/main/java/eu/kanade/tachiyomi/util/ChapterSourceSync.kt index 249970a30..672a94691 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/ChapterSourceSync.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/ChapterSourceSync.kt @@ -19,7 +19,7 @@ import java.util.* fun syncChaptersWithSource(db: DatabaseHelper, sourceChapters: List, manga: Manga, - source: Source) : Pair { + source: Source) : Pair, List> { // Chapters from db. val dbChapters = db.getChapters(manga).executeAsBlocking() @@ -44,22 +44,19 @@ fun syncChaptersWithSource(db: DatabaseHelper, // Chapters from the db not in the source. val toDelete = dbChapters.filterNot { it in sourceChapters } - // Amount of chapters added and deleted. - var added = 0 - var deleted = 0 - - // Amount of chapters readded (different url but the same chapter number). - var readded = 0 + val readded = mutableListOf() db.inTransaction { + val deletedChapterNumbers = TreeSet() val deletedReadChapterNumbers = TreeSet() if (!toDelete.isEmpty()) { for (c in toDelete) { if (c.read) { deletedReadChapterNumbers.add(c.chapter_number) } + deletedChapterNumbers.add(c.chapter_number) } - deleted = db.deleteChapters(toDelete).executeAsBlocking().results().size + db.deleteChapters(toDelete).executeAsBlocking() } if (!toAdd.isEmpty()) { @@ -73,14 +70,16 @@ fun syncChaptersWithSource(db: DatabaseHelper, // Try to mark already read chapters as read when the source deletes them if (c.isRecognizedNumber && c.chapter_number in deletedReadChapterNumbers) { c.read = true - readded++ + } + if (c.isRecognizedNumber && c.chapter_number in deletedChapterNumbers) { + readded.add(c) } } - added = db.insertChapters(toAdd).executeAsBlocking().numberOfInserts() + db.insertChapters(toAdd).executeAsBlocking() } // Fix order in source. db.fixChaptersSourceOrder(sourceChapters).executeAsBlocking() } - return Pair(added - readded, deleted - readded) + return Pair(toAdd.subtract(readded).toList(), toDelete.subtract(readded).toList()) } diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 42a9c84b2..52e687c77 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -65,6 +65,8 @@ pref_display_catalogue_as_list pref_last_catalogue_source_key + download_new + sans-serif sans-serif diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 03866d5c3..c412edda2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -166,6 +166,7 @@ Third to last chapter Fourth to last chapter Fifth to last chapter + Download new chapters Services diff --git a/app/src/main/res/xml/pref_downloads.xml b/app/src/main/res/xml/pref_downloads.xml index 7942c5682..8660252eb 100644 --- a/app/src/main/res/xml/pref_downloads.xml +++ b/app/src/main/res/xml/pref_downloads.xml @@ -44,6 +44,15 @@ android:summary="%s" android:title="@string/pref_remove_after_read" /> + + + + \ No newline at end of file