From 126cbbe36c220af8aab06ca0fcaa00c87afcd03f Mon Sep 17 00:00:00 2001 From: Carlos <2092019+CarlosEsco@users.noreply.github.com> Date: Mon, 27 Apr 2020 21:57:45 -0400 Subject: [PATCH] Add a new manga category dialog (#221) makes it more clear in browse and manga details when adding a manga --- .../library/AddToLibraryCategoriesDialog.kt | 63 +++++++++++++++++++ .../ui/manga/MangaDetailsController.kt | 16 ++++- .../source/browse/BrowseSourceController.kt | 21 +++++-- .../ui/source/browse/BrowseSourcePresenter.kt | 3 +- app/src/main/res/values/strings.xml | 1 + 5 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/library/AddToLibraryCategoriesDialog.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/AddToLibraryCategoriesDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/AddToLibraryCategoriesDialog.kt new file mode 100644 index 0000000000..076750e354 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/AddToLibraryCategoriesDialog.kt @@ -0,0 +1,63 @@ +package eu.kanade.tachiyomi.ui.library + +import android.app.Dialog +import android.os.Bundle +import com.afollestad.materialdialogs.MaterialDialog +import com.afollestad.materialdialogs.list.listItemsMultiChoice +import com.bluelinelabs.conductor.Controller +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.models.Category +import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.ui.base.controller.DialogController + +/** + * This class is used when adding new manga to your library + */ +class AddToLibraryCategoriesDialog(bundle: Bundle? = null) : + DialogController(bundle) where T : Controller, T : AddToLibraryCategoriesDialog.Listener { + + private var manga: Manga? = null + + private var categories = emptyList() + + private var preselected = emptyArray() + + private var position = 0 + + constructor( + target: T, + manga: Manga, + categories: List, + preselected: Array, + position: Int = 0 + ) : this() { + + this.manga = manga + this.categories = categories + this.preselected = preselected + this.position = position + targetController = target + } + + override fun onCreateDialog(savedViewState: Bundle?): Dialog { + + return MaterialDialog(activity!!).title(R.string.add_to_library).message(R.string.add_to_categories) + .listItemsMultiChoice( + items = categories.map { it.name }, + initialSelection = preselected.toIntArray(), + allowEmptySelection = true + ) { _, selections, _ -> + val newCategories = selections.map { categories[it] } + (targetController as? Listener)?.updateCategoriesForManga(manga, newCategories) + } + .positiveButton(android.R.string.ok) + .negativeButton(android.R.string.cancel) { + (targetController as? Listener)?.addToLibraryCancelled(manga, position) + } + } + + interface Listener { + fun updateCategoriesForManga(manga: Manga?, categories: List) + fun addToLibraryCancelled(manga: Manga?, position: Int) + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt index 45079a00b5..8ffc357aeb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt @@ -77,6 +77,7 @@ import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.controller.BaseController import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder +import eu.kanade.tachiyomi.ui.library.AddToLibraryCategoriesDialog import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.library.LibraryController import eu.kanade.tachiyomi.ui.main.MainActivity @@ -128,7 +129,8 @@ class MangaDetailsController : BaseController, ActionMode.Callback, MangaDetailsAdapter.MangaDetailsInterface, FlexibleAdapter.OnItemMoveListener, - ChangeMangaCategoriesDialog.Listener { + ChangeMangaCategoriesDialog.Listener, + AddToLibraryCategoriesDialog.Listener { constructor( manga: Manga?, @@ -1128,9 +1130,9 @@ class MangaDetailsController : BaseController, categories.indexOfFirst { it.id == id }.takeIf { it != -1 } }.toTypedArray() - ChangeMangaCategoriesDialog( + AddToLibraryCategoriesDialog( this, - listOf(manga), + manga, categories, preselected ).showDialog(router) @@ -1176,6 +1178,14 @@ class MangaDetailsController : BaseController, presenter.moveMangaToCategories(manga, categories) } + override fun updateCategoriesForManga(manga: Manga?, categories: List) { + manga?.let { presenter.moveMangaToCategories(manga, categories) } + } + + override fun addToLibraryCancelled(manga: Manga?, position: Int) { + manga?.let { presenter.toggleFavorite() } + } + /** * Copies a string to clipboard * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt index 6f165b1ec3..35d7a6299a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourceController.kt @@ -27,7 +27,7 @@ import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.ui.base.controller.NucleusController -import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog +import eu.kanade.tachiyomi.ui.library.AddToLibraryCategoriesDialog import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaDetailsController import eu.kanade.tachiyomi.ui.source.SourceController @@ -61,7 +61,7 @@ open class BrowseSourceController(bundle: Bundle) : FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemLongClickListener, FlexibleAdapter.EndlessScrollListener, - ChangeMangaCategoriesDialog.Listener { + AddToLibraryCategoriesDialog.Listener { constructor( source: CatalogueSource, @@ -592,7 +592,7 @@ open class BrowseSourceController(bundle: Bundle) : categories.indexOfFirst { it.id == id }.takeIf { it != -1 } }.toTypedArray() - ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected) + AddToLibraryCategoriesDialog(this, manga, categories, preselected, position) .showDialog(router) } } @@ -604,9 +604,18 @@ open class BrowseSourceController(bundle: Bundle) : * @param mangas The list of manga to move to categories. * @param categories The list of categories where manga will be placed. */ - override fun updateCategoriesForMangas(mangas: List, categories: List) { - val manga = mangas.firstOrNull() ?: return - presenter.updateMangaCategories(manga, categories) + override fun updateCategoriesForManga(manga: Manga?, categories: List) { + manga?.let { presenter.updateMangaCategories(manga, categories) } + } + + /** + * Update manga to remove from favorites + */ + override fun addToLibraryCancelled(manga: Manga?, position: Int) { + manga?.let { + presenter.changeMangaFavorite(manga) + adapter?.notifyItemChanged(position) + } } protected companion object { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourcePresenter.kt index ce2d06009f..551b761749 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/browse/BrowseSourcePresenter.kt @@ -400,7 +400,8 @@ open class BrowseSourcePresenter( moveMangaToCategories(manga, selectedCategories.filter { it.id != 0 }) } else { - changeMangaFavorite(manga) + if (!manga.favorite) + changeMangaFavorite(manga) } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7b7f199d44..ef0031b6bc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,6 +81,7 @@ Manage category Rename category Move to categories + Choose which categories to add this to. If none are selected, this will be added to the "default" category Hide categories %d category