diff --git a/app/src/main/kotlin/com/vanniktech/emoji/sample/ChatAdapter.kt b/app/src/main/kotlin/com/vanniktech/emoji/sample/ChatAdapter.kt index 5558fa0f94..dc5da61903 100644 --- a/app/src/main/kotlin/com/vanniktech/emoji/sample/ChatAdapter.kt +++ b/app/src/main/kotlin/com/vanniktech/emoji/sample/ChatAdapter.kt @@ -34,10 +34,10 @@ internal class ChatAdapter : RecyclerView.Adapter() { 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) diff --git a/emoji-ios/src/commonTest/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt b/emoji-ios/src/commonTest/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt index 499f6f9b37..b8506199f5 100644 --- a/emoji-ios/src/commonTest/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt +++ b/emoji-ios/src/commonTest/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt @@ -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) } @@ -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) } diff --git a/emoji/src/androidMain/kotlin/com/vanniktech/emoji/EmojiManagers.kt b/emoji/src/androidMain/kotlin/com/vanniktech/emoji/EmojiManagers.kt index 616ecc9e1d..a3d33cc3df 100644 --- a/emoji/src/androidMain/kotlin/com/vanniktech/emoji/EmojiManagers.kt +++ b/emoji/src/androidMain/kotlin/com/vanniktech/emoji/EmojiManagers.kt @@ -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, ) } diff --git a/emoji/src/androidMain/kotlin/com/vanniktech/emoji/inputfilters/MaximalNumberOfEmojisInputFilter.kt b/emoji/src/androidMain/kotlin/com/vanniktech/emoji/inputfilters/MaximalNumberOfEmojisInputFilter.kt index 056015c8d6..726cd4239a 100644 --- a/emoji/src/androidMain/kotlin/com/vanniktech/emoji/inputfilters/MaximalNumberOfEmojisInputFilter.kt +++ b/emoji/src/androidMain/kotlin/com/vanniktech/emoji/inputfilters/MaximalNumberOfEmojisInputFilter.kt @@ -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 } } diff --git a/emoji/src/commonMain/kotlin/com/vanniktech/emoji/EmojiManager.kt b/emoji/src/commonMain/kotlin/com/vanniktech/emoji/EmojiManager.kt index c437ad8afa..865538cff8 100644 --- a/emoji/src/commonMain/kotlin/com/vanniktech/emoji/EmojiManager.kt +++ b/emoji/src/commonMain/kotlin/com/vanniktech/emoji/EmojiManager.kt @@ -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 } diff --git a/emoji/src/commonMain/kotlin/com/vanniktech/emoji/Emojis.kt b/emoji/src/commonMain/kotlin/com/vanniktech/emoji/Emojis.kt index d8c5cf09a1..a892b3ff0e 100644 --- a/emoji/src/commonMain/kotlin/com/vanniktech/emoji/Emojis.kt +++ b/emoji/src/commonMain/kotlin/com/vanniktech/emoji/Emojis.kt @@ -51,4 +51,11 @@ fun CharSequence?.emojis(): List = 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, + ) +} diff --git a/emoji/src/commonTest/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt b/emoji/src/commonTest/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt index d1fd1aa719..ef43eac71f 100644 --- a/emoji/src/commonTest/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt +++ b/emoji/src/commonTest/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt @@ -78,8 +78,8 @@ 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, @@ -87,6 +87,14 @@ class EmojiManagerTest { ), actual = EmojiManager.findAllEmojis(text), ) + assertEquals( + expected = """🎈""", + actual = text.substring(firstExpectedRange.range), + ) + assertEquals( + expected = """🪀""", + actual = text.substring(secondExpectedRange.range), + ) } @Test fun findAllEmojisEmpty() { diff --git a/jvm/src/main/kotlin/com/vanniktech/emoji/jvm/Main.kt b/jvm/src/main/kotlin/com/vanniktech/emoji/jvm/Main.kt index 2f62481b82..876b3ac134 100644 --- a/jvm/src/main/kotlin/com/vanniktech/emoji/jvm/Main.kt +++ b/jvm/src/main/kotlin/com/vanniktech/emoji/jvm/Main.kt @@ -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)