From e421eb61bc7b4bcaf10b7e165358de83d2bfe368 Mon Sep 17 00:00:00 2001 From: arkon Date: Tue, 10 May 2022 09:19:10 -0400 Subject: [PATCH] Extract add duplicate manga dialog into a controller --- .../ui/base/controller/DialogController.kt | 2 +- .../ui/manga/AddDuplicateMangaDialog.kt | 49 +++++++++++++++++++ .../tachiyomi/ui/manga/MangaController.kt | 38 ++++---------- 3 files changed, 59 insertions(+), 30 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/base/controller/DialogController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/DialogController.kt index 847f97214..63f63e1d0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/DialogController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/DialogController.kt @@ -97,7 +97,7 @@ abstract class DialogController : Controller { /** * Dismiss the dialog and pop this controller */ - private fun dismissDialog() { + fun dismissDialog() { if (dismissed) { return } 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 000000000..5121f44cc --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/AddDuplicateMangaDialog.kt @@ -0,0 +1,49 @@ +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.pushController +import uy.kohesive.injekt.injectLazy + +class AddDuplicateMangaDialog(bundle: Bundle? = null) : DialogController(bundle) + where T : Controller, T : AddDuplicateMangaDialog.Listener { + + private val sourceManager: SourceManager by injectLazy() + + private lateinit var libraryManga: Manga + private lateinit var newManga: Manga + + constructor(target: T, libraryManga: Manga, newManga: Manga) : this() { + targetController = target + + this.libraryManga = libraryManga + this.newManga = newManga + } + + 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)) { _, _ -> + (targetController as? Listener)?.addToLibrary(newManga) + } + .setNegativeButton(android.R.string.cancel, null) + .setNeutralButton(activity?.getString(R.string.action_show_manga)) { _, _ -> + dismissDialog() + router.pushController(MangaController(libraryManga)) + } + .setCancelable(true) + .create() + } + + interface Listener { + fun addToLibrary(newManga: Manga) + } +} 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 60f0929d8..e36f5a203 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 @@ -114,6 +113,7 @@ class MangaController : FlexibleAdapter.OnItemClickListener, FlexibleAdapter.OnItemLongClickListener, BaseChaptersAdapter.OnChapterClickListener, + AddDuplicateMangaDialog.Listener, ChangeMangaCoverDialog.Listener, ChangeMangaCategoriesDialog.Listener, DownloadCustomChaptersDialog.Listener, @@ -521,38 +521,18 @@ class MangaController : } else { val duplicateManga = presenter.getDuplicateLibraryManga(manga) if (duplicateManga != null) { - showAddDuplicateDialog( - manga, - duplicateManga, - ) + AddDuplicateMangaDialog(this, duplicateManga, manga).showDialog(router) } else { addToLibrary(manga) } } } - 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)) - } - setCancelable(true) - }.create().show() - } - } - fun onTrackingClick() { trackSheet?.show() } - private fun addToLibrary(manga: Manga) { + override fun addToLibrary(newManga: Manga) { val categories = presenter.getCategories() val defaultCategoryId = preferences.defaultCategory() val defaultCategory = categories.find { it.id == defaultCategoryId } @@ -561,7 +541,7 @@ class MangaController : // Default category set defaultCategory != null -> { toggleFavorite() - presenter.moveMangaToCategory(manga, defaultCategory) + presenter.moveMangaToCategory(newManga, defaultCategory) activity?.toast(activity?.getString(R.string.manga_added_library)) activity?.invalidateOptionsMenu() } @@ -569,14 +549,14 @@ class MangaController : // Automatic 'Default' or no categories defaultCategoryId == 0 || categories.isEmpty() -> { toggleFavorite() - presenter.moveMangaToCategory(manga, null) + presenter.moveMangaToCategory(newManga, null) activity?.toast(activity?.getString(R.string.manga_added_library)) activity?.invalidateOptionsMenu() } // Choose a category else -> { - val ids = presenter.getMangaCategoryIds(manga) + val ids = presenter.getMangaCategoryIds(newManga) val preselected = categories.map { if (it.id in ids) { QuadStateTextView.State.CHECKED.ordinal @@ -585,7 +565,7 @@ class MangaController : } }.toTypedArray() - showChangeCategoryDialog(manga, categories, preselected) + showChangeCategoryDialog(newManga, categories, preselected) } } @@ -597,12 +577,12 @@ class MangaController : .forEach { service -> launchIO { try { - service.match(manga)?.let { track -> + service.match(newManga)?.let { track -> presenter.registerTracking(track, service as TrackService) } } catch (e: Exception) { logcat(LogPriority.WARN, e) { - "Could not match manga: ${manga.title} with service $service" + "Could not match manga: ${newManga.title} with service $service" } } }