From b2338dfcd39da4795b5923749dd4fe2731022067 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 28 Jan 2020 22:35:40 +0100 Subject: [PATCH] Make the TU passes --- .../java/im/vector/riotx/core/utils/Emoji.kt | 36 +++++++++++++++++++ .../composer/rainbow/RainbowGenerator.kt | 4 ++- .../composer/rainbow/RainbowGeneratorTest.kt | 30 +++++++--------- 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt b/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt index f9e5654726..e91a2896bc 100644 --- a/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt +++ b/vector/src/main/java/im/vector/riotx/core/utils/Emoji.kt @@ -113,3 +113,39 @@ fun containsOnlyEmojis(str: String?): Boolean { return res } + +/** + * Same as split, but considering emojis + */ +fun CharSequence.splitEmoji(): List { + val result = mutableListOf() + + var index = 0 + + while (index < length) { + val firstChar = get(index) + + if (firstChar.toInt() == 0x200e) { + // Left to right mark. What should I do with it? + } else if (firstChar.toInt() in 0xD800..0xDBFF && index + 1 < length) { + // We have the start of a surrogate pair + val secondChar = get(index + 1) + + if (secondChar.toInt() in 0xDC00..0xDFFF) { + // We have an emoji + result.add("$firstChar$secondChar") + index++ + } else { + // Not sure what we have here... + result.add("$firstChar") + } + } else { + // Regular char + result.add("$firstChar") + } + + index++ + } + + return result +} diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt index c9defac8e8..9662e2d372 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt @@ -16,6 +16,7 @@ package im.vector.riotx.features.home.room.detail.composer.rainbow +import im.vector.riotx.core.utils.splitEmoji import javax.inject.Inject import kotlin.math.abs import kotlin.math.roundToInt @@ -30,9 +31,10 @@ class RainbowGenerator @Inject constructor() { val frequency = 360f / text.length return text + .splitEmoji() .mapIndexed { idx, letter -> // Do better than React-Sdk: Avoid adding font color for spaces - if (letter == ' ') { + if (letter == " ") { "$letter" } else { val dashColor = hueToRGB(idx * frequency, 1.0f, 0.5f).toDashColor() diff --git a/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt b/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt index 521fb15bf2..6a798638f2 100644 --- a/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt +++ b/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt @@ -64,7 +64,7 @@ class RainbowGeneratorTest { @Test fun testEmoji1() { - assertEquals("""\uD83E\uDD1E""", rainbowGenerator.generate("\uD83E\uDD1E")) // 🤞 + assertEquals("""🤞""", rainbowGenerator.generate("\uD83E\uDD1E")) // 🤞 } @Test @@ -75,24 +75,20 @@ class RainbowGeneratorTest { @Test fun testEmojiMix() { val expected = """ - T - h - i - s + H + e + l + l + o - i - s + 🤞 - a - - r - a - i - n - b - o - w - ! + w + o + r + l + d + ! """ .trimIndent() .replace("\n", "")