refactor fav popup

This commit is contained in:
Jay 2020-08-09 16:41:21 -04:00
parent 198cf86377
commit b4151e6761

View File

@ -991,30 +991,8 @@ class MangaDetailsController : BaseController,
if (!manga.favorite) {
toggleMangaFavorite()
} else {
val headerHolder = getHeader() ?: return
val popup = PopupMenu(view!!.context, headerHolder.favorite_button)
popup.menu.add(0, 1, 0, R.string.remove_from_library)
if (categories.isNotEmpty()) {
popup.menu.add(0, 0, 1, R.string.edit_categories)
}
// Set a listener so we are notified if a menu item is clicked
popup.setOnMenuItemClickListener { menuItem ->
if (menuItem.itemId == 0) {
val ids = presenter.getMangaCategoryIds()
val preselected = ids.mapNotNull { id ->
categories.indexOfFirst { it.id == id }.takeIf { it != -1 }
}.toTypedArray()
ChangeMangaCategoriesDialog(
this, listOf(manga), categories, preselected
).showDialog(
router
)
} else {
toggleMangaFavorite()
}
true
}
val favButton = getHeader()?.favorite_button ?: return
val popup = makeFavPopup(favButton, manga, categories)
popup.show()
}
}
@ -1026,8 +1004,12 @@ class MangaDetailsController : BaseController,
popupView.setOnTouchListener(null)
return
}
val popup = makeFavPopup(popupView, manga, presenter.getCategories())
popupView.setOnTouchListener(popup.dragToOpenListener)
}
fun makeFavPopup(popupView: View, manga: Manga, categories: List<Category>): PopupMenu {
val popup = PopupMenu(view!!.context, popupView)
val categories = presenter.getCategories()
popup.menu.add(0, 1, 0, R.string.remove_from_library)
if (categories.isNotEmpty()) {
popup.menu.add(0, 0, 1, R.string.edit_categories)
@ -1050,7 +1032,7 @@ class MangaDetailsController : BaseController,
}
true
}
popupView.setOnTouchListener(popup.dragToOpenListener)
return popup
}
private fun toggleMangaFavorite() {