Skip to content

Commit

Permalink
Breaking: EmojiInformation rename emojis to emojiRange & add new emoj…
Browse files Browse the repository at this point in the history
…iCount property.
  • Loading branch information
vanniktech committed Aug 3, 2024
1 parent 7e4f82d commit edd85b2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ internal class ChatAdapter : RecyclerView.Adapter<ChatViewHolder>() {
val text = texts[position]
val emojiInformation = text.emojiInformation()
val res: Int = when {
emojiInformation.isOnlyEmojis && emojiInformation.emojis.size == 1 -> R.dimen.emoji_size_single_emoji
emojiInformation.isOnlyEmojis && emojiInformation.emojis.size > 1 -> R.dimen.emoji_size_only_emojis
emojiInformation.isOnlyEmojis && emojiInformation.emojiCount == 1 -> R.dimen.emoji_size_single_emoji
emojiInformation.isOnlyEmojis && emojiInformation.emojiCount > 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 @@ -17,15 +17,15 @@ class EmojiInformationTest {
val emojiInformation = text.emojiInformation()
assertEquals(expected = text.length, actual = emojiInformation.visualLength)
assertEquals(expected = false, actual = emojiInformation.isOnlyEmojis)
assertEquals(expected = 0, actual = emojiInformation.emojis.size)
assertEquals(expected = 0, actual = emojiInformation.emojiCount)
}

@Test fun plain() {
val text = "Hello World!"
val emojiInformation = text.emojiInformation()
assertEquals(expected = text.length, actual = emojiInformation.visualLength)
assertEquals(expected = false, actual = emojiInformation.isOnlyEmojis)
assertEquals(expected = 0, actual = emojiInformation.emojis.size)
assertEquals(expected = 0, actual = emojiInformation.emojiCount)
}

@Test fun femaleTinted() {
Expand All @@ -35,15 +35,15 @@ class EmojiInformationTest {
val emojiInformation = text.emojiInformation()
assertEquals(expected = 2, actual = emojiInformation.visualLength)
assertEquals(expected = true, actual = emojiInformation.isOnlyEmojis)
assertEquals(expected = 2, actual = emojiInformation.emojis.size)
assertEquals(expected = 2, actual = emojiInformation.emojiCount)

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 = 0..4, actual = emojiInformation.emojiRanges[0].range)
assertEquals(expected = mage1, actual = text.substring(emojiInformation.emojiRanges[0].range))
assertEquals(expected = mage1, actual = emojiInformation.emojiRanges[0].emoji.unicode)

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)
assertEquals(expected = 5..11, actual = emojiInformation.emojiRanges[1].range)
assertEquals(expected = mage2, actual = text.substring(emojiInformation.emojiRanges[1].range))
assertEquals(expected = mage2, actual = emojiInformation.emojiRanges[1].emoji.unicode)
}

@Test fun mixed() {
Expand All @@ -53,15 +53,15 @@ class EmojiInformationTest {
val emojiInformation = text.emojiInformation()
assertEquals(expected = 23, actual = emojiInformation.visualLength)
assertEquals(expected = false, actual = emojiInformation.isOnlyEmojis)
assertEquals(expected = 2, actual = emojiInformation.emojis.size)
assertEquals(expected = 2, actual = emojiInformation.emojiCount)

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 = 7..8, actual = emojiInformation.emojiRanges[0].range)
assertEquals(expected = hamburger, actual = text.substring(emojiInformation.emojiRanges[0].range))
assertEquals(expected = hamburger, actual = emojiInformation.emojiRanges[0].emoji.unicode)

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)
assertEquals(expected = 23..24, actual = emojiInformation.emojiRanges[1].range)
assertEquals(expected = cheese, actual = text.substring(emojiInformation.emojiRanges[1].range))
assertEquals(expected = cheese, actual = emojiInformation.emojiRanges[1].emoji.unicode)
}

@Test fun single() {
Expand All @@ -81,27 +81,27 @@ class EmojiInformationTest {
emojis.forEach {
val prefixed = "f$it".emojiInformation()
assertEquals(message = it, expected = 2, actual = prefixed.visualLength)
assertEquals(message = it, expected = 1, actual = prefixed.emojis.size)
assertEquals(message = it, expected = 1, actual = prefixed.emojiCount)
assertEquals(message = it, expected = false, actual = prefixed.isOnlyEmojis)

val postfixed = "${it}f".emojiInformation()
assertEquals(message = it, expected = 2, actual = postfixed.visualLength)
assertEquals(message = it, expected = 1, actual = postfixed.emojis.size)
assertEquals(message = it, expected = 1, actual = postfixed.emojiCount)
assertEquals(message = it, expected = false, actual = postfixed.isOnlyEmojis)

val single = it.emojiInformation()
assertEquals(message = it, expected = 1, actual = single.visualLength)
assertEquals(message = it, expected = 1, actual = single.emojis.size)
assertEquals(message = it, expected = 1, actual = single.emojiCount)
assertEquals(message = it, expected = true, actual = single.isOnlyEmojis)

val triple = "$it$it$it".emojiInformation()
assertEquals(message = it, expected = 3, actual = triple.visualLength)
assertEquals(message = it, expected = 3, actual = triple.emojis.size)
assertEquals(message = it, expected = 3, actual = triple.emojiCount)
assertEquals(message = it, expected = true, actual = triple.isOnlyEmojis)

val spaces = "$it $it".emojiInformation()
assertEquals(message = it, expected = 3, actual = spaces.visualLength)
assertEquals(message = it, expected = 2, actual = spaces.emojis.size)
assertEquals(message = it, expected = 2, actual = spaces.emojiCount)
assertEquals(message = it, expected = true, actual = spaces.isOnlyEmojis)

assertEquals(expected = triple, actual = triple)
Expand Down
6 changes: 4 additions & 2 deletions emoji/api/current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ package com.vanniktech.emoji {
method public int component1();
method public boolean component2();
method public java.util.List<com.vanniktech.emoji.EmojiRange> component3();
method public com.vanniktech.emoji.EmojiInformation copy(int visualLength, boolean isOnlyEmojis, java.util.List<com.vanniktech.emoji.EmojiRange> emojis);
field public final java.util.List<com.vanniktech.emoji.EmojiRange> emojis;
method public com.vanniktech.emoji.EmojiInformation copy(int visualLength, boolean isOnlyEmojis, java.util.List<com.vanniktech.emoji.EmojiRange> emojiRanges);
method public int getEmojiCount();
property public final int emojiCount;
field public final java.util.List<com.vanniktech.emoji.EmojiRange> emojiRanges;
field public final boolean isOnlyEmojis;
field public final int visualLength;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MaximalNumberOfEmojisInputFilter(
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
val emojiInformation = dest.subSequence(0, dest.length).emojiInformation()
return when {
emojiInformation.emojis.size >= maxCount -> "" // Reject.
emojiInformation.emojiCount >= maxCount -> "" // Reject.
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import kotlin.jvm.JvmField

data class EmojiInformation internal constructor(
/**
* The visual length with respect to the found [emojis].
* The visual length with respect to the found [emojiRanges].
* One [Emoji] can be made up of a bunch of characters but still displayed as a single one.
*/
@JvmField val visualLength: Int,
@JvmField val isOnlyEmojis: Boolean,
@JvmField val emojis: List<EmojiRange>,
)
@JvmField val emojiRanges: List<EmojiRange>,
) {
val emojiCount = emojiRanges.size
}
8 changes: 4 additions & 4 deletions emoji/src/commonMain/kotlin/com/vanniktech/emoji/Emojis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ fun CharSequence?.emojisCount() = emojis().size

/** Returns the [EmojiInformation] on the given [CharSequence]. */
fun CharSequence.emojiInformation(): EmojiInformation {
val emojis = emojis()
val emojiRanges = emojis()
return EmojiInformation(
visualLength = length - emojis.sumOf { it.range.last + 1 - it.range.first } + emojis.size,
isOnlyEmojis = isNotBlank() && emojis.reversed().fold(this) { string, emojiRange -> string.removeRange(emojiRange.range) }.isBlank(),
emojis = emojis,
visualLength = length - emojiRanges.sumOf { it.range.last + 1 - it.range.first } + emojiRanges.size,
isOnlyEmojis = isNotBlank() && emojiRanges.reversed().fold(this) { string, emojiRange -> string.removeRange(emojiRange.range) }.isBlank(),
emojiRanges = emojiRanges,
)
}

0 comments on commit edd85b2

Please sign in to comment.