From de2b1a6c95adf34052cef40aefdaf46910a1b5ee Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 14 Mar 2025 10:24:03 -0400 Subject: [PATCH] Optimizes caching for hidden chars on emoji --- .../amethyst/service/CachedRichTextParser.kt | 6 +++ .../amethyst/ui/components/RichTextViewer.kt | 18 ++++--- .../amethyst/ui/note/MultiSetCompose.kt | 21 ++++---- .../ui/note/UpdateReactionTypeDialog.kt | 51 ------------------- 4 files changed, 27 insertions(+), 69 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/CachedRichTextParser.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/CachedRichTextParser.kt index 55a13f3b6..cc5b30f6b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/CachedRichTextParser.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/CachedRichTextParser.kt @@ -41,6 +41,12 @@ object CachedRichTextParser { return result } + fun cachedText( + content: String, + tags: ImmutableListOfLists, + callbackUri: String? = null, + ): RichTextViewerState? = richTextCache[hashCodeCache(content, tags, callbackUri)] + fun parseText( content: String, tags: ImmutableListOfLists, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt index 1a5d99cb7..0320f5086 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/RichTextViewer.kt @@ -530,20 +530,22 @@ fun DisplaySecretEmoji( ) { if (canPreview && quotesLeft > 0) { var secretContent by remember { - mutableStateOf(null) + mutableStateOf(CachedRichTextParser.cachedText(EmojiCoder.decode(segment.segmentText), state.tags)) } var showPopup by remember { mutableStateOf(false) } - LaunchedEffect(segment) { - launch(Dispatchers.Default) { - secretContent = - CachedRichTextParser.parseText( - EmojiCoder.decode(segment.segmentText), - state.tags, - ) + if (secretContent == null) { + LaunchedEffect(segment) { + launch(Dispatchers.Default) { + secretContent = + CachedRichTextParser.parseText( + EmojiCoder.decode(segment.segmentText), + state.tags, + ) + } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt index 1f0a9252e..685f72bff 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt @@ -64,7 +64,6 @@ import androidx.compose.ui.window.Popup import androidx.compose.ui.window.PopupProperties import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.emojicoder.EmojiCoder -import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState import com.vitorpamplona.amethyst.model.FeatureSetType import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.NoteState @@ -251,21 +250,23 @@ fun DisplaySecretEmojiAsReaction( accountViewModel: AccountViewModel, nav: INav, ) { - var secretContent by remember { - mutableStateOf(null) + var secretContent by remember(reaction) { + mutableStateOf(CachedRichTextParser.cachedText(EmojiCoder.decode(reaction), EmptyTagList)) } var showPopup by remember { mutableStateOf(false) } - LaunchedEffect(reaction) { - launch(Dispatchers.Default) { - secretContent = - CachedRichTextParser.parseText( - EmojiCoder.decode(reaction), - EmptyTagList, - ) + if (secretContent == null) { + LaunchedEffect(reaction) { + launch(Dispatchers.Default) { + secretContent = + CachedRichTextParser.parseText( + EmojiCoder.decode(reaction), + EmptyTagList, + ) + } } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UpdateReactionTypeDialog.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UpdateReactionTypeDialog.kt index d1e2162b0..93cbd2be3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UpdateReactionTypeDialog.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UpdateReactionTypeDialog.kt @@ -53,7 +53,6 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState @@ -79,13 +78,10 @@ import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.compose.viewModel import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.commons.emojicoder.EmojiCoder -import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.AddressableNote -import com.vitorpamplona.amethyst.service.CachedRichTextParser import com.vitorpamplona.amethyst.service.firstFullChar import com.vitorpamplona.amethyst.ui.components.AnimatedBorderTextCornerRadius -import com.vitorpamplona.amethyst.ui.components.CoreSecretMessage import com.vitorpamplona.amethyst.ui.components.InLineIconRenderer import com.vitorpamplona.amethyst.ui.components.SetDialogToEdgeToEdge import com.vitorpamplona.amethyst.ui.navigation.INav @@ -380,53 +376,6 @@ private fun RenderReactionOption( } } -@Composable -fun DisplaySecretEmoji( - text: String, - state: RichTextViewerState, - callbackUri: String?, - canPreview: Boolean, - quotesLeft: Int, - backgroundColor: MutableState, - accountViewModel: AccountViewModel, - nav: INav, -) { - if (canPreview && quotesLeft > 0) { - var secretContent by remember { - mutableStateOf(null) - } - - var showPopup by remember { - mutableStateOf(false) - } - - LaunchedEffect(text) { - launch(Dispatchers.Default) { - secretContent = - CachedRichTextParser.parseText( - EmojiCoder.decode(text), - state.tags, - ) - } - } - - val localSecretContent = secretContent - - AnimatedBorderTextCornerRadius( - text, - Modifier.clickable { - showPopup = !showPopup - }, - ) - - if (localSecretContent != null && showPopup) { - CoreSecretMessage(localSecretContent, callbackUri, quotesLeft, backgroundColor, accountViewModel, nav) - } - } else { - Text(text) - } -} - @Composable private fun EmojiSelector( accountViewModel: AccountViewModel,