From 431c04e54f4acd1c660e83c27886c38955b67871 Mon Sep 17 00:00:00 2001 From: CVIUS <84634607+CVIUS@users.noreply.github.com> Date: Thu, 12 May 2022 20:58:37 +0800 Subject: [PATCH] Detect identical mangas when long pressing to add to library (#7095) * Detect identical mangas when long pressing to add to library * Use extracted duplicate manga dialog to avoid duplication * Partially revert previous commit * Review changes * Review changes part 2 (cherry picked from commit f1afeac0bcd3904c323e24d67dd945c85c666f92) --- .../details/ExtensionDetailsController.kt | 2 +- .../source/browse/BrowseSourceController.kt | 74 +++++++++++-------- .../source/browse/BrowseSourcePresenter.kt | 4 + .../ui/manga/AddDuplicateMangaDialog.kt | 48 ++++++++++++ .../tachiyomi/ui/manga/MangaController.kt | 15 +--- 5 files changed, 98 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/AddDuplicateMangaDialog.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt index c80f1a9d42..d0634cda65 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt @@ -253,7 +253,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) : else -> "$url/multisrc/overrides/$pkgFactory/" + (pkgName.split(".").lastOrNull() ?: "") + path } } else { - url + "/src/" + pkgName.replace(".", "/") + path + url + "/src/" + pkgName.replace(".", "/") + path } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt index ab20ee493d..cdf418c906 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourceController.kt @@ -37,6 +37,7 @@ import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchController import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting import eu.kanade.tachiyomi.ui.main.MainActivity +import eu.kanade.tachiyomi.ui.manga.AddDuplicateMangaDialog import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.ui.more.MoreController import eu.kanade.tachiyomi.ui.webview.WebViewActivity @@ -588,6 +589,7 @@ open class BrowseSourceController(bundle: Bundle) : override fun onItemLongClick(position: Int) { val activity = activity ?: return val manga = (adapter?.getItem(position) as? SourceItem?)?.manga ?: return + val duplicateManga = presenter.getDuplicateLibraryManga(manga) if (manga.favorite) { MaterialAlertDialogBuilder(activity) @@ -603,43 +605,53 @@ open class BrowseSourceController(bundle: Bundle) : } .show() } else { - val categories = presenter.getCategories() - val defaultCategoryId = preferences.defaultCategory() - val defaultCategory = categories.find { it.id == defaultCategoryId } + if (duplicateManga != null) { + AddDuplicateMangaDialog(this, duplicateManga) { addToLibrary(manga, position) } + .showDialog(router) + } else { + addToLibrary(manga, position) + } + } + } - when { - // Default category set - defaultCategory != null -> { - presenter.moveMangaToCategory(manga, defaultCategory) + private fun addToLibrary(newManga: Manga, position: Int) { + val activity = activity ?: return + val categories = presenter.getCategories() + val defaultCategoryId = preferences.defaultCategory() + val defaultCategory = categories.find { it.id == defaultCategoryId } - presenter.changeMangaFavorite(manga) - adapter?.notifyItemChanged(position) - activity.toast(activity.getString(R.string.manga_added_library)) - } + when { + // Default category set + defaultCategory != null -> { + presenter.moveMangaToCategory(newManga, defaultCategory) - // Automatic 'Default' or no categories - defaultCategoryId == 0 || categories.isEmpty() -> { - presenter.moveMangaToCategory(manga, null) + presenter.changeMangaFavorite(newManga) + adapter?.notifyItemChanged(position) + activity.toast(activity.getString(R.string.manga_added_library)) + } - 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(newManga, null) - // Choose a category - else -> { - val ids = presenter.getMangaCategoryIds(manga) - val preselected = categories.map { - if (it.id in ids) { - QuadStateTextView.State.CHECKED.ordinal - } else { - QuadStateTextView.State.UNCHECKED.ordinal - } - }.toTypedArray() + presenter.changeMangaFavorite(newManga) + adapter?.notifyItemChanged(position) + activity.toast(activity.getString(R.string.manga_added_library)) + } - ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected) - .showDialog(router) - } + // Choose a category + else -> { + val ids = presenter.getMangaCategoryIds(newManga) + val preselected = categories.map { + if (it.id in ids) { + QuadStateTextView.State.CHECKED.ordinal + } else { + QuadStateTextView.State.UNCHECKED.ordinal + } + }.toTypedArray() + + ChangeMangaCategoriesDialog(this, listOf(newManga), categories, preselected) + .showDialog(router) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt index 1bae71a314..afcb0c60ef 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/BrowseSourcePresenter.kt @@ -351,6 +351,10 @@ open class BrowseSourcePresenter( return db.getCategories().executeAsBlocking() } + fun getDuplicateLibraryManga(manga: Manga): Manga? { + return db.getDuplicateLibraryManga(manga).executeAsBlocking() + } + /** * Gets the category id's the manga is in, if the manga is not in a category, returns the default id. * diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/AddDuplicateMangaDialog.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/AddDuplicateMangaDialog.kt new file mode 100644 index 0000000000..8cac9c030f --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/AddDuplicateMangaDialog.kt @@ -0,0 +1,48 @@ +package eu.kanade.tachiyomi.ui.manga + +import android.app.Dialog +import android.os.Bundle +import com.bluelinelabs.conductor.Controller +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.source.SourceManager +import eu.kanade.tachiyomi.ui.base.controller.DialogController +import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction +import uy.kohesive.injekt.injectLazy + +class AddDuplicateMangaDialog(bundle: Bundle? = null) : DialogController(bundle) { + + private val sourceManager: SourceManager by injectLazy() + + private lateinit var libraryManga: Manga + private lateinit var onAddToLibrary: () -> Unit + + constructor( + target: Controller, + libraryManga: Manga, + onAddToLibrary: () -> Unit, + ) : this() { + targetController = target + + this.libraryManga = libraryManga + this.onAddToLibrary = onAddToLibrary + } + + override fun onCreateDialog(savedViewState: Bundle?): Dialog { + val source = sourceManager.getOrStub(libraryManga.source) + + return MaterialAlertDialogBuilder(activity!!) + .setMessage(activity?.getString(R.string.confirm_manga_add_duplicate, source.name)) + .setPositiveButton(activity?.getString(R.string.action_add)) { _, _ -> + onAddToLibrary() + } + .setNegativeButton(android.R.string.cancel, null) + .setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _ -> + dismissDialog() + router.pushController(MangaController(libraryManga.id!!).withFadeTransaction()) + } + .setCancelable(true) + .create() + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index c381f1194b..85fdccdcc9 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -29,7 +29,6 @@ import coil.request.ImageRequest import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.ControllerChangeHandler import com.bluelinelabs.conductor.ControllerChangeType -import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton import com.google.android.material.snackbar.Snackbar import dev.chrisbanes.insetter.applyInsetter @@ -542,18 +541,8 @@ class MangaController : private fun showAddDuplicateDialog(newManga: Manga, libraryManga: Manga) { activity?.let { - val source = sourceManager.getOrStub(libraryManga.source) - MaterialAlertDialogBuilder(it).apply { - setMessage(activity?.getString(R.string.confirm_manga_add_duplicate, source.name)) - setPositiveButton(activity?.getString(R.string.action_add)) { _, _ -> - addToLibrary(newManga) - } - setNegativeButton(activity?.getString(R.string.action_cancel)) { _, _ -> } - setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _ -> - router.pushController(MangaController(libraryManga).withFadeTransaction()) - } - setCancelable(true) - }.create().show() + AddDuplicateMangaDialog(this, libraryManga) { addToLibrary(newManga) } + .showDialog(router) } }