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) {
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<Manga>, categories: List<Category>) {
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 {

View File

@ -375,13 +375,15 @@ open class BrowseCataloguePresenter(
* @param selectedCategories selected categories
*/
fun updateMangaCategories(manga: Manga, selectedCategories: List<Category>) {
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())
}
}
}

View File

@ -380,20 +380,33 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
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<MangaInfoPresenter>(),
.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<MangaInfoPresenter>(),
ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected)
.showDialog(router)
} else {
onFabClick()
}
}
override fun updateCategoriesForMangas(mangas: List<Manga>, categories: List<Category>) {
val manga = mangas.firstOrNull() ?: return
if (!manga.favorite) {
toggleFavorite()
}
presenter.moveMangaToCategories(manga, categories)
activity?.toast(activity?.getString(R.string.manga_added_library))
}
/**