fix: pick language screen save config

This commit is contained in:
rhunk
2023-08-05 09:55:43 +02:00
parent 581b2df37e
commit cd4f3c29c4
4 changed files with 44 additions and 30 deletions

View File

@ -83,6 +83,7 @@ class SetupActivity : ComponentActivity() {
fun nextScreen() {
if (!canGoNext.value) return
requiredScreens.firstOrNull()?.onLeave()
if (requiredScreens.size > 1) {
canGoNext.value = false
requiredScreens.removeFirst()

View File

@ -25,6 +25,7 @@ abstract class SetupScreen {
}
open fun init() {}
open fun onLeave() {}
@Composable
abstract fun Content()

View File

@ -12,54 +12,70 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import me.rhunk.snapenhance.bridge.wrapper.LocaleWrapper
import me.rhunk.snapenhance.ui.util.ObservableMutableState
import me.rhunk.snapenhance.ui.setup.screens.SetupScreen
import me.rhunk.snapenhance.ui.util.ObservableMutableState
import java.util.Locale
class PickLanguageScreen : SetupScreen(){
@Composable
override fun Content() {
val androidContext = LocalContext.current
val availableLocales = remember { LocaleWrapper.fetchAvailableLocales(androidContext) }
private val availableLocales by lazy {
LocaleWrapper.fetchAvailableLocales(context.androidContext)
}
allowNext(true)
private lateinit var selectedLocale: ObservableMutableState<String>
fun getLocaleDisplayName(locale: String): String {
private fun getLocaleDisplayName(locale: String): String {
locale.split("_").let {
return Locale(it[0], it[1]).getDisplayName(Locale.getDefault())
}
}
val selectedLocale = remember {
val deviceLocale = Locale.getDefault().toString()
fun reloadTranslation(selectedLocale: String) {
context.translation.reloadFromContext(androidContext, selectedLocale)
private fun reloadTranslation(selectedLocale: String) {
context.translation.reloadFromContext(context.androidContext, selectedLocale)
}
private fun setLocale(locale: String) {
with(context) {
config.locale = locale
config.writeConfig()
translation.reloadFromContext(androidContext, locale)
reloadTranslation(locale)
}
}
override fun onLeave() {
context.config.locale = selectedLocale.value
context.config.writeConfig()
}
override fun init() {
val deviceLocale = Locale.getDefault().toString()
selectedLocale =
ObservableMutableState(
defaultValue = availableLocales.firstOrNull {
locale -> locale == deviceLocale
} ?: LocaleWrapper.DEFAULT_LOCALE
) { _, newValue ->
context.config.locale = newValue
context.config.writeConfig()
reloadTranslation(newValue)
setLocale(newValue)
}.also { reloadTranslation(it.value) }
}
@Composable
override fun Content() {
allowNext(true)
DialogText(text = context.translation["setup.dialogs.select_language"])
val isDialog = remember { mutableStateOf(false) }
@ -105,10 +121,11 @@ class PickLanguageScreen : SetupScreen(){
.fillMaxWidth(),
contentAlignment = Alignment.Center
) {
OutlinedButton(onClick = {
Button(onClick = {
isDialog.value = true
}) {
Text(text = getLocaleDisplayName(selectedLocale.value), fontSize = 16.sp, fontWeight = FontWeight.Light)
Text(text = getLocaleDisplayName(selectedLocale.value), fontSize = 16.sp,
fontWeight = FontWeight.Normal)
}
}
}

View File

@ -37,16 +37,11 @@ class LocaleWrapper {
var userLocale = DEFAULT_LOCALE
private val translationMap = linkedMapOf<String, String>()
private lateinit var _loadedLocaleString: String
val loadedLocale by lazy {
Locale(_loadedLocaleString.substring(0, 2), _loadedLocaleString.substring(3, 5))
}
lateinit var loadedLocale: Locale
private fun load(localePair: LocalePair) {
if (!::_loadedLocaleString.isInitialized) {
_loadedLocaleString = localePair.locale
}
loadedLocale = localePair.locale.let { Locale(it.substring(0, 2), it.substring(3, 5)) }
val translations = JsonParser.parseString(localePair.content).asJsonObject
if (translations == null || translations.isJsonNull) {