mirror of
https://github.com/rhunk/SnapEnhance.git
synced 2025-06-13 05:37:48 +02:00
fix: pick language screen save config
This commit is contained in:
@ -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()
|
||||
|
@ -25,6 +25,7 @@ abstract class SetupScreen {
|
||||
}
|
||||
|
||||
open fun init() {}
|
||||
open fun onLeave() {}
|
||||
|
||||
@Composable
|
||||
abstract fun Content()
|
||||
|
@ -12,53 +12,69 @@ 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 {
|
||||
locale.split("_").let {
|
||||
return Locale(it[0], it[1]).getDisplayName(Locale.getDefault())
|
||||
}
|
||||
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"])
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user