From 68c4116327ea2fde74fbcd72431fdb1412b5e3bf Mon Sep 17 00:00:00 2001 From: Bram van de Kerkhof Date: Sat, 18 Mar 2017 13:09:40 -0400 Subject: [PATCH] Category-specific auto download (#701) * Category-specific auto download --- .../data/library/LibraryUpdateService.kt | 28 ++++++++++++++---- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 4 ++- .../ui/setting/SettingsDownloadsFragment.kt | 29 +++++++++++++++++++ app/src/main/res/values/keys.xml | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/pref_downloads.xml | 4 +++ 7 files changed, 62 insertions(+), 7 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 09f5e93044..29a38e5966 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 @@ -57,6 +57,9 @@ class LibraryUpdateService : Service() { */ val preferences: PreferencesHelper by injectLazy() + /** + * Download Manager + */ val downloadManager: DownloadManager by injectLazy() /** @@ -216,7 +219,7 @@ class LibraryUpdateService : Service() { .filter { it.category in categoriesToUpdate } .distinctBy { it.id } else - db.getFavoriteMangas().executeAsBlocking().distinctBy { it.id } + db.getLibraryMangas().executeAsBlocking().distinctBy { it.id } } if (!intent.getBooleanExtra(UPDATE_DETAILS, false) && preferences.updateOnlyNonCompleted()) { @@ -238,8 +241,16 @@ class LibraryUpdateService : Service() { fun updateChapterList(mangaToUpdate: List): Observable { // Initialize the variables holding the progress of the updates. val count = AtomicInteger(0) + // List containing new updates val newUpdates = ArrayList() + // list containing failed updates val failedUpdates = ArrayList() + // List containing categories that get included in downloads. + val categoriesToDownload = preferences.downloadNewCategories().getOrDefault().map(String::toInt) + // Boolean to determine if user wants to automatically download new chapters. + val downloadNew = preferences.downloadNew().getOrDefault() + // Boolean to determine if DownloadManager has downloads + var hasDownloads = false // Emit each manga and update it sequentially. return Observable.from(mangaToUpdate) @@ -254,10 +265,13 @@ class LibraryUpdateService : Service() { Pair(emptyList(), emptyList()) } // Filter out mangas without new chapters (or failed). - .filter { pair -> pair.first.size > 0 } + .filter { pair -> pair.first.isNotEmpty() } .doOnNext { - if (preferences.downloadNew()) { - downloadChapters(manga, it.first) + if (downloadNew) { + if (categoriesToDownload.isEmpty() || manga.category in categoriesToDownload) { + downloadChapters(manga, it.first) + hasDownloads = true + } } } // Convert to the manga that contains new chapters. @@ -277,8 +291,10 @@ class LibraryUpdateService : Service() { cancelNotification() } else { showResultNotification(newUpdates, failedUpdates) - if (preferences.downloadNew()) { - DownloadService.start(this) + if (downloadNew) { + if (hasDownloads) { + DownloadService.start(this) + } } } } 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 fa77e4ad46..2ff7f41df7 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 @@ -93,6 +93,8 @@ class PreferenceKeys(context: Context) { val downloadNew = context.getString(R.string.pref_download_new_key) + val downloadNewCategories = context.getString(R.string.pref_download_new_categories_key) + fun sourceUsername(sourceId: Long) = "pref_source_username_$sourceId" fun sourcePassword(sourceId: Long) = "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 f6a9648299..932104c726 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 @@ -142,7 +142,9 @@ class PreferencesHelper(val context: Context) { fun hiddenCatalogues() = rxPrefs.getStringSet("hidden_catalogues", emptySet()) - fun downloadNew() = prefs.getBoolean(keys.downloadNew, false) + fun downloadNew() = rxPrefs.getBoolean(keys.downloadNew, false) + + fun downloadNewCategories() = rxPrefs.getStringSet(keys.downloadNewCategories, emptySet()) fun lang() = prefs.getString(keys.lang, "") diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt index 64c217d906..5a948b47e9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsDownloadsFragment.kt @@ -19,10 +19,12 @@ import com.nononsenseapps.filepicker.FilePickerActivity import com.nononsenseapps.filepicker.FilePickerFragment import com.nononsenseapps.filepicker.LogicHandler import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.util.inflate import eu.kanade.tachiyomi.util.plusAssign +import net.xpece.android.support.preference.MultiSelectListPreference import uy.kohesive.injekt.injectLazy import java.io.File @@ -41,8 +43,12 @@ class SettingsDownloadsFragment : SettingsFragment() { private val preferences: PreferencesHelper by injectLazy() + private val db: DatabaseHelper by injectLazy() + val downloadDirPref: Preference by bindPref(R.string.pref_download_directory_key) + val downloadCategory: MultiSelectListPreference by bindPref(R.string.pref_download_new_categories_key) + override fun onViewCreated(view: View, savedState: Bundle?) { super.onViewCreated(view, savedState) @@ -92,6 +98,29 @@ class SettingsDownloadsFragment : SettingsFragment() { dir.createFile(".nomedia") } } + + subscriptions += preferences.downloadNew().asObservable() + .subscribe { downloadCategory.isVisible = it } + + val dbCategories = db.getCategories().executeAsBlocking() + downloadCategory.apply { + entries = dbCategories.map { it.name }.toTypedArray() + entryValues = dbCategories.map { it.id.toString() }.toTypedArray() + } + + subscriptions += preferences.downloadNewCategories().asObservable() + .subscribe { + val selectedCategories = it + .mapNotNull { id -> dbCategories.find { it.id == id.toInt() } } + .sortedBy { it.order } + + val summary = if (selectedCategories.isEmpty()) + getString(R.string.all) + else + selectedCategories.joinToString { it.name } + + downloadCategory.summary = summary + } } fun getExternalFilesDirs(): List { diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 4c2ea1549c..ff4ce8afd7 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -68,6 +68,7 @@ last_catalogue_source download_new + download_new_categories sans-serif diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cfc4d48664..2f11f948df 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -176,6 +176,7 @@ Fourth to last chapter Fifth to last chapter Download new chapters + Categories to include in download Services diff --git a/app/src/main/res/xml/pref_downloads.xml b/app/src/main/res/xml/pref_downloads.xml index 8660252eb3..73260dfb25 100644 --- a/app/src/main/res/xml/pref_downloads.xml +++ b/app/src/main/res/xml/pref_downloads.xml @@ -53,6 +53,10 @@ android:key="@string/pref_download_new_key" android:title="@string/pref_download_new"/> + + \ No newline at end of file