Extract add duplicate manga dialog into a controller

This commit is contained in:
arkon 2022-05-10 09:19:10 -04:00
parent bc053580ad
commit e421eb61bc
3 changed files with 59 additions and 30 deletions

View file

@ -97,7 +97,7 @@ abstract class DialogController : Controller {
/**
* Dismiss the dialog and pop this controller
*/
private fun dismissDialog() {
fun dismissDialog() {
if (dismissed) {
return
}

View file

@ -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<T>(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)
}
}

View file

@ -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"
}
}
}