feat(creator): 채널 sort popup을 공통 컴포넌트로 추가한다
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
package kr.co.vividnext.sodalive.v2.creator.channel.ui
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.Outline
|
||||
import android.graphics.Rect
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.ViewOutlineProvider
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.PopupWindow
|
||||
import android.widget.TextView
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import kr.co.vividnext.sodalive.R
|
||||
import kr.co.vividnext.sodalive.v2.common.data.ContentSort
|
||||
import kr.co.vividnext.sodalive.v2.creator.channel.model.toSortOptionUiModel
|
||||
|
||||
class CreatorChannelSortPopup(
|
||||
private val anchor: View,
|
||||
private val selectedSort: ContentSort,
|
||||
private val onSortSelected: (ContentSort) -> Unit
|
||||
) {
|
||||
|
||||
private val popupWindow: PopupWindow = PopupWindow(anchor.context).apply {
|
||||
contentView = createContentView()
|
||||
width = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
height = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
isOutsideTouchable = true
|
||||
isFocusable = true
|
||||
setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
|
||||
}
|
||||
|
||||
fun show() {
|
||||
val content = popupWindow.contentView
|
||||
content.measure(
|
||||
View.MeasureSpec.makeMeasureSpec(anchor.rootView.width, View.MeasureSpec.AT_MOST),
|
||||
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
|
||||
)
|
||||
popupWindow.showAsDropDown(anchor, calculateHorizontalOffset(content.measuredWidth), 0)
|
||||
}
|
||||
|
||||
fun dismiss() {
|
||||
popupWindow.dismiss()
|
||||
}
|
||||
|
||||
private fun createContentView(): View {
|
||||
val root = FrameLayout(anchor.context)
|
||||
val view = LayoutInflater.from(anchor.context).inflate(
|
||||
R.layout.view_creator_channel_sort_menu,
|
||||
root,
|
||||
false
|
||||
)
|
||||
view.clipToOutline = true
|
||||
view.outlineProvider = object : ViewOutlineProvider() {
|
||||
override fun getOutline(view: View, outline: Outline) {
|
||||
outline.setRoundRect(0, 0, view.width, view.height, view.resources.getDimension(R.dimen.radius_14))
|
||||
}
|
||||
}
|
||||
val container = view.findViewById<LinearLayout>(R.id.layout_creator_channel_sort_options)
|
||||
val sample = view.findViewById<TextView>(R.id.tv_creator_channel_sort_option_sample)
|
||||
container.removeView(sample)
|
||||
|
||||
ContentSort.entries.map { it.toSortOptionUiModel(selectedSort) }.forEach { option ->
|
||||
val row = TextView(anchor.context).apply {
|
||||
layoutParams = LinearLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
typeface = sample.typeface
|
||||
includeFontPadding = false
|
||||
setPadding(sample.paddingStart, sample.paddingTop, sample.paddingEnd, sample.paddingBottom)
|
||||
setText(option.labelResId)
|
||||
setTextColor(sample.currentTextColor)
|
||||
textSize = 16f
|
||||
if (option.isSelected) {
|
||||
setBackgroundResource(R.drawable.bg_creator_channel_sort_selected)
|
||||
}
|
||||
setOnClickListener {
|
||||
if (option.sort != selectedSort) {
|
||||
onSortSelected(option.sort)
|
||||
}
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
container.addView(row)
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
private fun calculateHorizontalOffset(popupWidth: Int): Int {
|
||||
val visibleDisplayFrame = Rect()
|
||||
anchor.rootView.getWindowVisibleDisplayFrame(visibleDisplayFrame)
|
||||
|
||||
val anchorLocation = IntArray(2)
|
||||
anchor.getLocationOnScreen(anchorLocation)
|
||||
val popupRight = anchorLocation[0] + popupWidth
|
||||
return if (popupRight > visibleDisplayFrame.right) {
|
||||
visibleDisplayFrame.right - popupRight
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user