From 0390dd7672cd88717f9ec682915cb776b6575cbf Mon Sep 17 00:00:00 2001 From: Felix Kaiser Date: Sun, 6 Feb 2022 02:04:55 +0100 Subject: [PATCH] added duplicate manga check When adding a manga to your library, the app will go through each manga previously added and compare their names. If a match is detected, it will prompt the user and ask for confirmation. On this prompt there is also an option to view the other manga. --- .../tachiyomi/ui/manga/MangaController.kt | 48 ++++++++++++++++++- .../tachiyomi/ui/manga/MangaPresenter.kt | 5 ++ app/src/main/res/values/strings.xml | 2 + 3 files changed, 54 insertions(+), 1 deletion(-) 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 06cb69ad94..51f4dc44c7 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 @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.manga import android.app.Activity import android.app.ActivityOptions +import android.app.AlertDialog import android.content.Context import android.content.Intent import android.graphics.Bitmap @@ -516,10 +517,55 @@ class MangaController : activity?.toast(activity?.getString(R.string.manga_removed_library)) activity?.invalidateOptionsMenu() } else { - addToLibrary(manga) + if (!isDuplicateOfOtherSource(manga)) { + addToLibrary(manga) + } } } + private fun isDuplicateOfOtherSource(origin: Manga): Boolean { + var result = false + val mangas = presenter.getAllMangaMutableList() + + mangas.forEach { item -> + if (isSameManga(origin, item)) { + showAddDuplicateDialog( + origin, + item, + getSourceFromLong(item.source) + ) + result = true + return@forEach + } + } + + return result + } + + private fun isSameManga(origin: Manga, other: Manga): Boolean { + return origin.title.equals(other.title, ignoreCase = true) && + // Source check, just in case we fetched the library list before it got removed + origin.source != other.source + } + + private fun getSourceFromLong(sourceId: Long): Source { + return Injekt.get().getOrStub(sourceId) + } + + private fun showAddDuplicateDialog(manga: Manga, other: Manga, source: Source) { + AlertDialog.Builder(activity).apply { + setMessage(activity?.getString(R.string.confirm_manga_add_duplicate, source.name)) + setPositiveButton(activity?.getString(R.string.action_add)) { _, _, -> + addToLibrary(manga) + } + setNegativeButton(activity?.getString(R.string.action_cancel)) { _, _, -> } + setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _, -> + router.pushController(MangaController(other).withFadeTransaction()) + } + setCancelable(true) + }.create().show() + } + fun onTrackingClick() { trackSheet?.show() } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt index beccee6b99..6ed82224ec 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaPresenter.kt @@ -12,6 +12,7 @@ import eu.kanade.tachiyomi.data.cache.CoverCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Category import eu.kanade.tachiyomi.data.database.models.Chapter +import eu.kanade.tachiyomi.data.database.models.LibraryManga import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.MangaCategory import eu.kanade.tachiyomi.data.database.models.Track @@ -174,6 +175,10 @@ class MangaPresenter( fetchTrackers() } + fun getAllMangaMutableList(): MutableList { + return db.getLibraryMangas().executeAsBlocking() + } + // Manga info - start private fun getMangaObservable(): Observable { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21b622b3e6..a066afc412 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -81,6 +81,7 @@ Start Resume Open in browser + Show manga Open in WebView WebView @@ -571,6 +572,7 @@ In library Remove from library Title + We found a manga in your library with the same name, but from a different source (%1$s).\n\nDo you still wish to continue? Added to library Removed from library More