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
|
||||||
|
}
|
||||||
|
}
|
||||||
114
app/src/main/res/layout/view_feed_content.xml
Normal file
114
app/src/main/res/layout/view_feed_content.xml
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<kr.co.vividnext.sodalive.v2.widget.feed.FeedContentView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="374dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@drawable/bg_feed_card"
|
||||||
|
android:clipToOutline="true"
|
||||||
|
android:padding="@dimen/spacing_14">
|
||||||
|
|
||||||
|
<FrameLayout
|
||||||
|
android:id="@+id/fl_feed_content_image_container"
|
||||||
|
android:layout_width="163dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/iv_feed_content_image"
|
||||||
|
android:layout_width="163dp"
|
||||||
|
android:layout_height="163dp"
|
||||||
|
android:contentDescription="@string/a11y_feed_content_image"
|
||||||
|
android:scaleType="centerCrop"
|
||||||
|
tools:src="@drawable/ic_launcher_background" />
|
||||||
|
|
||||||
|
</FrameLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/ll_feed_content_info"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginStart="@dimen/spacing_14"
|
||||||
|
android:orientation="vertical"
|
||||||
|
app:layout_constraintBottom_toBottomOf="@id/fl_feed_content_image_container"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toEndOf="@id/fl_feed_content_image_container"
|
||||||
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="20dp"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/iv_feed_content_profile"
|
||||||
|
android:layout_width="20dp"
|
||||||
|
android:layout_height="20dp"
|
||||||
|
android:contentDescription="@string/a11y_feed_profile_image"
|
||||||
|
android:scaleType="centerCrop"
|
||||||
|
tools:src="@drawable/ic_launcher_background" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_feed_content_creator"
|
||||||
|
style="@style/Typography.Caption3"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="@dimen/spacing_4"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:includeFontPadding="false"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
tools:text="크리에이터이름" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_feed_content_title"
|
||||||
|
style="@style/Typography.Heading4"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginTop="@dimen/spacing_6"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:includeFontPadding="false"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
tools:text="콘텐츠 이름" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/ll_feed_content_meta"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center_vertical"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_feed_content_category"
|
||||||
|
style="@style/Typography.Body6"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@drawable/bg_feed_category_tag"
|
||||||
|
android:includeFontPadding="false"
|
||||||
|
android:paddingHorizontal="@dimen/spacing_4"
|
||||||
|
android:paddingVertical="2dp"
|
||||||
|
android:textColor="@color/gray_100"
|
||||||
|
tools:text="콘텐츠" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_feed_content_created_at"
|
||||||
|
style="@style/Typography.Body6"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="@dimen/spacing_8"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:gravity="end"
|
||||||
|
android:includeFontPadding="false"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:textColor="@color/gray_500"
|
||||||
|
tools:text="2분 전" />
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
||||||
|
</kr.co.vividnext.sodalive.v2.widget.feed.FeedContentView>
|
||||||
Reference in New Issue
Block a user