LibraryItem.kt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package eu.kanade.tachiyomi.ui.library
  2. import android.view.Gravity
  3. import android.view.View
  4. import android.view.ViewGroup.LayoutParams.MATCH_PARENT
  5. import android.widget.FrameLayout
  6. import androidx.recyclerview.widget.RecyclerView
  7. import com.f2prateek.rx.preferences.Preference
  8. import eu.davidea.flexibleadapter.FlexibleAdapter
  9. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
  10. import eu.davidea.flexibleadapter.items.IFilterable
  11. import eu.davidea.flexibleadapter.items.IFlexible
  12. import eu.kanade.tachiyomi.R
  13. import eu.kanade.tachiyomi.data.database.models.LibraryManga
  14. import eu.kanade.tachiyomi.data.preference.getOrDefault
  15. import eu.kanade.tachiyomi.source.SourceManager
  16. import eu.kanade.tachiyomi.widget.AutofitRecyclerView
  17. import kotlinx.android.synthetic.main.source_grid_item.view.card
  18. import kotlinx.android.synthetic.main.source_grid_item.view.gradient
  19. import uy.kohesive.injekt.Injekt
  20. import uy.kohesive.injekt.api.get
  21. class LibraryItem(val manga: LibraryManga, private val libraryAsList: Preference<Boolean>) :
  22. AbstractFlexibleItem<LibraryHolder>(), IFilterable<String> {
  23. private val sourceManager: SourceManager = Injekt.get()
  24. var downloadCount = -1
  25. override fun getLayoutRes(): Int {
  26. return if (libraryAsList.getOrDefault())
  27. R.layout.source_list_item
  28. else
  29. R.layout.source_grid_item
  30. }
  31. override fun createViewHolder(view: View, adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>): LibraryHolder {
  32. val parent = adapter.recyclerView
  33. return if (parent is AutofitRecyclerView) {
  34. view.apply {
  35. val coverHeight = parent.itemWidth / 3 * 4
  36. card.layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, coverHeight)
  37. gradient.layoutParams = FrameLayout.LayoutParams(
  38. MATCH_PARENT, coverHeight / 2, Gravity.BOTTOM)
  39. }
  40. LibraryGridHolder(view, adapter)
  41. } else {
  42. LibraryListHolder(view, adapter)
  43. }
  44. }
  45. override fun bindViewHolder(
  46. adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>,
  47. holder: LibraryHolder,
  48. position: Int,
  49. payloads: List<Any?>?
  50. ) {
  51. holder.onSetValues(this)
  52. }
  53. /**
  54. * Filters a manga depending on a query.
  55. *
  56. * @param constraint the query to apply.
  57. * @return true if the manga should be included, false otherwise.
  58. */
  59. override fun filter(constraint: String): Boolean {
  60. return manga.title.contains(constraint, true) ||
  61. (manga.author?.contains(constraint, true) ?: false) ||
  62. (manga.artist?.contains(constraint, true) ?: false) ||
  63. sourceManager.getOrStub(manga.source).name.contains(constraint, true) ||
  64. if (constraint.contains(",")) {
  65. constraint.split(",").all { containsGenre(it.trim(), manga.getGenres()) }
  66. } else {
  67. containsGenre(constraint, manga.getGenres())
  68. }
  69. }
  70. private fun containsGenre(tag: String, genres: List<String>?): Boolean {
  71. return if (tag.startsWith("-"))
  72. genres?.find {
  73. it.trim().toLowerCase() == tag.substringAfter("-").toLowerCase()
  74. } == null
  75. else
  76. genres?.find {
  77. it.trim().toLowerCase() == tag.toLowerCase()
  78. } != null
  79. }
  80. override fun equals(other: Any?): Boolean {
  81. if (other is LibraryItem) {
  82. return manga.id == other.manga.id
  83. }
  84. return false
  85. }
  86. override fun hashCode(): Int {
  87. return manga.id!!.hashCode()
  88. }
  89. }