From ec142b18090342c4850ad02ec6941f760a6fe498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Wed, 7 Aug 2024 00:50:51 +0200 Subject: [PATCH] More clean up --- .../net/mullvad/mullvadvpn/ui/MainActivity.kt | 10 ---- .../mullvadvpn/lib/shared/LocaleRepository.kt | 6 ++- .../RelayLocationTranslationRepository.kt | 46 +++++++++++-------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt index 0a778a2d6ca1..a4ae13113902 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/MainActivity.kt @@ -10,7 +10,6 @@ import androidx.core.view.WindowCompat import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle -import co.touchlab.kermit.Logger import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch @@ -21,8 +20,6 @@ import net.mullvad.mullvadvpn.lib.common.util.SdkUtils.requestNotificationPermis import net.mullvad.mullvadvpn.lib.daemon.grpc.GrpcConnectivityState import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService import net.mullvad.mullvadvpn.lib.intent.IntentProvider -import net.mullvad.mullvadvpn.lib.shared.LocaleRepository -import net.mullvad.mullvadvpn.lib.shared.RelayLocationTranslationRepository import net.mullvad.mullvadvpn.lib.theme.AppTheme import net.mullvad.mullvadvpn.repository.PrivacyDisclaimerRepository import net.mullvad.mullvadvpn.repository.SplashCompleteRepository @@ -48,7 +45,6 @@ class MainActivity : ComponentActivity(), AndroidScopeComponent { private val serviceConnectionManager by inject() private val splashCompleteRepository by inject() private val managementService by inject() - private val localeRepository by inject() private var isReadyNextDraw: Boolean = false @@ -67,12 +63,6 @@ class MainActivity : ComponentActivity(), AndroidScopeComponent { } super.onCreate(savedInstanceState) - lifecycleScope.launch { - RelayLocationTranslationRepository(this@MainActivity, localeRepository, this) - .translations - .collect { Logger.d("Relay location translation table updated: $it") } - localeRepository.currentLocale.collect { Logger.d("Locale changed to: $it") } - } // Needs to be before set content since we want to access the intent in compose if (savedInstanceState == null) { intentProvider.setStartIntent(intent) diff --git a/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/LocaleRepository.kt b/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/LocaleRepository.kt index fed72c063a77..8d952ec112b9 100644 --- a/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/LocaleRepository.kt +++ b/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/LocaleRepository.kt @@ -5,14 +5,16 @@ import co.touchlab.kermit.Logger import java.util.Locale import kotlin.also import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow class LocaleRepository(val resources: Resources) { - val currentLocale = MutableStateFlow(getLocale()) + private val _currentLocale = MutableStateFlow(getLocale()) + val currentLocale: StateFlow = _currentLocale private fun getLocale(): Locale? = resources.configuration.locales.get(0) fun refreshLocale() { Logger.d("AppLang: Refreshing locale") - currentLocale.value = getLocale().also { Logger.d("AppLang: New locale: $it") } + _currentLocale.value = getLocale().also { Logger.d("AppLang: New locale: $it") } } } diff --git a/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt b/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt index b210ee8c2dca..6ae6b1828ba8 100644 --- a/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt +++ b/android/lib/shared/src/main/kotlin/net/mullvad/mullvadvpn/lib/shared/RelayLocationTranslationRepository.kt @@ -10,16 +10,28 @@ import kotlin.collections.associate import kotlin.collections.set import kotlin.collections.toMap import kotlin.to +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.launch +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.withContext + +typealias Translations = Map class RelayLocationTranslationRepository( val context: Context, val localeRepository: LocaleRepository, - scope: CoroutineScope + externalScpoe: CoroutineScope, + val dispatcher: CoroutineDispatcher = Dispatchers.IO ) { - val translations = MutableStateFlow>(mapOf()) + + val translations: StateFlow = + localeRepository.currentLocale + .map { loadTranslations(it) } + .stateIn(externalScpoe, SharingStarted.Eagerly, emptyMap()) private val defaultTranslation: Map @@ -28,33 +40,31 @@ class RelayLocationTranslationRepository( val confContext = context.createConfigurationContext(defaultConfiguration) val defaultTranslationXml = confContext.resources.getXml(R.xml.relay_locations) defaultTranslation = loadRelayTranslation(defaultTranslationXml) - Logger.d("AppLang: Default translation = $defaultTranslation") - - scope.launch { localeRepository.currentLocale.collect { dodo(it) } } } - private fun dodo(locale: Locale?) { - Logger.d("AppLang: Updating based on current locale to $locale") - if (locale == null || locale.language == DEFAULT_LANGUAGE) translations.value = emptyMap() - else { - // Load current translations - val xml = context.resources.getXml(R.xml.relay_locations) - val translation = loadRelayTranslation(xml) + private suspend fun loadTranslations(locale: Locale?): Translations = + withContext(dispatcher) { + Logger.d( + "AppLang ${this@RelayLocationTranslationRepository}: Updating based on current locale to $locale" + ) + if (locale == null || locale.language == DEFAULT_LANGUAGE) emptyMap() + else { + // Load current translations + val xml = context.resources.getXml(R.xml.relay_locations) + val translation = loadRelayTranslation(xml) - translations.value = translation.entries .associate { (id, name) -> defaultTranslation[id]!! to name } .also { Logger.d("AppLang: New translationTable: $it") } + } } - } private fun loadRelayTranslation(xml: XmlResourceParser): Map { val translation = mutableMapOf() while (xml.eventType != XmlResourceParser.END_DOCUMENT) { if (xml.eventType == XmlResourceParser.START_TAG && xml.name == "string") { val key = xml.getAttributeValue(null, "name") - xml.next() - val value = xml.text + val value = xml.nextText() translation[key] = value } xml.next()