feat(feed): 랭킹 피드 뷰를 추가한다
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package kr.co.vividnext.sodalive.v2.widget.feed
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Outline
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewOutlineProvider
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class FeedRankView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : LinearLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
private var image: ImageView? = null
|
||||
private var rankText: TextView? = null
|
||||
private var messageText: TextView? = null
|
||||
private var currentItem: FeedItem.Rank? = null
|
||||
private var clickListener: ((FeedItem) -> Unit)? = null
|
||||
|
||||
override fun onFinishInflate() {
|
||||
super.onFinishInflate()
|
||||
image = findViewById(R.id.iv_feed_rank_image)
|
||||
rankText = findViewById(R.id.tv_feed_rank_overlay)
|
||||
messageText = findViewById(R.id.tv_feed_rank_message)
|
||||
imageView().clipToOutline = true
|
||||
imageView().outlineProvider = roundedOutlineProvider()
|
||||
}
|
||||
|
||||
fun bind(item: FeedItem.Rank) {
|
||||
currentItem = item
|
||||
requireNotNull(rankText).text = item.rankText
|
||||
requireNotNull(messageText).text = FeedRankTextStyler.style(
|
||||
text = item.message,
|
||||
highlightRanges = item.highlightRanges,
|
||||
highlightColor = ContextCompat.getColor(context, R.color.soda_400)
|
||||
)
|
||||
applyClickState(item)
|
||||
}
|
||||
|
||||
fun imageView(): ImageView = requireNotNull(image)
|
||||
|
||||
fun setFeedSize(size: FeedSize) {
|
||||
updateRootWidth(size.rootWidthDp.dpToPx())
|
||||
}
|
||||
|
||||
fun setOnFeedClick(listener: ((FeedItem) -> Unit)?) {
|
||||
clickListener = listener
|
||||
currentItem?.let(::applyClickState)
|
||||
}
|
||||
|
||||
private fun applyClickState(item: FeedItem.Rank) {
|
||||
val listener = clickListener
|
||||
setOnClickListener(if (listener == null) null else View.OnClickListener { listener(item) })
|
||||
isClickable = listener != null
|
||||
}
|
||||
|
||||
private fun updateRootWidth(width: Int) {
|
||||
val currentLayoutParams = layoutParams
|
||||
layoutParams = if (currentLayoutParams == null) {
|
||||
ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||
} else {
|
||||
currentLayoutParams.apply { this.width = width }
|
||||
}
|
||||
}
|
||||
|
||||
private fun roundedOutlineProvider() = object : ViewOutlineProvider() {
|
||||
override fun getOutline(view: View, outline: Outline) {
|
||||
outline.setRoundRect(0, 0, view.width, view.height, resources.getDimension(R.dimen.radius_14))
|
||||
}
|
||||
}
|
||||
|
||||
private fun Int.dpToPx(): Int = (this * resources.displayMetrics.density).roundToInt()
|
||||
}
|
||||
Reference in New Issue
Block a user