diff --git a/amethyst/src/play/java/com/vitorpamplona/amethyst/service/lang/TranslationsCache.kt b/amethyst/src/play/java/com/vitorpamplona/amethyst/service/lang/TranslationsCache.kt new file mode 100644 index 000000000..0bf351c8f --- /dev/null +++ b/amethyst/src/play/java/com/vitorpamplona/amethyst/service/lang/TranslationsCache.kt @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.lang + +import android.util.LruCache +import com.vitorpamplona.amethyst.ui.components.TranslationConfig + +object TranslationsCache { + val cache = LruCache(100) + + fun get(content: String): TranslationConfig = cache.get(content) ?: TranslationConfig(content, null, null, false) + + fun set( + content: String, + config: TranslationConfig, + ) { + cache.put(content, config) + } +} diff --git a/amethyst/src/play/java/com/vitorpamplona/amethyst/ui/components/TranslatableRichTextViewer.kt b/amethyst/src/play/java/com/vitorpamplona/amethyst/ui/components/TranslatableRichTextViewer.kt index 4dc9b42f6..2f52b4cf5 100644 --- a/amethyst/src/play/java/com/vitorpamplona/amethyst/ui/components/TranslatableRichTextViewer.kt +++ b/amethyst/src/play/java/com/vitorpamplona/amethyst/ui/components/TranslatableRichTextViewer.kt @@ -54,6 +54,7 @@ import androidx.compose.ui.unit.dp import androidx.core.os.ConfigurationCompat import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.service.lang.LanguageTranslatorService +import com.vitorpamplona.amethyst.service.lang.TranslationsCache import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled import com.vitorpamplona.amethyst.ui.navigation.INav import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel @@ -80,18 +81,7 @@ fun TranslatableRichTextViewer( accountViewModel: AccountViewModel, nav: INav, ) { - var translatedTextState by - remember(id) { mutableStateOf(TranslationConfig(content, null, null, false)) } - - TranslateAndWatchLanguageChanges(content, accountViewModel) { result -> - if ( - !translatedTextState.result.equals(result.result, true) || - translatedTextState.sourceLang != result.sourceLang || - translatedTextState.targetLang != result.targetLang - ) { - translatedTextState = result - } - } + var translatedTextState by translateAndWatchLanguageChanges(content, id, accountViewModel) CrossfadeIfEnabled(targetState = translatedTextState, accountViewModel = accountViewModel) { RenderText( @@ -333,6 +323,31 @@ private fun TranslationMessage( } } +@Composable +fun translateAndWatchLanguageChanges( + content: String, + id: String, + accountViewModel: AccountViewModel, +): MutableState { + var translatedTextState = remember(id) { mutableStateOf(TranslationsCache.get(content)) } + + TranslateAndWatchLanguageChanges( + content, + accountViewModel, + ) { result -> + if ( + !translatedTextState.value.result.equals(result.result, true) || + translatedTextState.value.sourceLang != result.sourceLang || + translatedTextState.value.targetLang != result.targetLang + ) { + TranslationsCache.set(content, result) + translatedTextState.value = result + } + } + + return translatedTextState +} + @Composable fun TranslateAndWatchLanguageChanges( content: String,