diff --git a/app/src/main/java/kr/co/vividnext/sodalive/v2/widget/feed/FeedCommunityView.kt b/app/src/main/java/kr/co/vividnext/sodalive/v2/widget/feed/FeedCommunityView.kt new file mode 100644 index 00000000..73bea907 --- /dev/null +++ b/app/src/main/java/kr/co/vividnext/sodalive/v2/widget/feed/FeedCommunityView.kt @@ -0,0 +1,99 @@ +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 kr.co.vividnext.sodalive.R +import kotlin.math.roundToInt + +class FeedCommunityView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : LinearLayout(context, attrs, defStyleAttr) { + + private var profileImage: ImageView? = null + private var creatorText: TextView? = null + private var createdAtText: TextView? = null + private var bodyText: TextView? = null + private var keywordText: TextView? = null + private var commentCountText: TextView? = null + private var likeCountText: TextView? = null + private var currentItem: FeedItem.Community? = null + private var clickListener: ((FeedItem) -> Unit)? = null + private var hideEmptyTextRows: Boolean = false + + override fun onFinishInflate() { + super.onFinishInflate() + profileImage = findViewById(R.id.iv_feed_community_profile) + creatorText = findViewById(R.id.tv_feed_community_creator) + createdAtText = findViewById(R.id.tv_feed_community_created_at) + bodyText = findViewById(R.id.tv_feed_community_body) + keywordText = findViewById(R.id.tv_feed_community_keyword) + commentCountText = findViewById(R.id.tv_feed_community_comment_count) + likeCountText = findViewById(R.id.tv_feed_community_like_count) + profileImageView().clipToOutline = true + profileImageView().outlineProvider = circleOutlineProvider() + } + + fun bind(item: FeedItem.Community) { + currentItem = item + requireNotNull(creatorText).text = item.creatorName + requireNotNull(createdAtText).text = item.createdAtText + requireNotNull(bodyText).text = item.bodyText + requireNotNull(keywordText).text = item.keywordText + requireNotNull(bodyText).visibility = visibilityForText(item.bodyText) + requireNotNull(keywordText).visibility = visibilityForText(item.keywordText) + requireNotNull(commentCountText).text = item.commentCount.toString() + requireNotNull(likeCountText).text = item.likeCount.toString() + applyClickState(item) + } + + fun profileImageView(): ImageView = requireNotNull(profileImage) + + fun setFeedSize(size: FeedSize) { + updateRootWidth(size.rootWidthDp.dpToPx()) + } + + fun setOnFeedClick(listener: ((FeedItem) -> Unit)?) { + clickListener = listener + currentItem?.let(::applyClickState) + } + + fun setHideEmptyTextRows(hide: Boolean) { + hideEmptyTextRows = hide + currentItem?.let(::bind) + } + + private fun visibilityForText(text: String): Int = + if (hideEmptyTextRows && text.isEmpty()) View.GONE else View.VISIBLE + + private fun applyClickState(item: FeedItem.Community) { + 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 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() +} diff --git a/app/src/main/res/layout/view_feed_community.xml b/app/src/main/res/layout/view_feed_community.xml new file mode 100644 index 00000000..8b9407dd --- /dev/null +++ b/app/src/main/res/layout/view_feed_community.xml @@ -0,0 +1,118 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +