feat(gift): 약관 필수 문구 링크를 추가한다
This commit is contained in:
@@ -0,0 +1,84 @@
|
|||||||
|
package kr.co.vividnext.sodalive.v2.gift
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.text.Spannable
|
||||||
|
import android.text.SpannableString
|
||||||
|
import android.text.Spanned
|
||||||
|
import android.text.style.ClickableSpan
|
||||||
|
import android.view.MotionEvent
|
||||||
|
import android.view.View
|
||||||
|
import android.widget.CheckBox
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import kr.co.vividnext.sodalive.R
|
||||||
|
|
||||||
|
internal fun Context.openGiftAgreementPage() {
|
||||||
|
startActivity(Intent(Intent.ACTION_VIEW, GIFT_AGREEMENT_URL.toUri()))
|
||||||
|
}
|
||||||
|
|
||||||
|
private const val GIFT_AGREEMENT_URL = "https://app.notion.com/p/3eb27b359673802b937ae741ab7b4ad3"
|
||||||
|
|
||||||
|
internal fun CheckBox.setRequiredLink(onClick: () -> Unit) {
|
||||||
|
val requiredLength = context.getString(R.string.gift_send_required).length
|
||||||
|
val linkedText = SpannableString(text).apply {
|
||||||
|
setSpan(
|
||||||
|
object : ClickableSpan() {
|
||||||
|
override fun onClick(widget: View) = onClick()
|
||||||
|
},
|
||||||
|
length - requiredLength,
|
||||||
|
length,
|
||||||
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
text = linkedText
|
||||||
|
highlightColor = Color.TRANSPARENT
|
||||||
|
var pressedLink: ClickableSpan? = null
|
||||||
|
setOnTouchListener { _, event ->
|
||||||
|
when (event.actionMasked) {
|
||||||
|
MotionEvent.ACTION_DOWN -> {
|
||||||
|
pressedLink = clickableSpanAt(event)
|
||||||
|
pressedLink != null
|
||||||
|
}
|
||||||
|
|
||||||
|
MotionEvent.ACTION_UP -> {
|
||||||
|
val link = pressedLink
|
||||||
|
pressedLink = null
|
||||||
|
if (link == null) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
if (clickableSpanAt(event) === link) {
|
||||||
|
link.onClick(this)
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MotionEvent.ACTION_CANCEL -> (pressedLink != null).also { pressedLink = null }
|
||||||
|
else -> pressedLink != null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun CheckBox.clickableSpanAt(event: MotionEvent): ClickableSpan? {
|
||||||
|
val layout = layout ?: return null
|
||||||
|
val vertical = event.y - totalPaddingTop + scrollY
|
||||||
|
if (vertical < layout.getLineTop(0) || vertical >= layout.getLineBottom(layout.lineCount - 1)) return null
|
||||||
|
val horizontal = event.x - totalPaddingLeft + scrollX
|
||||||
|
val line = layout.getLineForVertical(vertical.toInt())
|
||||||
|
if (horizontal < layout.getLineLeft(line) || horizontal > layout.getLineRight(line)) return null
|
||||||
|
val text = text as? Spanned ?: return null
|
||||||
|
val lineStart = layout.getLineStart(line)
|
||||||
|
val lineEnd = layout.getLineEnd(line)
|
||||||
|
return text.getSpans(lineStart, lineEnd, ClickableSpan::class.java).firstOrNull { link ->
|
||||||
|
val start = maxOf(text.getSpanStart(link), lineStart)
|
||||||
|
val end = minOf(text.getSpanEnd(link), lineEnd)
|
||||||
|
if (start >= end) {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
val first = if (start == lineStart) layout.getLineLeft(line) else layout.getPrimaryHorizontal(start)
|
||||||
|
val last = if (end == lineEnd) layout.getLineRight(line) else layout.getPrimaryHorizontal(end)
|
||||||
|
horizontal in minOf(first, last)..maxOf(first, last)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user