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 b27d788172..c603e445d5 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 @@ -132,6 +132,11 @@ class LibraryUpdateService( */ const val KEY_TARGET = "target" + /** + * Key for list of manga to be updated. (For dynamic categories) + */ + const val KEY_MANGAS = "mangas" + /** * Returns the status of the service. * @@ -149,12 +154,21 @@ class LibraryUpdateService( * @param category a specific category to update, or null for global update. * @param target defines what should be updated. */ - fun start(context: Context, category: Category? = null, target: Target = Target.CHAPTERS) { + fun start( + context: Context, + category: Category? = null, + target: Target = Target.CHAPTERS, + mangaToUse: List? = null + ) { if (!isRunning()) { val intent = Intent(context, LibraryUpdateService::class.java).apply { putExtra(KEY_TARGET, target) category?.id?.let { id -> putExtra(KEY_CATEGORY, id) + if (mangaToUse != null) putExtra( + KEY_MANGAS, + mangaToUse.mapNotNull { it.id }.toLongArray() + ) } } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { @@ -164,7 +178,8 @@ class LibraryUpdateService( } } else { if (target == Target.CHAPTERS) category?.id?.let { - instance?.addCategory(it) + if (mangaToUse != null) instance?.addMangaToQueue(it, mangaToUse) + else instance?.addCategory(it) } } } @@ -227,6 +242,13 @@ class LibraryUpdateService( } } + private fun addMangaToQueue(categoryId: Int, manga: List) { + val selectedScheme = preferences.libraryUpdatePrioritization().getOrDefault() + val mangas = manga.sortedWith(rankingScheme[selectedScheme]) + categoryIds.add(categoryId) + addManga(mangas) + } + private fun addCategory(categoryId: Int) { val selectedScheme = preferences.libraryUpdatePrioritization().getOrDefault() val mangas = @@ -322,7 +344,18 @@ class LibraryUpdateService( instance = this val selectedScheme = preferences.libraryUpdatePrioritization().getOrDefault() - val mangaList = getMangaToUpdate(intent, target).sortedWith(rankingScheme[selectedScheme]) + val savedMangasList = intent.getLongArrayExtra(KEY_MANGAS)?.asList() + + val mangaList = (if (savedMangasList != null) { + val mangas = db.getLibraryMangas().executeAsBlocking().filter { + it.id in savedMangasList + }.distinctBy { it.id } + val categoryId = intent.getIntExtra(KEY_CATEGORY, -1) + if (categoryId > -1) categoryIds.add(categoryId) + mangas + } else { + getMangaToUpdate(intent, target) + }).sortedWith(rankingScheme[selectedScheme]) // Update favorite manga. Destroy service when completed or in case of an error. launchTarget(target, mangaList, startId) return START_REDELIVER_INTENT diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 41dc1da096..a6d8ba49e6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -1140,8 +1140,8 @@ class LibraryController( override fun openCategoriesBackdrop() = showCategories(true) - override fun updateCategory(catId: Int): Boolean { - val category = (adapter.getItem(catId) as? LibraryHeaderItem)?.category ?: return false + override fun updateCategory(position: Int): Boolean { + val category = (adapter.getItem(position) as? LibraryHeaderItem)?.category ?: return false val inQueue = LibraryUpdateService.categoryInQueue(category.id) snack?.dismiss() snack = view?.snack( @@ -1157,10 +1157,19 @@ class LibraryController( view.elevation = 15f.dpToPx setAction(R.string.cancel) { LibraryUpdateService.stop(context) - Handler().post { NotificationReceiver.dismissNotification(context, Notifications.ID_LIBRARY_PROGRESS) } + Handler().post { + NotificationReceiver.dismissNotification( + context, + Notifications.ID_LIBRARY_PROGRESS + ) + } } } - if (!inQueue) LibraryUpdateService.start(view!!.context, category) + if (!inQueue) LibraryUpdateService.start( + view!!.context, category, mangaToUse = if (category.isDynamic) { + presenter.getMangaInCategories(category.id) + } else null + ) return true } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt index 8d5a16431d..4a747c8130 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt @@ -116,20 +116,20 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd catProgress.gone() setSelection() } - category.isDynamic -> { + category.id ?: -1 < 0 -> { expandImage.gone() checkboxImage.gone() catProgress.gone() updateButton.gone() } LibraryUpdateService.categoryInQueue(category.id) -> { - expandImage.visibleIf(!adapter.isSingleCategory) + expandImage.visibleIf(!adapter.isSingleCategory && !category.isDynamic) checkboxImage.gone() catProgress.visible() updateButton.invisible() } else -> { - expandImage.visibleIf(!adapter.isSingleCategory) + expandImage.visibleIf(!adapter.isSingleCategory && !category.isDynamic) catProgress.gone() checkboxImage.gone() updateButton.visibleIf(!adapter.isSingleCategory) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index e51c7456da..c3f224c816 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -152,6 +152,11 @@ class LibraryPresenter( ) } + fun getMangaInCategories(catId: Int?): List? { + catId ?: return null + return allLibraryItems.filter { it.header.category.id == catId }.map { it.manga } + } + private suspend fun sectionLibrary(items: List, freshStart: Boolean = false) { libraryItems = items val showAll = showAllCategories || !libraryIsGrouped ||