Skip to content

Commit

Permalink
Bug fix: Correct range of EmojiRange. It was off by one. (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech authored Aug 2, 2024
1 parent 7707582 commit 1318293
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ internal class ChatAdapter : RecyclerView.Adapter<ChatViewHolder>() {

override fun onBindViewHolder(chatViewHolder: ChatViewHolder, position: Int) {
val text = texts[position]
val (isOnlyEmojis, emojis) = text.emojiInformation()
val emojiInformation = text.emojiInformation()
val res: Int = when {
isOnlyEmojis && emojis.size == 1 -> R.dimen.emoji_size_single_emoji
isOnlyEmojis && emojis.size > 1 -> R.dimen.emoji_size_only_emojis
emojiInformation.isOnlyEmojis && emojiInformation.emojis.size == 1 -> R.dimen.emoji_size_single_emoji
emojiInformation.isOnlyEmojis && emojiInformation.emojis.size > 1 -> R.dimen.emoji_size_only_emojis
else -> R.dimen.emoji_size_default
}
chatViewHolder.textView.setEmojiSizeRes(res, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class EmojiUtilsTest {
assertEquals(expected = true, actual = emojiInformation.isOnlyEmojis)
assertEquals(expected = 2, actual = emojiInformation.emojis.size)

assertEquals(expected = 0..5, actual = emojiInformation.emojis[0].range)
assertEquals(expected = mage1, actual = text.substring(emojiInformation.emojis[0].range.first, emojiInformation.emojis[0].range.last))
assertEquals(expected = 0..4, actual = emojiInformation.emojis[0].range)
assertEquals(expected = mage1, actual = text.substring(emojiInformation.emojis[0].range))
assertEquals(expected = mage1, actual = emojiInformation.emojis[0].emoji.unicode)

assertEquals(expected = 5..12, actual = emojiInformation.emojis[1].range)
assertEquals(expected = mage2, actual = text.substring(emojiInformation.emojis[1].range.first, emojiInformation.emojis[1].range.last))
assertEquals(expected = 5..11, actual = emojiInformation.emojis[1].range)
assertEquals(expected = mage2, actual = text.substring(emojiInformation.emojis[1].range))
assertEquals(expected = mage2, actual = emojiInformation.emojis[1].emoji.unicode)
}

Expand All @@ -38,12 +38,12 @@ class EmojiUtilsTest {
assertEquals(expected = false, actual = emojiInformation.isOnlyEmojis)
assertEquals(expected = 2, actual = emojiInformation.emojis.size)

assertEquals(expected = 7..9, actual = emojiInformation.emojis[0].range)
assertEquals(expected = hamburger, actual = text.substring(emojiInformation.emojis[0].range.first, emojiInformation.emojis[0].range.last))
assertEquals(expected = 7..8, actual = emojiInformation.emojis[0].range)
assertEquals(expected = hamburger, actual = text.substring(emojiInformation.emojis[0].range))
assertEquals(expected = hamburger, actual = emojiInformation.emojis[0].emoji.unicode)

assertEquals(expected = 23..25, actual = emojiInformation.emojis[1].range)
assertEquals(expected = cheese, actual = text.substring(emojiInformation.emojis[1].range.first, emojiInformation.emojis[1].range.last))
assertEquals(expected = 23..24, actual = emojiInformation.emojis[1].range)
assertEquals(expected = cheese, actual = text.substring(emojiInformation.emojis[1].range))
assertEquals(expected = cheese, actual = emojiInformation.emojis[1].emoji.unicode)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal val defaultEmojiReplacer = EmojiReplacer { context, text, emojiSize, _
text.setSpan(
EmojiSpan(context, emoji, emojiSize),
range.first,
range.last,
range.last + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class MaximalNumberOfEmojisInputFilter(
private val maxCount: Int,
) : InputFilter {
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
val (_, emojis) = dest.subSequence(0, dest.length).emojiInformation()
val emojiInformation = dest.subSequence(0, dest.length).emojiInformation()
return when {
emojis.size >= maxCount -> "" // Reject.
emojiInformation.emojis.size >= maxCount -> "" // Reject.
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ object EmojiManager {
val emoji = findEmoji(it.value)

if (emoji != null) {
EmojiRange(emoji, IntRange(it.range.first, it.range.last + 1))
EmojiRange(
emoji = emoji,
range = it.range,
)
} else {
null
}
Expand Down
9 changes: 8 additions & 1 deletion emoji/src/commonMain/kotlin/com/vanniktech/emoji/Emojis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ fun CharSequence?.emojis(): List<EmojiRange> = EmojiManager.findAllEmojis(this)
fun CharSequence?.emojisCount() = emojis().size

/** Returns a class that contains all of the emoji information that was found in the given text. */
fun CharSequence?.emojiInformation(): EmojiInformation = EmojiInformation(isOnlyEmojis(), emojis())
fun CharSequence?.emojiInformation(): EmojiInformation {
val emojis = emojis()
val isOnlyEmojis = isOnlyEmojis()
return EmojiInformation(
isOnlyEmojis = isOnlyEmojis,
emojis = emojis,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,23 @@ class EmojiManagerTest {
@Test fun findAllEmojisNormal() {
EmojiManager.install(TestEmojiProvider)
val text = "te${emojiBalloon.unicode}st${emojiYoYo.unicode}"
val firstExpectedRange = EmojiRange(emojiBalloon, 2..4)
val secondExpectedRange = EmojiRange(emojiYoYo, 6..8)
val firstExpectedRange = EmojiRange(emojiBalloon, 2..3)
val secondExpectedRange = EmojiRange(emojiYoYo, 6..7)
assertEquals(
expected = listOf(
firstExpectedRange,
secondExpectedRange,
),
actual = EmojiManager.findAllEmojis(text),
)
assertEquals(
expected = """🎈""",
actual = text.substring(firstExpectedRange.range),
)
assertEquals(
expected = """🪀""",
actual = text.substring(secondExpectedRange.range),
)
}

@Test fun findAllEmojisEmpty() {
Expand Down
2 changes: 1 addition & 1 deletion jvm/src/main/kotlin/com/vanniktech/emoji/jvm/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fun main() {
EmojiManager.install(IosEmojiProvider())

val emojiString = """Hello from JVM ❤️💚💙"""
println("\nGetting emoji information from $emojiString:")
println("\nGetting emoji information from: $emojiString")
val emojiInformation = emojiString.emojiInformation()
println(emojiInformation)

Expand Down

0 comments on commit 1318293

Please sign in to comment.