From d288010a83bb3c441c17ccfd75a1514142249564 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 1 Apr 2025 19:13:35 -0400 Subject: [PATCH] Move the stringresource setup out of the Activity --- .../com/vitorpamplona/amethyst/ui/MainActivity.kt | 7 ++----- .../amethyst/ui/StringResourceCache.kt | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt index 03f1230c4..98f3bc62a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/MainActivity.kt @@ -70,6 +70,8 @@ class MainActivity : AppCompatActivity() { Log.d("Lifetime Event", "MainActivity.onCreate") setContent { + StringResSetup() + val sharedPreferencesViewModel = prepareSharedViewModel() AmethystTheme(sharedPreferencesViewModel) { val accountStateViewModel: AccountStateViewModel = viewModel() @@ -87,11 +89,6 @@ class MainActivity : AppCompatActivity() { override fun onResume() { super.onResume() - val locales = this.applicationContext.resources.configuration.locales - if (!locales.isEmpty) { - checkLanguage(locales.get(0).language) - } - Log.d("Lifetime Event", "MainActivity.onResume") // starts muted every time diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/StringResourceCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/StringResourceCache.kt index f4205b7f3..91719a61f 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/StringResourceCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/StringResourceCache.kt @@ -25,6 +25,7 @@ import android.util.LruCache import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.res.stringResource +import androidx.lifecycle.compose.LifecycleResumeEffect /** * Cache for stringResource because it seems to be > 1ms function in some phones @@ -43,6 +44,19 @@ fun checkLanguage(currentLanguage: String) { } } +@Composable +fun StringResSetup() { + val config = LocalConfiguration.current + if (!config.locales.isEmpty) { + val language = config.locales.get(0).language + LifecycleResumeEffect(language) { + checkLanguage(language) + + onPauseOrDispose { } + } + } +} + @Composable fun stringRes(id: Int): String = resourceCache.get(id) ?: stringResource(id).also { resourceCache.put(id, it) }