Option to include default category in downloading new chapters

This commit is contained in:
Jays2Kings 2021-04-27 02:06:57 -04:00
parent 63e6fb715d
commit 5c6a91b0d3
3 changed files with 27 additions and 49 deletions

View File

@ -28,14 +28,13 @@ import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.data.track.TrackManager import eu.kanade.tachiyomi.data.track.TrackManager
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.model.SManga
import eu.kanade.tachiyomi.source.model.toMangaInfo
import eu.kanade.tachiyomi.source.model.toSChapter import eu.kanade.tachiyomi.source.model.toSChapter
import eu.kanade.tachiyomi.source.model.toSManga import eu.kanade.tachiyomi.source.model.toSManga
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.storage.getUriCompat import eu.kanade.tachiyomi.util.storage.getUriCompat
import eu.kanade.tachiyomi.util.manga.MangaShortcutManager import eu.kanade.tachiyomi.util.manga.MangaShortcutManager
import eu.kanade.tachiyomi.util.system.executeOnIO import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -97,10 +96,6 @@ class LibraryUpdateService(
val count = AtomicInteger(0) val count = AtomicInteger(0)
val jobCount = AtomicInteger(0) val jobCount = AtomicInteger(0)
// List containing categories that get included in downloads.
private val categoriesToDownload =
preferences.downloadNewCategories().get().map(String::toInt)
// Boolean to determine if user wants to automatically download new chapters. // Boolean to determine if user wants to automatically download new chapters.
private val downloadNew: Boolean = preferences.downloadNew().get() private val downloadNew: Boolean = preferences.downloadNew().get()
@ -258,13 +253,7 @@ class LibraryUpdateService(
} }
GlobalScope.launch(handler) { GlobalScope.launch(handler) {
val hasDLs = try { val hasDLs = try {
requestSemaphore.withPermit { requestSemaphore.withPermit { updateMangaInSource(it.key) }
updateMangaInSource(
it.key,
downloadNew,
categoriesToDownload
)
}
} catch (e: Exception) { } catch (e: Exception) {
false false
} }
@ -314,7 +303,7 @@ class LibraryUpdateService(
async { async {
try { try {
requestSemaphore.withPermit { requestSemaphore.withPermit {
updateMangaInSource(source, downloadNew, categoriesToDownload) updateMangaInSource(source)
} }
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e) Timber.e(e)
@ -341,7 +330,7 @@ class LibraryUpdateService(
DownloadService.start(this) DownloadService.start(this)
} }
} else if (downloadNew && hasDownloads) { } else if (downloadNew && hasDownloads) {
DownloadService.start(this) DownloadService.start(this.applicationContext)
} }
newUpdates.clear() newUpdates.clear()
} }
@ -357,25 +346,14 @@ class LibraryUpdateService(
notifier.cancelProgressNotification() notifier.cancelProgressNotification()
} }
private suspend fun updateMangaInSource( private suspend fun updateMangaInSource(source: Long): Boolean {
source: Long,
downloadNew: Boolean,
categoriesToDownload: List<Int>
): Boolean {
if (mangaToUpdateMap[source] == null) return false if (mangaToUpdateMap[source] == null) return false
var count = 0 var count = 0
var hasDownloads = false var hasDownloads = false
while (count < mangaToUpdateMap[source]!!.size) { while (count < mangaToUpdateMap[source]!!.size) {
val shouldDownload = val manga = mangaToUpdateMap[source]!![count]
( val shouldDownload = manga.shouldDownloadNewChapters(db, preferences)
downloadNew && ( if (updateMangaChapters(manga, this.count.andIncrement, shouldDownload)) {
categoriesToDownload.isEmpty() ||
mangaToUpdateMap[source]!![count].category in categoriesToDownload ||
db.getCategoriesForManga(mangaToUpdateMap[source]!![count])
.executeOnIO().any { (it.id ?: -1) in categoriesToDownload }
)
)
if (updateMangaChapters(mangaToUpdateMap[source]!![count], this.count.andIncrement, shouldDownload)) {
hasDownloads = true hasDownloads = true
} }
count++ count++

View File

@ -80,7 +80,7 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
category.name = text category.name = text
if (this.category == null) { if (this.category == null) {
val categories = db.getCategories().executeAsBlocking() val categories = db.getCategories().executeAsBlocking()
category.order = categories.maxOf { it.order } + 1 category.order = (categories.maxOfOrNull { it.order } ?: 0) + 1
category.mangaSort = LibrarySort.Title.categoryValue category.mangaSort = LibrarySort.Title.categoryValue
val dbCategory = db.insertCategory(category).executeAsBlocking() val dbCategory = db.insertCategory(category).executeAsBlocking()
category.id = dbCategory.insertedId()?.toInt() category.id = dbCategory.insertedId()?.toInt()
@ -124,7 +124,6 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
fun onViewCreated() { fun onViewCreated() {
if (category?.id ?: 0 <= 0 && category != null) { if (category?.id ?: 0 <= 0 && category != null) {
binding.categoryTextLayout.isVisible = false binding.categoryTextLayout.isVisible = false
binding.downloadNew.isVisible = false
} }
binding.editCategories.isVisible = category != null binding.editCategories.isVisible = category != null
binding.editCategories.setOnClickListener { binding.editCategories.setOnClickListener {
@ -134,23 +133,22 @@ class ManageCategoryDialog(bundle: Bundle? = null) :
binding.title.addTextChangedListener { binding.title.addTextChangedListener {
binding.categoryTextLayout.error = null binding.categoryTextLayout.error = null
} }
binding.title.hint = category?.name ?: binding.editCategories.context.getString(R.string.category) binding.title.hint =
category?.name ?: binding.editCategories.context.getString(R.string.category)
binding.title.append(category?.name ?: "") binding.title.append(category?.name ?: "")
if (binding.downloadNew.isVisible) { val downloadNew = preferences.downloadNew().get()
val downloadNew = preferences.downloadNew().get() setCheckbox(
setCheckbox( binding.downloadNew,
binding.downloadNew, preferences.downloadNewCategories(),
preferences.downloadNewCategories(), true
true )
) if (downloadNew && preferences.downloadNewCategories().get().isEmpty()) {
if (downloadNew && preferences.downloadNewCategories().get().isEmpty()) { binding.downloadNew.isVisible = false
binding.downloadNew.isVisible = false } else if (!downloadNew) {
} else if (!downloadNew) { binding.downloadNew.isVisible = true
binding.downloadNew.isVisible = true
}
binding.downloadNew.isChecked =
preferences.downloadNew().get() && binding.downloadNew.isChecked
} }
binding.downloadNew.isChecked =
preferences.downloadNew().get() && binding.downloadNew.isChecked
setCheckbox( setCheckbox(
binding.includeGlobal, binding.includeGlobal,
preferences.libraryUpdateCategories(), preferences.libraryUpdateCategories(),

View File

@ -15,6 +15,7 @@ import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Category
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asImmediateFlowIn import eu.kanade.tachiyomi.data.preference.asImmediateFlowIn
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
@ -78,6 +79,7 @@ class SettingsDownloadController : SettingsController() {
} }
val dbCategories = db.getCategories().executeAsBlocking() val dbCategories = db.getCategories().executeAsBlocking()
val categories = listOf(Category.createDefault(context)) + dbCategories
preferenceCategory { preferenceCategory {
titleRes = R.string.download_new_chapters titleRes = R.string.download_new_chapters
@ -90,8 +92,8 @@ class SettingsDownloadController : SettingsController() {
multiSelectListPreferenceMat(activity) { multiSelectListPreferenceMat(activity) {
key = Keys.downloadNewCategories key = Keys.downloadNewCategories
titleRes = R.string.categories_to_include_in_download titleRes = R.string.categories_to_include_in_download
entries = dbCategories.map { it.name } entries = categories.map { it.name }
entryValues = dbCategories.map { it.id.toString() } entryValues = categories.map { it.id.toString() }
allSelectionRes = R.string.all allSelectionRes = R.string.all
preferences.downloadNew().asImmediateFlowIn(viewScope) { isVisible = it } preferences.downloadNew().asImmediateFlowIn(viewScope) { isVisible = it }