RecentChaptersHolder.kt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package eu.kanade.tachiyomi.ui.recent
  2. import android.support.v4.content.ContextCompat
  3. import android.view.View
  4. import android.widget.PopupMenu
  5. import eu.kanade.tachiyomi.R
  6. import eu.kanade.tachiyomi.data.database.models.Chapter
  7. import eu.kanade.tachiyomi.data.database.models.MangaChapter
  8. import eu.kanade.tachiyomi.data.download.model.Download
  9. import eu.kanade.tachiyomi.ui.base.adapter.FlexibleViewHolder
  10. import kotlinx.android.synthetic.main.item_recent_chapter.view.*
  11. import rx.Observable
  12. /**
  13. * Holder that contains chapter item
  14. * Uses R.layout.item_recent_chapter.
  15. * UI related actions should be called from here.
  16. *
  17. * @param view the inflated view for this holder.
  18. * @param adapter the adapter handling this holder.
  19. * @param listener a listener to react to single tap and long tap events.
  20. * @constructor creates a new library holder.
  21. */
  22. class RecentChaptersHolder(view: View, private val adapter: RecentChaptersAdapter, listener: FlexibleViewHolder.OnListItemClickListener) : FlexibleViewHolder(view, adapter, listener) {
  23. /**
  24. * Color of read chapter
  25. */
  26. private val readColor: Int
  27. /**
  28. * Color of unread chapter
  29. */
  30. private val unreadColor: Int
  31. /**
  32. * Object containing chapter information
  33. */
  34. private var mangaChapter: MangaChapter? = null
  35. init {
  36. // Set colors.
  37. readColor = ContextCompat.getColor(view.context, R.color.hint_text)
  38. unreadColor = ContextCompat.getColor(view.context, R.color.primary_text)
  39. //Set OnClickListener for download menu
  40. itemView.chapterMenu.setOnClickListener { v -> v.post({ showPopupMenu(v) }) }
  41. }
  42. /**
  43. * Set values of view
  44. *
  45. * @param item item containing chapter information
  46. */
  47. fun onSetValues(item: MangaChapter) {
  48. this.mangaChapter = item
  49. // Set chapter title
  50. itemView.chapter_title.text = item.chapter.name
  51. // Set manga title
  52. itemView.manga_title.text = item.manga.title
  53. // Check if chapter is read and set correct color
  54. if (item.chapter.read) {
  55. itemView.chapter_title.setTextColor(readColor)
  56. itemView.manga_title.setTextColor(readColor)
  57. } else {
  58. itemView.chapter_title.setTextColor(unreadColor)
  59. itemView.manga_title.setTextColor(unreadColor)
  60. }
  61. // Set chapter status
  62. onStatusChange(item.chapter.status)
  63. }
  64. /**
  65. * Updates chapter status in view.
  66. * @param status download status
  67. */
  68. fun onStatusChange(status: Int) {
  69. when (status) {
  70. Download.QUEUE -> itemView.download_text.setText(R.string.chapter_queued)
  71. Download.DOWNLOADING -> itemView.download_text.setText(R.string.chapter_downloading)
  72. Download.DOWNLOADED -> itemView.download_text.setText(R.string.chapter_downloaded)
  73. Download.ERROR -> itemView.download_text.setText(R.string.chapter_error)
  74. else -> itemView.download_text.text = ""
  75. }
  76. }
  77. /**
  78. * Show pop up menu
  79. * @param view view containing popup menu.
  80. */
  81. private fun showPopupMenu(view: View) {
  82. // Create a PopupMenu, giving it the clicked view for an anchor
  83. val popup = PopupMenu(adapter.fragment.activity, view)
  84. // Inflate our menu resource into the PopupMenu's Menu
  85. popup.menuInflater.inflate(R.menu.chapter_recent, popup.menu)
  86. mangaChapter?.let {
  87. // Hide download and show delete if the chapter is downloaded and
  88. if (it.chapter.isDownloaded) {
  89. val menu = popup.menu
  90. menu.findItem(R.id.action_download).isVisible = false
  91. menu.findItem(R.id.action_delete).isVisible = true
  92. }
  93. // Hide mark as unread when the chapter is unread
  94. if (!it.chapter.read /*&& mangaChapter.chapter.last_page_read == 0*/) {
  95. popup.menu.findItem(R.id.action_mark_as_unread).isVisible = false
  96. }
  97. // Hide mark as read when the chapter is read
  98. if (it.chapter.read) {
  99. popup.menu.findItem(R.id.action_mark_as_read).isVisible = false
  100. }
  101. // Set a listener so we are notified if a menu item is clicked
  102. popup.setOnMenuItemClickListener { menuItem ->
  103. val chapterObservable = Observable.just<Chapter>(it.chapter)
  104. when (menuItem.itemId) {
  105. R.id.action_download -> adapter.fragment.onDownload(chapterObservable, it.manga)
  106. R.id.action_delete -> adapter.fragment.onDelete(chapterObservable, it.manga)
  107. R.id.action_mark_as_read -> adapter.fragment.onMarkAsRead(chapterObservable);
  108. R.id.action_mark_as_unread -> adapter.fragment.onMarkAsUnread(chapterObservable);
  109. }
  110. false
  111. }
  112. }
  113. // Finally show the PopupMenu
  114. popup.show()
  115. }
  116. }