Convert GalleryHolder to view binding
This commit is contained in:
1 parent
562a6d6ab8
commit
e9839335c6
2 files changed
+95
-115
No files matched your search
@@ -17,23 +17,20 @@ package com.hippo.ehviewer.ui.scene
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.res.Resources
|
||||
import android.text.TextUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.IntDef
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.StaggeredGridLayoutManager
|
||||
import com.hippo.drawable.TriangleDrawable
|
||||
import com.hippo.easyrecyclerview.MarginItemDecoration
|
||||
import com.hippo.ehviewer.download.DownloadManager as downloadManager
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.dao.LocalFavoriteInfo
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.widget.TileThumb
|
||||
import com.hippo.ehviewer.databinding.ItemGalleryGridBinding
|
||||
import com.hippo.ehviewer.databinding.ItemGalleryListBinding
|
||||
import com.hippo.widget.recyclerview.AutoStaggeredGridLayoutManager
|
||||
import com.hippo.yorozuya.ViewUtils
|
||||
|
||||
@@ -48,7 +45,6 @@ internal abstract class GalleryAdapter(
|
||||
private val mListThumbWidth: Int
|
||||
private val mListThumbHeight: Int
|
||||
private val mShowFavourited: Boolean
|
||||
private val mDownloadManager: DownloadManager
|
||||
private var mListDecoration: MarginItemDecoration? = null
|
||||
private var mGirdDecoration: MarginItemDecoration? = null
|
||||
private var mType = TYPE_INVALID
|
||||
@@ -63,8 +59,7 @@ internal abstract class GalleryAdapter(
|
||||
val recyclerView = mRecyclerView
|
||||
when (type) {
|
||||
TYPE_LIST -> {
|
||||
val columnWidth =
|
||||
mResources.getDimensionPixelOffset(Settings.detailSizeResId)
|
||||
val columnWidth = mResources.getDimensionPixelOffset(Settings.detailSizeResId)
|
||||
mLayoutManager.setColumnSize(columnWidth)
|
||||
mLayoutManager.setStrategy(AutoStaggeredGridLayoutManager.STRATEGY_MIN_SIZE)
|
||||
if (null != mGirdDecoration) {
|
||||
@@ -119,7 +114,6 @@ internal abstract class GalleryAdapter(
|
||||
mListThumbHeight = calculator.measuredHeight
|
||||
mListThumbWidth = mListThumbHeight * 2 / 3
|
||||
this.type = type
|
||||
mDownloadManager = downloadManager
|
||||
}
|
||||
|
||||
private fun adjustPaddings() {
|
||||
@@ -131,29 +125,22 @@ internal abstract class GalleryAdapter(
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryHolder {
|
||||
val layoutId = when (viewType) {
|
||||
TYPE_LIST -> R.layout.item_gallery_list
|
||||
TYPE_GRID -> R.layout.item_gallery_grid
|
||||
val holder = when (viewType) {
|
||||
TYPE_LIST -> ListGalleryHolder(
|
||||
ItemGalleryListBinding.inflate(mInflater, parent, false),
|
||||
mShowFavourited,
|
||||
mListThumbWidth,
|
||||
mListThumbHeight
|
||||
)
|
||||
|
||||
TYPE_GRID -> GridGalleryHolder(ItemGalleryGridBinding.inflate(mInflater, parent, false))
|
||||
else -> throw IllegalStateException("Unexpected value: $viewType")
|
||||
}
|
||||
val holder = GalleryHolder(mInflater.inflate(layoutId, parent, false))
|
||||
if (viewType == TYPE_LIST) {
|
||||
val lp = holder.thumb.layoutParams
|
||||
lp.width = mListThumbWidth
|
||||
lp.height = mListThumbHeight
|
||||
holder.thumb.layoutParams = lp
|
||||
holder.itemView.setOnClickListener {
|
||||
onItemClick(holder.itemView, holder.bindingAdapterPosition)
|
||||
}
|
||||
holder.card.setOnClickListener {
|
||||
onItemClick(
|
||||
holder.itemView,
|
||||
holder.bindingAdapterPosition
|
||||
)
|
||||
}
|
||||
holder.card.setOnLongClickListener {
|
||||
onItemLongClick(
|
||||
holder.itemView,
|
||||
holder.bindingAdapterPosition
|
||||
)
|
||||
holder.itemView.setOnLongClickListener {
|
||||
onItemLongClick(holder.itemView, holder.bindingAdapterPosition)
|
||||
}
|
||||
return holder
|
||||
}
|
||||
@@ -166,61 +153,12 @@ internal abstract class GalleryAdapter(
|
||||
|
||||
abstract fun getDataAt(position: Int): GalleryInfo?
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun onBindViewHolder(holder: GalleryHolder, position: Int) {
|
||||
val gi = getDataAt(position) ?: return
|
||||
if (gi is LocalFavoriteInfo) {
|
||||
gi.thumb = EhUtils.fixThumbUrl(gi.thumb!!)
|
||||
}
|
||||
when (mType) {
|
||||
TYPE_LIST -> {
|
||||
holder.thumb.load(gi.thumb!!)
|
||||
holder.title!!.text = EhUtils.getSuitableTitle(gi)
|
||||
holder.uploader!!.text = gi.uploader
|
||||
holder.uploader.alpha = if (gi.disowned) .5f else 1f
|
||||
holder.rating!!.rating = gi.rating
|
||||
val category = holder.category
|
||||
val newCategoryText = EhUtils.getCategory(gi.category)
|
||||
if (newCategoryText != category.text.toString()) {
|
||||
category.text = newCategoryText
|
||||
category.setBackgroundColor(EhUtils.getCategoryColor(gi.category))
|
||||
}
|
||||
holder.posted!!.text = gi.posted
|
||||
if (gi.pages == 0 || !Settings.showGalleryPages) {
|
||||
holder.pages?.text = null
|
||||
holder.pages!!.visibility = View.GONE
|
||||
} else {
|
||||
holder.pages!!.text = gi.pages.toString() + "P"
|
||||
holder.pages.visibility = View.VISIBLE
|
||||
}
|
||||
if (TextUtils.isEmpty(gi.simpleLanguage)) {
|
||||
holder.simpleLanguage.text = null
|
||||
holder.simpleLanguage.visibility = View.GONE
|
||||
} else {
|
||||
holder.simpleLanguage.text = gi.simpleLanguage
|
||||
holder.simpleLanguage.visibility = View.VISIBLE
|
||||
}
|
||||
holder.favourited!!.visibility =
|
||||
if (mShowFavourited && gi.favoriteSlot >= -1 && gi.favoriteSlot <= 10) View.VISIBLE else View.GONE
|
||||
holder.downloaded!!.visibility =
|
||||
if (mDownloadManager.containDownloadInfo(gi.gid)) View.VISIBLE else View.GONE
|
||||
}
|
||||
|
||||
TYPE_GRID -> {
|
||||
(holder.thumb as TileThumb).setThumbSize(gi.thumbWidth, gi.thumbHeight)
|
||||
holder.thumb.load(gi.thumb!!)
|
||||
val category: View = holder.category
|
||||
var drawable = category.background
|
||||
val color = EhUtils.getCategoryColor(gi.category)
|
||||
if (drawable !is TriangleDrawable) {
|
||||
drawable = TriangleDrawable(color)
|
||||
category.background = drawable
|
||||
} else {
|
||||
drawable.setColor(color)
|
||||
}
|
||||
holder.simpleLanguage.text = gi.simpleLanguage
|
||||
}
|
||||
}
|
||||
holder.bind(gi)
|
||||
}
|
||||
|
||||
@IntDef(TYPE_LIST, TYPE_GRID)
|
||||
|
||||
@@ -15,46 +15,88 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.ui.scene
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.text.TextUtils
|
||||
import android.view.View
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.card.MaterialCardView
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.widget.SimpleRatingView
|
||||
import com.hippo.widget.LoadImageView
|
||||
import androidx.viewbinding.ViewBinding
|
||||
import com.hippo.drawable.TriangleDrawable
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.databinding.ItemGalleryGridBinding
|
||||
import com.hippo.ehviewer.databinding.ItemGalleryListBinding
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
|
||||
internal class GalleryHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
@JvmField
|
||||
val thumb: LoadImageView = itemView.findViewById(R.id.thumb)
|
||||
internal abstract class GalleryHolder(binding: ViewBinding) :
|
||||
RecyclerView.ViewHolder(binding.root) {
|
||||
abstract fun bind(galleryInfo: GalleryInfo)
|
||||
}
|
||||
|
||||
@JvmField
|
||||
val title: TextView? = itemView.findViewById(R.id.title)
|
||||
internal class ListGalleryHolder(
|
||||
private val binding: ItemGalleryListBinding,
|
||||
private val showFavourited: Boolean,
|
||||
thumbWidth: Int,
|
||||
thumbHeight: Int
|
||||
) : GalleryHolder(binding) {
|
||||
init {
|
||||
val lp = binding.thumb.layoutParams
|
||||
lp.width = thumbWidth
|
||||
lp.height = thumbHeight
|
||||
binding.thumb.layoutParams = lp
|
||||
}
|
||||
|
||||
@JvmField
|
||||
val uploader: TextView? = itemView.findViewById(R.id.uploader)
|
||||
@SuppressLint("SetTextI18n")
|
||||
override fun bind(galleryInfo: GalleryInfo) {
|
||||
binding.run {
|
||||
thumb.load(galleryInfo.thumb!!)
|
||||
title.text = EhUtils.getSuitableTitle(galleryInfo)
|
||||
uploader.text = galleryInfo.uploader
|
||||
uploader.alpha = if (galleryInfo.disowned) .5f else 1f
|
||||
rating.rating = galleryInfo.rating
|
||||
val newCategoryText = EhUtils.getCategory(galleryInfo.category)
|
||||
if (newCategoryText != category.text.toString()) {
|
||||
category.text = newCategoryText
|
||||
category.setBackgroundColor(EhUtils.getCategoryColor(galleryInfo.category))
|
||||
}
|
||||
posted.text = galleryInfo.posted
|
||||
if (galleryInfo.pages == 0 || !Settings.showGalleryPages) {
|
||||
pages.text = null
|
||||
pages.visibility = View.GONE
|
||||
} else {
|
||||
pages.text = "${galleryInfo.pages}P"
|
||||
pages.visibility = View.VISIBLE
|
||||
}
|
||||
if (TextUtils.isEmpty(galleryInfo.simpleLanguage)) {
|
||||
simpleLanguage.text = null
|
||||
simpleLanguage.visibility = View.GONE
|
||||
} else {
|
||||
simpleLanguage.text = galleryInfo.simpleLanguage
|
||||
simpleLanguage.visibility = View.VISIBLE
|
||||
}
|
||||
favourited.visibility =
|
||||
if (showFavourited && galleryInfo.favoriteSlot >= -1 && galleryInfo.favoriteSlot <= 10) View.VISIBLE else View.GONE
|
||||
downloaded.visibility =
|
||||
if (DownloadManager.containDownloadInfo(galleryInfo.gid)) View.VISIBLE else View.GONE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmField
|
||||
val rating: SimpleRatingView? = itemView.findViewById(R.id.rating)
|
||||
|
||||
@JvmField
|
||||
val category: TextView = itemView.findViewById(R.id.category)
|
||||
|
||||
@JvmField
|
||||
val posted: TextView? = itemView.findViewById(R.id.posted)
|
||||
|
||||
@JvmField
|
||||
val pages: TextView? = itemView.findViewById(R.id.pages)
|
||||
|
||||
@JvmField
|
||||
val simpleLanguage: TextView = itemView.findViewById(R.id.simple_language)
|
||||
|
||||
@JvmField
|
||||
val favourited: ImageView? = itemView.findViewById(R.id.favourited)
|
||||
|
||||
@JvmField
|
||||
val downloaded: ImageView? = itemView.findViewById(R.id.downloaded)
|
||||
|
||||
@JvmField
|
||||
val card: MaterialCardView = itemView.findViewById(R.id.card)
|
||||
internal class GridGalleryHolder(private val binding: ItemGalleryGridBinding) :
|
||||
GalleryHolder(binding) {
|
||||
override fun bind(galleryInfo: GalleryInfo) {
|
||||
binding.run {
|
||||
thumb.setThumbSize(galleryInfo.thumbWidth, galleryInfo.thumbHeight)
|
||||
thumb.load(galleryInfo.thumb!!)
|
||||
var drawable = category.background
|
||||
val color = EhUtils.getCategoryColor(galleryInfo.category)
|
||||
if (drawable !is TriangleDrawable) {
|
||||
drawable = TriangleDrawable(color)
|
||||
category.background = drawable
|
||||
} else {
|
||||
drawable.setColor(color)
|
||||
}
|
||||
simpleLanguage.text = galleryInfo.simpleLanguage
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user