Long press in global search to add to/remove from library

This commit is contained in:
Jays2Kings 2021-04-24 04:02:01 -04:00
parent a33f339c19
commit 9e813f773b
5 changed files with 55 additions and 10 deletions

View File

@ -18,6 +18,8 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.ui.main.BottomNavBarInterface
import eu.kanade.tachiyomi.ui.migration.manga.process.MigrationListController
import eu.kanade.tachiyomi.ui.source.browse.BrowseSourceItem
import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchCardAdapter
import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchController
import eu.kanade.tachiyomi.ui.source.global_search.GlobalSearchPresenter
import eu.kanade.tachiyomi.util.view.withFadeTransaction
@ -126,8 +128,9 @@ class SearchController(
dialog.showDialog(router)
}
override fun onMangaLongClick(manga: Manga) {
override fun onMangaLongClick(position: Int, adapter: GlobalSearchCardAdapter) {
// Call parent's default click listener
val manga = adapter.getItem(position)?.manga ?: return
super.onMangaClick(manga)
}

View File

@ -22,6 +22,6 @@ class GlobalSearchCardAdapter(controller: GlobalSearchController) :
*/
interface OnMangaClickListener {
fun onMangaClick(manga: Manga)
fun onMangaLongClick(manga: Manga)
fun onMangaLongClick(position: Int, adapter: GlobalSearchCardAdapter)
}
}

View File

@ -7,17 +7,23 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import androidx.appcompat.widget.SearchView
import com.google.android.material.snackbar.Snackbar
import com.jakewharton.rxbinding.support.v7.widget.queryTextChangeEvents
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.SourceGlobalSearchControllerBinding
import eu.kanade.tachiyomi.source.CatalogueSource
import eu.kanade.tachiyomi.ui.base.controller.NucleusController
import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaDetailsController
import eu.kanade.tachiyomi.util.addOrRemoveToFavorites
import eu.kanade.tachiyomi.util.view.activityBinding
import eu.kanade.tachiyomi.util.view.scrollViewWith
import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.util.view.updatePaddingRelative
import eu.kanade.tachiyomi.util.view.withFadeTransaction
import uy.kohesive.injekt.injectLazy
/**
* This controller shows and manages the different search result in global search.
@ -30,6 +36,11 @@ open class GlobalSearchController(
) : NucleusController<SourceGlobalSearchControllerBinding, GlobalSearchPresenter>(),
GlobalSearchCardAdapter.OnMangaClickListener {
/**
* Preferences helper.
*/
private val preferences: PreferencesHelper by injectLazy()
/**
* Adapter containing search results grouped by lang.
*/
@ -37,6 +48,11 @@ open class GlobalSearchController(
private var customTitle: String? = null
/**
* Snackbar containing an error message when a request fails.
*/
private var snack: Snackbar? = null
/**
* Called when controller is initialized.
*/
@ -79,9 +95,27 @@ open class GlobalSearchController(
*
* @param manga clicked item containing manga information.
*/
override fun onMangaLongClick(manga: Manga) {
// Delegate to single click by default.
onMangaClick(manga)
override fun onMangaLongClick(position: Int, adapter: GlobalSearchCardAdapter) {
val manga = adapter.getItem(position)?.manga ?: return
val view = view ?: return
val activity = activity ?: return
snack?.dismiss()
snack = manga.addOrRemoveToFavorites(
presenter.db,
preferences,
view,
activity,
onMangaAdded = {
adapter.notifyItemChanged(position)
snack = view.snack(R.string.added_to_library)
},
onMangaMoved = { adapter.notifyItemChanged(position) },
onMangaDeleted = { presenter.confirmDeletion(manga) }
)
if (snack?.duration == Snackbar.LENGTH_INDEFINITE) {
(activity as? MainActivity)?.setUndoSnackBar(snack)
}
}
/**

View File

@ -24,10 +24,7 @@ class GlobalSearchMangaHolder(view: View, adapter: GlobalSearchCardAdapter) :
}
}
itemView.setOnLongClickListener {
val item = adapter.getItem(flexibleAdapterPosition)
if (item != null) {
adapter.mangaClickListener.onMangaLongClick(item.manga)
}
adapter.mangaClickListener.onMangaLongClick(flexibleAdapterPosition, adapter)
true
}
}

View File

@ -1,8 +1,10 @@
package eu.kanade.tachiyomi.ui.source.global_search
import android.os.Bundle
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.extension.ExtensionManager
@ -38,7 +40,8 @@ open class GlobalSearchPresenter(
private val sourcesToUse: List<CatalogueSource>? = null,
val sourceManager: SourceManager = Injekt.get(),
val db: DatabaseHelper = Injekt.get(),
private val preferencesHelper: PreferencesHelper = Injekt.get()
private val preferencesHelper: PreferencesHelper = Injekt.get(),
private val coverCache: CoverCache = Injekt.get()
) : BasePresenter<GlobalSearchController>() {
/**
@ -146,6 +149,14 @@ open class GlobalSearchPresenter(
return GlobalSearchItem(source, results)
}
fun confirmDeletion(manga: Manga) {
coverCache.deleteFromCache(manga)
val downloadManager: DownloadManager = Injekt.get()
sourceManager.get(manga.source)?.let { source ->
downloadManager.deleteManga(manga, source)
}
}
/**
* Initiates a search for manga per catalogue.
*