feat(feed): 콘텐츠 피드 뷰를 추가한다
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
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.TextView
|
||||
import androidx.constraintlayout.widget.ConstraintLayout
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
class FeedContentView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
defStyleAttr: Int = 0
|
||||
) : ConstraintLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
private var contentImage: ImageView? = null
|
||||
private var profileImage: ImageView? = null
|
||||
private var creatorText: TextView? = null
|
||||
private var titleText: TextView? = null
|
||||
private var categoryText: TextView? = null
|
||||
private var createdAtText: TextView? = null
|
||||
private var currentItem: FeedItem.Content? = null
|
||||
private var clickListener: ((FeedItem) -> Unit)? = null
|
||||
|
||||
override fun onFinishInflate() {
|
||||
super.onFinishInflate()
|
||||
contentImage = findViewById(R.id.iv_feed_content_image)
|
||||
profileImage = findViewById(R.id.iv_feed_content_profile)
|
||||
creatorText = findViewById(R.id.tv_feed_content_creator)
|
||||
titleText = findViewById(R.id.tv_feed_content_title)
|
||||
categoryText = findViewById(R.id.tv_feed_content_category)
|
||||
createdAtText = findViewById(R.id.tv_feed_content_created_at)
|
||||
contentImageView().clipToOutline = true
|
||||
contentImageView().outlineProvider = roundedOutlineProvider()
|
||||
profileImageView().clipToOutline = true
|
||||
profileImageView().outlineProvider = circleOutlineProvider()
|
||||
}
|
||||
|
||||
fun bind(item: FeedItem.Content) {
|
||||
currentItem = item
|
||||
requireNotNull(creatorText).text = item.creatorName
|
||||
requireNotNull(titleText).text = item.contentTitle
|
||||
requireNotNull(categoryText).text = context.getString(item.category.labelResId)
|
||||
requireNotNull(createdAtText).text = item.createdAtText
|
||||
applyContentImageSize(item.category)
|
||||
applyClickState(item)
|
||||
}
|
||||
|
||||
fun contentImageView(): ImageView = requireNotNull(contentImage)
|
||||
|
||||
fun profileImageView(): ImageView = requireNotNull(profileImage)
|
||||
|
||||
fun setFeedSize(size: FeedSize) {
|
||||
updateRootWidth(size.rootWidthDp.dpToPx())
|
||||
}
|
||||
|
||||
fun setOnFeedClick(listener: ((FeedItem) -> Unit)?) {
|
||||
clickListener = listener
|
||||
currentItem?.let(::applyClickState)
|
||||
}
|
||||
|
||||
private fun applyContentImageSize(category: FeedContentCategory) {
|
||||
val image = contentImageView()
|
||||
val width = image.layoutParams.width.takeIf { it > 0 } ?: CONTENT_IMAGE_WIDTH_DP.dpToPx()
|
||||
val widthDp = (width / resources.displayMetrics.density).roundToInt()
|
||||
val size = FeedContentImageSize.from(widthDp, category)
|
||||
image.layoutParams = image.layoutParams.apply {
|
||||
this.width = size.widthDp.dpToPx()
|
||||
this.height = size.heightDp.dpToPx()
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyClickState(item: FeedItem.Content) {
|
||||
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 circleOutlineProvider() = object : ViewOutlineProvider() {
|
||||
override fun getOutline(view: View, outline: Outline) {
|
||||
outline.setOval(0, 0, view.width, view.height)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Int.dpToPx(): Int = (this * resources.displayMetrics.density).roundToInt()
|
||||
|
||||
private companion object {
|
||||
const val CONTENT_IMAGE_WIDTH_DP = 163
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user