Tweak manga favoriting logic

Fixes #2192 and #2489
This commit is contained in:
arkon 2020-02-17 16:27:32 -05:00
parent aa0597da2a
commit 8d712c81d4
3 changed files with 65 additions and 31 deletions

View File

@ -495,6 +495,7 @@ open class BrowseCatalogueController(bundle: Bundle) :
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
val activity = activity ?: return val activity = activity ?: return
val manga = (adapter?.getItem(position) as? CatalogueItem?)?.manga ?: return val manga = (adapter?.getItem(position) as? CatalogueItem?)?.manga ?: return
if (manga.favorite) { if (manga.favorite) {
MaterialDialog.Builder(activity) MaterialDialog.Builder(activity)
.items(activity.getString(R.string.remove_from_library)) .items(activity.getString(R.string.remove_from_library))
@ -508,16 +509,30 @@ open class BrowseCatalogueController(bundle: Bundle) :
} }
}.show() }.show()
} else { } else {
presenter.changeMangaFavorite(manga)
adapter?.notifyItemChanged(position)
val categories = presenter.getCategories() val categories = presenter.getCategories()
val defaultCategoryId = preferences.defaultCategory() val defaultCategoryId = preferences.defaultCategory()
val defaultCategory = categories.find { it.id == defaultCategoryId } val defaultCategory = categories.find { it.id == defaultCategoryId }
when { when {
defaultCategory != null -> presenter.moveMangaToCategory(manga, defaultCategory) // Default category set
defaultCategoryId == 0 || categories.isEmpty() -> // 'Default' or no category 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.moveMangaToCategory(manga, null)
presenter.changeMangaFavorite(manga)
adapter?.notifyItemChanged(position)
activity.toast(activity.getString(R.string.manga_added_library))
}
// Choose a category
else -> { else -> {
val ids = presenter.getMangaCategoryIds(manga) val ids = presenter.getMangaCategoryIds(manga)
val preselected = ids.mapNotNull { id -> val preselected = ids.mapNotNull { id ->
@ -528,7 +543,6 @@ open class BrowseCatalogueController(bundle: Bundle) :
.showDialog(router) .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<Manga>, categories: List<Category>) { override fun updateCategoriesForMangas(mangas: List<Manga>, categories: List<Category>) {
val manga = mangas.firstOrNull() ?: return val manga = mangas.firstOrNull() ?: return
presenter.changeMangaFavorite(manga)
presenter.updateMangaCategories(manga, categories) 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 { protected companion object {

View File

@ -375,13 +375,15 @@ open class BrowseCataloguePresenter(
* @param selectedCategories selected categories * @param selectedCategories selected categories
*/ */
fun updateMangaCategories(manga: Manga, selectedCategories: List<Category>) { fun updateMangaCategories(manga: Manga, selectedCategories: List<Category>) {
if (selectedCategories.isNotEmpty()) { if (!manga.favorite) {
if (!manga.favorite)
changeMangaFavorite(manga) changeMangaFavorite(manga)
}
if (selectedCategories.isNotEmpty()) {
moveMangaToCategories(manga, selectedCategories.filter { it.id != 0 }) moveMangaToCategories(manga, selectedCategories.filter { it.id != 0 })
} else { } else {
changeMangaFavorite(manga) // Default category
moveMangaToCategories(manga, emptyList())
} }
} }
} }

View File

@ -380,20 +380,33 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
swipe_refresh?.isRefreshing = value swipe_refresh?.isRefreshing = value
} }
/**
* Called when the fab is clicked.
*/
private fun onFabClick() { private fun onFabClick() {
val manga = presenter.manga val manga = presenter.manga
toggleFavorite()
if (manga.favorite) { if (manga.favorite) {
toggleFavorite()
activity?.toast(activity?.getString(R.string.manga_removed_library))
} else {
val categories = presenter.getCategories() val categories = presenter.getCategories()
val defaultCategoryId = preferences.defaultCategory() val defaultCategoryId = preferences.defaultCategory()
val defaultCategory = categories.find { it.id == defaultCategoryId } val defaultCategory = categories.find { it.id == defaultCategoryId }
when { when {
defaultCategory != null -> presenter.moveMangaToCategory(manga, defaultCategory) // Default category set
defaultCategoryId == 0 || categories.isEmpty() -> // 'Default' or no category 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) presenter.moveMangaToCategory(manga, null)
activity?.toast(activity?.getString(R.string.manga_added_library))
}
// Choose a category
else -> { else -> {
val ids = presenter.getMangaCategoryIds(manga) val ids = presenter.getMangaCategoryIds(manga)
val preselected = ids.mapNotNull { id -> val preselected = ids.mapNotNull { id ->
@ -404,26 +417,15 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
.showDialog(router) .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() { private fun onFabLongClick() {
val manga = presenter.manga val manga = presenter.manga
if (!manga.favorite) {
toggleFavorite() if (manga.favorite && presenter.getCategories().isNotEmpty()) {
activity?.toast(activity?.getString(R.string.manga_added_library))
}
val categories = presenter.getCategories() 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 {
val ids = presenter.getMangaCategoryIds(manga) val ids = presenter.getMangaCategoryIds(manga)
val preselected = ids.mapNotNull { id -> val preselected = ids.mapNotNull { id ->
categories.indexOfFirst { it.id == id }.takeIf { it != -1 } categories.indexOfFirst { it.id == id }.takeIf { it != -1 }
@ -431,12 +433,20 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected) ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected)
.showDialog(router) .showDialog(router)
} else {
onFabClick()
} }
} }
override fun updateCategoriesForMangas(mangas: List<Manga>, categories: List<Category>) { override fun updateCategoriesForMangas(mangas: List<Manga>, categories: List<Category>) {
val manga = mangas.firstOrNull() ?: return val manga = mangas.firstOrNull() ?: return
if (!manga.favorite) {
toggleFavorite()
}
presenter.moveMangaToCategories(manga, categories) presenter.moveMangaToCategories(manga, categories)
activity?.toast(activity?.getString(R.string.manga_added_library))
} }
/** /**