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)
(cherry picked from commit afd1c3b49155e68011f24593a2663c285d6c4e66)
This commit is contained in:
CVIUS 2022-05-12 20:58:37 +08:00 committed by Jobobby04
parent 5d5678861d
commit 5dcdd3454b
2 changed files with 47 additions and 31 deletions

View File

@ -41,6 +41,7 @@ import eu.kanade.tachiyomi.ui.browse.source.globalsearch.GlobalSearchController
import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog
import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting import eu.kanade.tachiyomi.ui.library.setting.DisplayModeSetting
import eu.kanade.tachiyomi.ui.main.MainActivity 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.manga.MangaController
import eu.kanade.tachiyomi.ui.more.MoreController import eu.kanade.tachiyomi.ui.more.MoreController
import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.ui.webview.WebViewActivity
@ -742,6 +743,7 @@ open class BrowseSourceController(bundle: Bundle) :
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
val activity = activity ?: return val activity = activity ?: return
val manga = (adapter?.getItem(position) as? SourceItem?)?.manga ?: return val manga = (adapter?.getItem(position) as? SourceItem?)?.manga ?: return
val duplicateManga = presenter.getDuplicateLibraryManga(manga)
if (manga.favorite) { if (manga.favorite) {
MaterialAlertDialogBuilder(activity) MaterialAlertDialogBuilder(activity)
@ -757,6 +759,17 @@ open class BrowseSourceController(bundle: Bundle) :
} }
.show() .show()
} else { } else {
if (duplicateManga != null) {
AddDuplicateMangaDialog(this, duplicateManga) { addToLibrary(manga, position) }
.showDialog(router)
} else {
addToLibrary(manga, position)
}
}
}
private fun addToLibrary(newManga: Manga, position: Int) {
val activity = activity ?: return
val categories = presenter.getCategories() val categories = presenter.getCategories()
val defaultCategoryId = preferences.defaultCategory() val defaultCategoryId = preferences.defaultCategory()
val defaultCategory = categories.find { it.id == defaultCategoryId } val defaultCategory = categories.find { it.id == defaultCategoryId }
@ -764,25 +777,25 @@ open class BrowseSourceController(bundle: Bundle) :
when { when {
// Default category set // Default category set
defaultCategory != null -> { defaultCategory != null -> {
presenter.moveMangaToCategory(manga, defaultCategory) presenter.moveMangaToCategory(newManga, defaultCategory)
presenter.changeMangaFavorite(manga) presenter.changeMangaFavorite(newManga)
adapter?.notifyItemChanged(position) adapter?.notifyItemChanged(position)
activity.toast(activity.getString(R.string.manga_added_library)) activity.toast(activity.getString(R.string.manga_added_library))
} }
// Automatic 'Default' or no categories // Automatic 'Default' or no categories
defaultCategoryId == 0 || categories.isEmpty() -> { defaultCategoryId == 0 || categories.isEmpty() -> {
presenter.moveMangaToCategory(manga, null) presenter.moveMangaToCategory(newManga, null)
presenter.changeMangaFavorite(manga) presenter.changeMangaFavorite(newManga)
adapter?.notifyItemChanged(position) adapter?.notifyItemChanged(position)
activity.toast(activity.getString(R.string.manga_added_library)) activity.toast(activity.getString(R.string.manga_added_library))
} }
// Choose a category // Choose a category
else -> { else -> {
val ids = presenter.getMangaCategoryIds(manga) val ids = presenter.getMangaCategoryIds(newManga)
val preselected = categories.map { val preselected = categories.map {
if (it.id in ids) { if (it.id in ids) {
QuadStateTextView.State.CHECKED.ordinal QuadStateTextView.State.CHECKED.ordinal
@ -791,12 +804,11 @@ open class BrowseSourceController(bundle: Bundle) :
} }
}.toTypedArray() }.toTypedArray()
ChangeMangaCategoriesDialog(this, listOf(manga), categories, preselected) ChangeMangaCategoriesDialog(this, listOf(newManga), categories, preselected)
.showDialog(router) .showDialog(router)
} }
} }
} }
}
/** /**
* Update manga to use selected categories. * Update manga to use selected categories.

View File

@ -435,6 +435,10 @@ open class BrowseSourcePresenter(
return db.getCategories().executeAsBlocking() 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. * Gets the category id's the manga is in, if the manga is not in a category, returns the default id.
* *