Skip to content

Commit

Permalink
More clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Aug 6, 2024
1 parent 28f7e20 commit ec142b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -48,7 +45,6 @@ class MainActivity : ComponentActivity(), AndroidScopeComponent {
private val serviceConnectionManager by inject<ServiceConnectionManager>()
private val splashCompleteRepository by inject<SplashCompleteRepository>()
private val managementService by inject<ManagementService>()
private val localeRepository by inject<LocaleRepository>()

private var isReadyNextDraw: Boolean = false

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Locale?> = _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") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>

class RelayLocationTranslationRepository(
val context: Context,
val localeRepository: LocaleRepository,
scope: CoroutineScope
externalScpoe: CoroutineScope,
val dispatcher: CoroutineDispatcher = Dispatchers.IO
) {
val translations = MutableStateFlow<Map<String, String>>(mapOf())

val translations: StateFlow<Translations> =
localeRepository.currentLocale
.map { loadTranslations(it) }
.stateIn(externalScpoe, SharingStarted.Eagerly, emptyMap())

private val defaultTranslation: Map<String, String>

Expand All @@ -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<String, String> {
val translation = mutableMapOf<String, String>()
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()
Expand Down

0 comments on commit ec142b1

Please sign in to comment.