feat(feed): 랭킹 강조 텍스트 계약을 추가한다
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package kr.co.vividnext.sodalive.v2.widget.feed
|
||||
|
||||
data class FeedRankHighlight(
|
||||
val start: Int,
|
||||
val endExclusive: Int
|
||||
) {
|
||||
fun coerceIn(text: CharSequence): FeedRankHighlight? {
|
||||
if (text.isEmpty()) return null
|
||||
val safeStart = start.coerceIn(0, text.length)
|
||||
val safeEnd = endExclusive.coerceIn(0, text.length)
|
||||
return if (safeStart < safeEnd) FeedRankHighlight(safeStart, safeEnd) else null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package kr.co.vividnext.sodalive.v2.widget.feed
|
||||
|
||||
import android.text.SpannableString
|
||||
import android.text.Spanned
|
||||
import android.text.style.ForegroundColorSpan
|
||||
|
||||
object FeedRankTextStyler {
|
||||
fun style(
|
||||
text: String,
|
||||
highlightRanges: List<FeedRankHighlight>,
|
||||
highlightColor: Int
|
||||
): SpannableString {
|
||||
val spannable = SpannableString(text)
|
||||
highlightRanges.mapNotNull { it.coerceIn(text) }.forEach { range ->
|
||||
spannable.setSpan(
|
||||
ForegroundColorSpan(highlightColor),
|
||||
range.start,
|
||||
range.endExclusive,
|
||||
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
||||
)
|
||||
}
|
||||
return spannable
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user