From 8d712c81d4854a52ae21c1a0d924629b9d76ba98 Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 17 Feb 2020 16:27:32 -0500 Subject: [PATCH] Tweak manga favoriting logic Fixes #2192 and #2489 --- .../browse/BrowseCatalogueController.kt | 34 +++++++++--- .../browse/BrowseCataloguePresenter.kt | 10 ++-- .../ui/manga/info/MangaInfoController.kt | 52 +++++++++++-------- 3 files changed, 65 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt index 469fe733c..37619f7f2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt @@ -495,6 +495,7 @@ open class BrowseCatalogueController(bundle: Bundle) : override fun onItemLongClick(position: Int) { val activity = activity ?: return val manga = (adapter?.getItem(position) as? CatalogueItem?)?.manga ?: return + if (manga.favorite) { MaterialDialog.Builder(activity) .items(activity.getString(R.string.remove_from_library)) @@ -508,16 +509,30 @@ open class BrowseCatalogueController(bundle: Bundle) : } }.show() } else { - presenter.changeMangaFavorite(manga) - adapter?.notifyItemChanged(position) - val categories = presenter.getCategories() val defaultCategoryId = preferences.defaultCategory() val defaultCategory = categories.find { it.id == defaultCategoryId } + when { - defaultCategory != null -> presenter.moveMangaToCategory(manga, defaultCategory) - defaultCategoryId == 0 || categories.isEmpty() -> // 'Default' or no category + // Default category set + defaultCategory != null -> { + presenter.moveMangaToCategory(manga, defaultCategory) + + presenter.changeMangaFavorite(manga) + adapter?.notifyItemChanged(position) + activity.toast(activity.getString(R.string.manga_added_library)) + } + + // Automatic 'Default' or no categories + defaultCategoryId == 0 || categories.isEmpty() -> { presenter.moveMangaToCategory(manga, null) + + presenter.changeMangaFavorite(manga) + adapter?.notifyItemChanged(position) + activity.toast(activity.getString(R.string.manga_added_library)) + } + + // Choose a category else -> { val ids = presenter.getMangaCategoryIds(manga) val preselected = ids.mapNotNull { id -> @@ -528,7 +543,6 @@ open class BrowseCatalogueController(bundle: Bundle) : .showDialog(router) } } - activity.toast(activity.getString(R.string.manga_added_library)) } } @@ -540,7 +554,15 @@ open class BrowseCatalogueController(bundle: Bundle) : */ override fun updateCategoriesForMangas(mangas: List, categories: List) { val manga = mangas.firstOrNull() ?: return + + presenter.changeMangaFavorite(manga) presenter.updateMangaCategories(manga, categories) + + val position = adapter?.currentItems?.indexOfFirst { it -> (it as CatalogueItem).manga.id == manga.id } + if (position != null) { + adapter?.notifyItemChanged(position) + } + activity?.toast(activity?.getString(R.string.manga_added_library)) } protected companion object { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCataloguePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCataloguePresenter.kt index fd0c3ea8b..48416bad9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCataloguePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCataloguePresenter.kt @@ -375,13 +375,15 @@ open class BrowseCataloguePresenter( * @param selectedCategories selected categories */ fun updateMangaCategories(manga: Manga, selectedCategories: List) { - if (selectedCategories.isNotEmpty()) { - if (!manga.favorite) - changeMangaFavorite(manga) + if (!manga.favorite) { + changeMangaFavorite(manga) + } + if (selectedCategories.isNotEmpty()) { moveMangaToCategories(manga, selectedCategories.filter { it.id != 0 }) } else { - changeMangaFavorite(manga) + // Default category + moveMangaToCategories(manga, emptyList()) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt index d2756d1ef..771d6c781 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt @@ -380,20 +380,33 @@ class MangaInfoController : NucleusController(), swipe_refresh?.isRefreshing = value } - /** - * Called when the fab is clicked. - */ private fun onFabClick() { val manga = presenter.manga - toggleFavorite() + if (manga.favorite) { + toggleFavorite() + activity?.toast(activity?.getString(R.string.manga_removed_library)) + } else { val categories = presenter.getCategories() val defaultCategoryId = preferences.defaultCategory() val defaultCategory = categories.find { it.id == defaultCategoryId } + when { - defaultCategory != null -> presenter.moveMangaToCategory(manga, defaultCategory) - defaultCategoryId == 0 || categories.isEmpty() -> // 'Default' or no category + // Default category set + defaultCategory != null -> { + toggleFavorite() + presenter.moveMangaToCategory(manga, defaultCategory) + activity?.toast(activity?.getString(R.string.manga_added_library)) + } + + // Automatic 'Default' or no categories + defaultCategoryId == 0 || categories.isEmpty() -> { + toggleFavorite() presenter.moveMangaToCategory(manga, null) + activity?.toast(activity?.getString(R.string.manga_added_library)) + } + + // Choose a category else -> { val ids = presenter.getMangaCategoryIds(manga) val preselected = ids.mapNotNull { id -> @@ -404,26 +417,15 @@ class MangaInfoController : NucleusController(), .showDialog(router) } } - activity?.toast(activity?.getString(R.string.manga_added_library)) - } else { - activity?.toast(activity?.getString(R.string.manga_removed_library)) } } - /** - * Called when the fab is long clicked. - */ private fun onFabLongClick() { val manga = presenter.manga - if (!manga.favorite) { - toggleFavorite() - activity?.toast(activity?.getString(R.string.manga_added_library)) - } - val categories = presenter.getCategories() - if (categories.isEmpty()) { - // no categories exist, display a message about adding categories - activity?.toast(activity?.getString(R.string.action_add_category)) - } else { + + if (manga.favorite && presenter.getCategories().isNotEmpty()) { + val categories = presenter.getCategories() + val ids = presenter.getMangaCategoryIds(manga) val preselected = ids.mapNotNull { id -> categories.indexOfFirst { it.id == id }.takeIf { it != -1 } @@ -431,12 +433,20 @@ class MangaInfoController : NucleusController(), ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected) .showDialog(router) + } else { + onFabClick() } } override fun updateCategoriesForMangas(mangas: List, categories: List) { val manga = mangas.firstOrNull() ?: return + + if (!manga.favorite) { + toggleFavorite() + } + presenter.moveMangaToCategories(manga, categories) + activity?.toast(activity?.getString(R.string.manga_added_library)) } /**