Caches translations to avoid blinking text every time the user comes back to an older screen.
This commit is contained in:
@@ -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<String, TranslationConfig>(100)
|
||||||
|
|
||||||
|
fun get(content: String): TranslationConfig = cache.get(content) ?: TranslationConfig(content, null, null, false)
|
||||||
|
|
||||||
|
fun set(
|
||||||
|
content: String,
|
||||||
|
config: TranslationConfig,
|
||||||
|
) {
|
||||||
|
cache.put(content, config)
|
||||||
|
}
|
||||||
|
}
|
||||||
+27
-12
@@ -54,6 +54,7 @@ import androidx.compose.ui.unit.dp
|
|||||||
import androidx.core.os.ConfigurationCompat
|
import androidx.core.os.ConfigurationCompat
|
||||||
import com.vitorpamplona.amethyst.R
|
import com.vitorpamplona.amethyst.R
|
||||||
import com.vitorpamplona.amethyst.service.lang.LanguageTranslatorService
|
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.actions.CrossfadeIfEnabled
|
||||||
import com.vitorpamplona.amethyst.ui.navigation.INav
|
import com.vitorpamplona.amethyst.ui.navigation.INav
|
||||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||||
@@ -80,18 +81,7 @@ fun TranslatableRichTextViewer(
|
|||||||
accountViewModel: AccountViewModel,
|
accountViewModel: AccountViewModel,
|
||||||
nav: INav,
|
nav: INav,
|
||||||
) {
|
) {
|
||||||
var translatedTextState by
|
var translatedTextState by translateAndWatchLanguageChanges(content, id, accountViewModel)
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CrossfadeIfEnabled(targetState = translatedTextState, accountViewModel = accountViewModel) {
|
CrossfadeIfEnabled(targetState = translatedTextState, accountViewModel = accountViewModel) {
|
||||||
RenderText(
|
RenderText(
|
||||||
@@ -333,6 +323,31 @@ private fun TranslationMessage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun translateAndWatchLanguageChanges(
|
||||||
|
content: String,
|
||||||
|
id: String,
|
||||||
|
accountViewModel: AccountViewModel,
|
||||||
|
): MutableState<TranslationConfig> {
|
||||||
|
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
|
@Composable
|
||||||
fun TranslateAndWatchLanguageChanges(
|
fun TranslateAndWatchLanguageChanges(
|
||||||
content: String,
|
content: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user