From cade5c7b61f844369b573f8bd0d8f9ed6ce110e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Br=C3=BCggemann?= Date: Fri, 8 Mar 2024 20:46:16 +0100 Subject: [PATCH 1/3] Support AnnotatedString parameters --- .../kotlin/org/kodein/emoji/compose/text.kt | 16 +++++++++++----- .../org/kodein/emoji/compose/iosPlatform.kt | 4 ++-- .../org/kodein/emoji/compose/jvmPlatform.kt | 4 ++-- .../org/kodein/emoji/compose/platformWasm.kt | 2 +- .../kotlin/org/kodein/emoji/CodePoints.kt | 2 +- .../org/kodein/emoji/EmojiCodePointTree.kt | 4 ++-- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt b/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt index 5a3d833..0086538 100644 --- a/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt +++ b/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt @@ -29,7 +29,7 @@ public fun String.withEmoji(): String { @Composable private fun WithNotoEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit, createInlineTextContent: suspend (FoundEmoji) -> InlineTextContent? ) { @@ -55,7 +55,10 @@ private fun WithNotoEmoji( val annotatedString = buildAnnotatedString { var start = 0 all.forEach { (found, inlineTextContent) -> - append(text.substring(start, found.start)) + if (text is AnnotatedString) + append(text.subSequence(start, found.start)) + else + append(text.substring(start, found.start)) val itc = inlineTextContent.value if (itc != null) { val inlineContentID = "emoji:${found.emoji}" @@ -66,7 +69,10 @@ private fun WithNotoEmoji( } start = found.end } - append(text.substring(start, text.length)) + if (text is AnnotatedString) + append(text.subSequence(start, text.length)) + else + append(text.substring(start, text.length)) } content(annotatedString, inlineContent) @@ -90,7 +96,7 @@ private suspend fun createNotoSvgInlineContent(emoji: Emoji, download: suspend ( @Composable public fun WithNotoImageEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { val download = LocalEmojiDownloader.current @@ -133,6 +139,6 @@ public fun WithNotoAnimatedEmoji( @Composable public expect fun WithPlatformEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) diff --git a/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt b/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt index 80409ec..e5ba632 100644 --- a/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt +++ b/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt @@ -34,10 +34,10 @@ internal actual suspend fun platformDownloadBytes(url: String): ByteArray { @Composable public actual fun WithPlatformEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { - content(AnnotatedString(text), emptyMap()) + content(AnnotatedString.Builder().append(text).toAnnotatedString(), emptyMap()) } @Composable diff --git a/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt b/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt index cfad595..216057a 100644 --- a/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt +++ b/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt @@ -17,10 +17,10 @@ internal actual suspend fun platformDownloadBytes(url: String): ByteArray = @Composable public actual fun WithPlatformEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { - content(AnnotatedString(text), emptyMap()) + content(AnnotatedString.Builder().append(text).toAnnotatedString(), emptyMap()) } @Composable diff --git a/emoji-compose/src/wasmJsMain/kotlin/org/kodein/emoji/compose/platformWasm.kt b/emoji-compose/src/wasmJsMain/kotlin/org/kodein/emoji/compose/platformWasm.kt index 1e8b03a..1c93393 100644 --- a/emoji-compose/src/wasmJsMain/kotlin/org/kodein/emoji/compose/platformWasm.kt +++ b/emoji-compose/src/wasmJsMain/kotlin/org/kodein/emoji/compose/platformWasm.kt @@ -25,7 +25,7 @@ internal actual suspend fun platformDownloadBytes(url: String): ByteArray { @Composable public actual fun WithPlatformEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { WithNotoImageEmoji(text, content) diff --git a/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/CodePoints.kt b/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/CodePoints.kt index 554d63a..49505ea 100644 --- a/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/CodePoints.kt +++ b/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/CodePoints.kt @@ -8,7 +8,7 @@ internal fun codePointCharLength(code: Int) = if (isCodePointInOneChar(code)) 1 else 2 -internal fun codePointAt(string: String, index: Int): Int { +internal fun codePointAt(string: CharSequence, index: Int): Int { if (isCodePointInOneChar(string[index].code)) { return string[index].code } diff --git a/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/EmojiCodePointTree.kt b/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/EmojiCodePointTree.kt index cf77220..87bde5c 100644 --- a/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/EmojiCodePointTree.kt +++ b/emoji-kt/src/commonMain/kotlin/org/kodein/emoji/EmojiCodePointTree.kt @@ -71,7 +71,7 @@ public data class FoundEmoji( public val length: Int get() = end - start } -private tailrec fun follow(string: String, index: Int, node: EmojiFinder.Node, start: Int): FoundEmoji? { +private tailrec fun follow(string: CharSequence, index: Int, node: EmojiFinder.Node, start: Int): FoundEmoji? { if (index >= string.length) return node.emoji?.let { FoundEmoji(start, index, it) } val branches = node.branches ?: return node.emoji?.let { FoundEmoji(start, index, it) } val code = codePointAt(string, index) @@ -87,7 +87,7 @@ private tailrec fun follow(string: String, index: Int, node: EmojiFinder.Node, s /** * Finds all emojis inside a String and returns their position and details. */ -public fun EmojiFinder.findEmoji(str: String): Sequence = +public fun EmojiFinder.findEmoji(str: CharSequence): Sequence = sequence { var index = 0 while (index < str.length) { From ce53f46c444df1f9c41e6babed83e8a3f523021d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Br=C3=BCggemann?= Date: Fri, 8 Mar 2024 20:46:53 +0100 Subject: [PATCH 2/3] Another parameter --- .../src/commonMain/kotlin/org/kodein/emoji/compose/text.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt b/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt index 0086538..46a0a29 100644 --- a/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt +++ b/emoji-compose/src/commonMain/kotlin/org/kodein/emoji/compose/text.kt @@ -126,7 +126,7 @@ private suspend fun createNotoLottieInlineContent(emoji: Emoji, download: suspen @Composable public fun WithNotoAnimatedEmoji( - text: String, + text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { val download = LocalEmojiDownloader.current From 49a6ec9f428d4f0d32d8aab47f68e93c48294712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Br=C3=BCggemann?= Date: Fri, 8 Mar 2024 20:53:24 +0100 Subject: [PATCH 3/3] Caching --- .../iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt | 4 +++- .../kotlin/org/kodein/emoji/compose/jvmPlatform.kt | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt b/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt index e5ba632..4118e59 100644 --- a/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt +++ b/emoji-compose/src/iosMain/kotlin/org/kodein/emoji/compose/iosPlatform.kt @@ -2,6 +2,7 @@ package org.kodein.emoji.compose import androidx.compose.foundation.text.InlineTextContent import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.text.AnnotatedString import kotlinx.cinterop.ExperimentalForeignApi @@ -37,7 +38,8 @@ public actual fun WithPlatformEmoji( text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { - content(AnnotatedString.Builder().append(text).toAnnotatedString(), emptyMap()) + val annotatedString = remember(text) { AnnotatedString.Builder().append(text).toAnnotatedString() } + content(annotatedString, emptyMap()) } @Composable diff --git a/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt b/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt index 216057a..4d5299a 100644 --- a/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt +++ b/emoji-compose/src/jvmBasedMain/kotlin/org/kodein/emoji/compose/jvmPlatform.kt @@ -2,6 +2,7 @@ package org.kodein.emoji.compose import androidx.compose.foundation.text.InlineTextContent import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.text.AnnotatedString import kotlinx.coroutines.Dispatchers @@ -20,7 +21,8 @@ public actual fun WithPlatformEmoji( text: CharSequence, content: @Composable (AnnotatedString, Map) -> Unit ) { - content(AnnotatedString.Builder().append(text).toAnnotatedString(), emptyMap()) + val annotatedString = remember(text) { AnnotatedString.Builder().append(text).toAnnotatedString() } + content(annotatedString, emptyMap()) } @Composable