feat(widget): 오디오 콘텐츠 태그 배지를 추가한다
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
package kr.co.vividnext.sodalive.v2.widget
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Outline
|
||||
import android.util.AttributeSet
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewOutlineProvider
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.content.res.ResourcesCompat
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
@@ -15,7 +22,10 @@ class AudioContentCardView @JvmOverloads constructor(
|
||||
defStyleAttr: Int = 0
|
||||
) : LinearLayout(context, attrs, defStyleAttr) {
|
||||
|
||||
private var thumbnailContainer: FrameLayout? = null
|
||||
private var thumbnail: ImageView? = null
|
||||
private var topTagContainer: LinearLayout? = null
|
||||
private var bottomTagContainer: LinearLayout? = null
|
||||
private var labelContainer: LinearLayout? = null
|
||||
private var titleText: TextView? = null
|
||||
private var creatorText: TextView? = null
|
||||
@@ -26,21 +36,31 @@ class AudioContentCardView @JvmOverloads constructor(
|
||||
|
||||
override fun onFinishInflate() {
|
||||
super.onFinishInflate()
|
||||
thumbnailContainer = findViewById(R.id.fl_audio_content_thumbnail_container)
|
||||
thumbnail = findViewById(R.id.iv_audio_content_thumbnail)
|
||||
topTagContainer = findViewById(R.id.ll_audio_content_tag_top)
|
||||
bottomTagContainer = findViewById(R.id.ll_audio_content_tag_bottom)
|
||||
labelContainer = findViewById(R.id.ll_audio_content_label)
|
||||
titleText = findViewById(R.id.tv_audio_content_title)
|
||||
creatorText = findViewById(R.id.tv_audio_content_creator)
|
||||
if (isInEditMode && !hasRequiredViews()) return
|
||||
setThumbnailOutline()
|
||||
setSize(AudioContentCardSize.Medium)
|
||||
}
|
||||
|
||||
fun setSize(size: AudioContentCardSize) {
|
||||
updateRootWidth(size.cardWidthDp.dpToPx())
|
||||
|
||||
requireNotNull(thumbnail).layoutParams = LayoutParams(
|
||||
requireNotNull(thumbnailContainer).layoutParams = LayoutParams(
|
||||
size.thumbnailSizeDp.dpToPx(),
|
||||
size.thumbnailSizeDp.dpToPx()
|
||||
)
|
||||
|
||||
requireNotNull(thumbnail).layoutParams = FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
|
||||
requireNotNull(labelContainer).layoutParams = LayoutParams(
|
||||
size.labelWidthDp.dpToPx(),
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
@@ -70,6 +90,115 @@ class AudioContentCardView @JvmOverloads constructor(
|
||||
|
||||
fun thumbnailView(): ImageView = requireNotNull(thumbnail)
|
||||
|
||||
fun setTags(tags: Set<AudioContentTag>) {
|
||||
renderTags(requireNotNull(topTagContainer), orderedAudioContentTopTags(tags))
|
||||
renderTags(requireNotNull(bottomTagContainer), orderedAudioContentBottomTags(tags))
|
||||
}
|
||||
|
||||
private fun renderTags(
|
||||
container: LinearLayout,
|
||||
tags: List<AudioContentTag>
|
||||
) {
|
||||
container.removeAllViews()
|
||||
container.visibility = if (tags.isEmpty()) GONE else VISIBLE
|
||||
tags.forEach { tag ->
|
||||
container.addView(createTagView(tag))
|
||||
}
|
||||
}
|
||||
|
||||
private fun createTagView(tag: AudioContentTag): View = when (tag) {
|
||||
AudioContentTag.Original -> createIconTag(R.drawable.ic_content_tag_original)
|
||||
AudioContentTag.Point -> createIconTag(R.drawable.ic_content_tag_point)
|
||||
AudioContentTag.First -> createFirstTag()
|
||||
AudioContentTag.Free -> createFreeTag()
|
||||
}
|
||||
|
||||
private fun createIconTag(drawableResId: Int): ImageView {
|
||||
return ImageView(context).apply {
|
||||
setImageResource(drawableResId)
|
||||
contentDescription = null
|
||||
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
|
||||
layoutParams = LinearLayout.LayoutParams(TAG_HEIGHT_DP.dpToPx(), TAG_HEIGHT_DP.dpToPx())
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFirstTag(): LinearLayout {
|
||||
return LinearLayout(context).apply {
|
||||
orientation = HORIZONTAL
|
||||
background = ContextCompat.getDrawable(context, R.drawable.bg_audio_content_tag_first)
|
||||
layoutParams = LinearLayout.LayoutParams(FIRST_TAG_WIDTH_DP.dpToPx(), TAG_HEIGHT_DP.dpToPx())
|
||||
addView(createFirstStarView())
|
||||
addView(createFirstTextView())
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFirstStarView(): ImageView {
|
||||
return ImageView(context).apply {
|
||||
setImageResource(R.drawable.ic_content_tag_first_star)
|
||||
contentDescription = null
|
||||
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
|
||||
layoutParams = LinearLayout.LayoutParams(FIRST_STAR_SIZE_DP.dpToPx(), FIRST_STAR_SIZE_DP.dpToPx()).apply {
|
||||
marginStart = 2.dpToPx()
|
||||
topMargin = 4.dpToPx()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFirstTextView(): TextView {
|
||||
return TextView(context).apply {
|
||||
text = FIRST_TEXT
|
||||
typeface = ResourcesCompat.getFont(context, R.font.phosphate_solid)
|
||||
setTextColor(ContextCompat.getColor(context, R.color.white))
|
||||
textSize = 16f
|
||||
isSingleLine = true
|
||||
includeFontPadding = false
|
||||
gravity = Gravity.CENTER_VERTICAL
|
||||
layoutParams = LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
).apply {
|
||||
marginStart = 1.dpToPx()
|
||||
topMargin = 2.dpToPx()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun createFreeTag(): TextView {
|
||||
return TextView(context).apply {
|
||||
text = context.getString(R.string.audio_content_tag_free)
|
||||
background = ContextCompat.getDrawable(context, R.drawable.bg_audio_content_tag_free)
|
||||
setTextColor(ContextCompat.getColor(context, R.color.white))
|
||||
textSize = 14f
|
||||
gravity = Gravity.CENTER
|
||||
isSingleLine = true
|
||||
includeFontPadding = false
|
||||
minWidth = FREE_TAG_MIN_WIDTH_DP.dpToPx()
|
||||
setPadding(FREE_TAG_HORIZONTAL_PADDING_DP.dpToPx(), 0, FREE_TAG_HORIZONTAL_PADDING_DP.dpToPx(), 0)
|
||||
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, TAG_HEIGHT_DP.dpToPx())
|
||||
}
|
||||
}
|
||||
|
||||
private fun setThumbnailOutline() {
|
||||
requireNotNull(thumbnailContainer).apply {
|
||||
clipToOutline = true
|
||||
outlineProvider = 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 hasRequiredViews(): Boolean {
|
||||
return thumbnailContainer != null &&
|
||||
thumbnail != null &&
|
||||
topTagContainer != null &&
|
||||
bottomTagContainer != null &&
|
||||
labelContainer != null &&
|
||||
titleText != null &&
|
||||
creatorText != null
|
||||
}
|
||||
|
||||
private fun updateRootWidth(width: Int) {
|
||||
val currentLayoutParams = layoutParams
|
||||
layoutParams = if (currentLayoutParams == null) {
|
||||
@@ -86,5 +215,11 @@ class AudioContentCardView @JvmOverloads constructor(
|
||||
|
||||
private companion object {
|
||||
const val TITLE_CREATOR_GAP_DP = 2
|
||||
const val TAG_HEIGHT_DP = 24
|
||||
const val FIRST_TAG_WIDTH_DP = 62
|
||||
const val FIRST_STAR_SIZE_DP = 17
|
||||
const val FREE_TAG_MIN_WIDTH_DP = 34
|
||||
const val FREE_TAG_HORIZONTAL_PADDING_DP = 6
|
||||
const val FIRST_TEXT = "FIRST"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package kr.co.vividnext.sodalive.v2.widget
|
||||
|
||||
enum class AudioContentTag {
|
||||
Original,
|
||||
First,
|
||||
Point,
|
||||
Free
|
||||
}
|
||||
|
||||
fun Collection<AudioContentTag>.audioContentTopTags(): List<AudioContentTag> = orderedBy(
|
||||
AudioContentTag.Original,
|
||||
AudioContentTag.First
|
||||
)
|
||||
|
||||
fun Collection<AudioContentTag>.audioContentBottomTags(): List<AudioContentTag> = orderedBy(
|
||||
AudioContentTag.Point,
|
||||
AudioContentTag.Free
|
||||
)
|
||||
|
||||
fun orderedAudioContentTopTags(tags: Collection<AudioContentTag>): List<AudioContentTag> = tags.audioContentTopTags()
|
||||
|
||||
fun orderedAudioContentBottomTags(tags: Collection<AudioContentTag>): List<AudioContentTag> = tags.audioContentBottomTags()
|
||||
|
||||
private fun Collection<AudioContentTag>.orderedBy(
|
||||
vararg orderedTags: AudioContentTag
|
||||
): List<AudioContentTag> {
|
||||
val includedTags = toSet()
|
||||
return orderedTags.filter { it in includedTags }
|
||||
}
|
||||
Reference in New Issue
Block a user