refactor(creator-ranking): 상위 카드 뷰를 추가한다
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.widget.creatorranking
|
||||||
|
|
||||||
|
data class CreatorRankingTopCardMargin(
|
||||||
|
val nameHorizontalDp: Int,
|
||||||
|
val nameBottomDp: Int,
|
||||||
|
val rankGroupStartDp: Int
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
fun fromRank(rank: Int): CreatorRankingTopCardMargin = when (rank) {
|
||||||
|
1 -> CreatorRankingTopCardMargin(
|
||||||
|
nameHorizontalDp = 20,
|
||||||
|
nameBottomDp = 24,
|
||||||
|
rankGroupStartDp = 10
|
||||||
|
)
|
||||||
|
in 2..7 -> CreatorRankingTopCardMargin(
|
||||||
|
nameHorizontalDp = 10,
|
||||||
|
nameBottomDp = 10,
|
||||||
|
rankGroupStartDp = 8
|
||||||
|
)
|
||||||
|
in 8..10 -> CreatorRankingTopCardMargin(
|
||||||
|
nameHorizontalDp = 10,
|
||||||
|
nameBottomDp = 10,
|
||||||
|
rankGroupStartDp = 6
|
||||||
|
)
|
||||||
|
else -> error("Unsupported top-card rank: $rank")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.widget.creatorranking
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.graphics.Outline
|
||||||
|
import android.graphics.RenderEffect
|
||||||
|
import android.graphics.Shader
|
||||||
|
import android.os.Build
|
||||||
|
import android.util.AttributeSet
|
||||||
|
import android.util.TypedValue
|
||||||
|
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 CreatorRankingTopCardView @JvmOverloads constructor(
|
||||||
|
context: Context,
|
||||||
|
attrs: AttributeSet? = null,
|
||||||
|
defStyleAttr: Int = 0
|
||||||
|
) : ConstraintLayout(context, attrs, defStyleAttr) {
|
||||||
|
|
||||||
|
private var image: ImageView? = null
|
||||||
|
private var dimGradient: View? = null
|
||||||
|
private var rankText: TextView? = null
|
||||||
|
private var rankGroup: View? = null
|
||||||
|
private var deltaGroup: View? = null
|
||||||
|
private var deltaAmountText: TextView? = null
|
||||||
|
private var deltaIcon: ImageView? = null
|
||||||
|
private var nameText: TextView? = null
|
||||||
|
private var currentItem: CreatorRankingItem? = null
|
||||||
|
private var clickListener: ((CreatorRankingItem) -> Unit)? = null
|
||||||
|
|
||||||
|
override fun onFinishInflate() {
|
||||||
|
super.onFinishInflate()
|
||||||
|
image = findViewById(R.id.iv_creator_ranking_image)
|
||||||
|
dimGradient = findViewById(R.id.v_creator_ranking_dim_gradient)
|
||||||
|
rankGroup = findViewById(R.id.ll_creator_ranking_rank_group)
|
||||||
|
rankText = findViewById(R.id.tv_creator_ranking_rank)
|
||||||
|
deltaGroup = findViewById(R.id.ll_creator_ranking_delta)
|
||||||
|
deltaAmountText = findViewById(R.id.tv_creator_ranking_delta_amount)
|
||||||
|
deltaIcon = findViewById(R.id.iv_creator_ranking_delta_icon)
|
||||||
|
nameText = findViewById(R.id.tv_creator_ranking_name)
|
||||||
|
clipToOutline = true
|
||||||
|
outlineProvider = roundOutlineProvider()
|
||||||
|
imageView().outlineProvider = roundOutlineProvider()
|
||||||
|
imageView().clipToOutline = true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
||||||
|
val width = MeasureSpec.getSize(widthMeasureSpec)
|
||||||
|
val squareHeightSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY)
|
||||||
|
super.onMeasure(widthMeasureSpec, squareHeightSpec)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun bind(item: CreatorRankingItem) {
|
||||||
|
currentItem = item
|
||||||
|
val style = CreatorRankingTextStyle.fromRank(item.rank)
|
||||||
|
applyMargins(CreatorRankingTopCardMargin.fromRank(item.rank))
|
||||||
|
requireNotNull(rankText).apply {
|
||||||
|
text = item.rank.toString()
|
||||||
|
setTextSize(TypedValue.COMPLEX_UNIT_SP, style.rankTextSizeSp.toFloat())
|
||||||
|
applyCreatorRankingRankGradient()
|
||||||
|
}
|
||||||
|
bindDelta(item, style)
|
||||||
|
requireNotNull(nameText).apply {
|
||||||
|
text = item.displayName(context.getString(R.string.creator_ranking_inaccessible_info))
|
||||||
|
setTextSize(TypedValue.COMPLEX_UNIT_SP, style.nameTextSizeSp.toFloat())
|
||||||
|
visibility = if (text.isNullOrBlank()) INVISIBLE else VISIBLE
|
||||||
|
}
|
||||||
|
dimGradient?.visibility = VISIBLE
|
||||||
|
applyAccessState(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun imageView(): ImageView = requireNotNull(image)
|
||||||
|
|
||||||
|
fun setOnCreatorClick(listener: ((CreatorRankingItem) -> Unit)?) {
|
||||||
|
clickListener = listener
|
||||||
|
currentItem?.let(::applyAccessState)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyMargins(margin: CreatorRankingTopCardMargin) {
|
||||||
|
requireNotNull(rankGroup).layoutParams =
|
||||||
|
(requireNotNull(rankGroup).layoutParams as MarginLayoutParams).apply {
|
||||||
|
marginStart = margin.rankGroupStartDp.dpToPx()
|
||||||
|
}
|
||||||
|
requireNotNull(nameText).layoutParams =
|
||||||
|
(requireNotNull(nameText).layoutParams as MarginLayoutParams).apply {
|
||||||
|
marginStart = margin.nameHorizontalDp.dpToPx()
|
||||||
|
marginEnd = margin.nameHorizontalDp.dpToPx()
|
||||||
|
bottomMargin = margin.nameBottomDp.dpToPx()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun bindDelta(
|
||||||
|
item: CreatorRankingItem,
|
||||||
|
style: CreatorRankingTextStyle
|
||||||
|
) {
|
||||||
|
requireNotNull(deltaGroup).visibility = if (item.showRankChange) VISIBLE else GONE
|
||||||
|
if (!item.showRankChange) return
|
||||||
|
|
||||||
|
val presentation = CreatorRankingDeltaPresentation.from(item.rankChangeType, item.rankChangeAmount)
|
||||||
|
applyDeltaContainer(presentation)
|
||||||
|
requireNotNull(deltaIcon).apply {
|
||||||
|
setImageResource(presentation.iconRes)
|
||||||
|
layoutParams = (layoutParams as MarginLayoutParams).apply {
|
||||||
|
width = presentation.iconWidthDp.dpToPx()
|
||||||
|
height = presentation.iconHeightDp.dpToPx()
|
||||||
|
marginStart = presentation.iconMarginStartDp.dpToPx()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
requireNotNull(deltaAmountText).apply {
|
||||||
|
text = presentation.amountText.orEmpty()
|
||||||
|
setTextSize(TypedValue.COMPLEX_UNIT_SP, style.deltaTextSizeSp.toFloat())
|
||||||
|
visibility = if (presentation.showAmount) VISIBLE else GONE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyDeltaContainer(presentation: CreatorRankingDeltaPresentation) {
|
||||||
|
requireNotNull(deltaGroup).apply {
|
||||||
|
setBackgroundResource(if (presentation.showPillBackground) R.drawable.bg_creator_ranking_delta else 0)
|
||||||
|
val horizontalPadding = if (presentation.showPillBackground) 4.dpToPx() else 0
|
||||||
|
setPadding(horizontalPadding, paddingTop, horizontalPadding, paddingBottom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyAccessState(item: CreatorRankingItem) {
|
||||||
|
applyBlur(item.isInaccessible)
|
||||||
|
isClickable = item.isTouchable && clickListener != null
|
||||||
|
setOnClickListener(if (isClickable) OnClickListener { clickListener?.invoke(item) } else null)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyBlur(enabled: Boolean) {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
imageView().setRenderEffect(
|
||||||
|
if (enabled) RenderEffect.createBlurEffect(16f, 16f, Shader.TileMode.CLAMP) else null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun roundOutlineProvider() = object : ViewOutlineProvider() {
|
||||||
|
override fun getOutline(view: View, outline: Outline) {
|
||||||
|
outline.setRoundRect(0, 0, view.width, view.height, 14.dpToPx().toFloat())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Int.dpToPx(): Int = (this * resources.displayMetrics.density).roundToInt()
|
||||||
|
}
|
||||||
96
app/src/main/res/layout/view_creator_ranking_top_card.xml
Normal file
96
app/src/main/res/layout/view_creator_ranking_top_card.xml
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<kr.co.vividnext.sodalive.v2.widget.creatorranking.CreatorRankingTopCardView 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="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/iv_creator_ranking_image"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:background="@drawable/bg_creator_ranking_image"
|
||||||
|
android:contentDescription="@null"
|
||||||
|
android:scaleType="centerCrop"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintDimensionRatio="1:1"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:id="@+id/v_creator_ranking_dim_gradient"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:background="@drawable/bg_creator_ranking_dim_gradient"
|
||||||
|
app:layout_constraintBottom_toBottomOf="@id/iv_creator_ranking_image"
|
||||||
|
app:layout_constraintEnd_toEndOf="@id/iv_creator_ranking_image"
|
||||||
|
app:layout_constraintStart_toStartOf="@id/iv_creator_ranking_image"
|
||||||
|
app:layout_constraintTop_toTopOf="@id/iv_creator_ranking_image" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/ll_creator_ranking_rank_group"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center"
|
||||||
|
android:orientation="vertical"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_creator_ranking_rank"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:fontFamily="@font/phosphate_solid"
|
||||||
|
android:gravity="center"
|
||||||
|
android:padding="0dp"
|
||||||
|
android:shadowColor="#7A000000"
|
||||||
|
android:shadowRadius="4"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
tools:text="1" />
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/ll_creator_ranking_delta"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:background="@drawable/bg_creator_ranking_delta"
|
||||||
|
android:gravity="center"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:paddingHorizontal="4dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_creator_ranking_delta_amount"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:fontFamily="@font/medium"
|
||||||
|
android:includeFontPadding="false"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
tools:text="4" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/iv_creator_ranking_delta_icon"
|
||||||
|
android:layout_width="14dp"
|
||||||
|
android:layout_height="14dp"
|
||||||
|
android:layout_marginStart="2dp"
|
||||||
|
android:contentDescription="@null"
|
||||||
|
tools:src="@drawable/ic_rank_caret_increase" />
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_creator_ranking_name"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginHorizontal="12dp"
|
||||||
|
android:layout_marginBottom="20dp"
|
||||||
|
android:ellipsize="end"
|
||||||
|
android:fontFamily="@font/bold"
|
||||||
|
android:gravity="center"
|
||||||
|
android:includeFontPadding="false"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:textColor="@color/white"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
tools:text="크리에이터 이름" />
|
||||||
|
</kr.co.vividnext.sodalive.v2.widget.creatorranking.CreatorRankingTopCardView>
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.widget.creatorranking
|
||||||
|
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class CreatorRankingTopCardMarginTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `rank 1은 큰 닉네임 margin과 rank group start margin을 사용한다`() {
|
||||||
|
val margin = CreatorRankingTopCardMargin.fromRank(1)
|
||||||
|
|
||||||
|
assertEquals(20, margin.nameHorizontalDp)
|
||||||
|
assertEquals(24, margin.nameBottomDp)
|
||||||
|
assertEquals(10, margin.rankGroupStartDp)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `rank 2부터 7은 중간 rank group start margin을 사용한다`() {
|
||||||
|
(2..7).forEach { rank ->
|
||||||
|
val margin = CreatorRankingTopCardMargin.fromRank(rank)
|
||||||
|
|
||||||
|
assertEquals(10, margin.nameHorizontalDp)
|
||||||
|
assertEquals(10, margin.nameBottomDp)
|
||||||
|
assertEquals(8, margin.rankGroupStartDp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `rank 8부터 10은 작은 rank group start margin을 사용한다`() {
|
||||||
|
(8..10).forEach { rank ->
|
||||||
|
val margin = CreatorRankingTopCardMargin.fromRank(rank)
|
||||||
|
|
||||||
|
assertEquals(10, margin.nameHorizontalDp)
|
||||||
|
assertEquals(10, margin.nameBottomDp)
|
||||||
|
assertEquals(6, margin.rankGroupStartDp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalStateException::class)
|
||||||
|
fun `top-card 범위 밖 rank는 지원하지 않는다`() {
|
||||||
|
CreatorRankingTopCardMargin.fromRank(11)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user