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() {
|
fun nextScreen() {
|
||||||
if (!canGoNext.value) return
|
if (!canGoNext.value) return
|
||||||
|
requiredScreens.firstOrNull()?.onLeave()
|
||||||
if (requiredScreens.size > 1) {
|
if (requiredScreens.size > 1) {
|
||||||
canGoNext.value = false
|
canGoNext.value = false
|
||||||
requiredScreens.removeFirst()
|
requiredScreens.removeFirst()
|
||||||
|
@ -25,6 +25,7 @@ abstract class SetupScreen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
open fun init() {}
|
open fun init() {}
|
||||||
|
open fun onLeave() {}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
abstract fun Content()
|
abstract fun Content()
|
||||||
|
@ -12,53 +12,69 @@ import androidx.compose.foundation.lazy.items
|
|||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.material.Surface
|
import androidx.compose.material.Surface
|
||||||
import androidx.compose.material.Text
|
import androidx.compose.material.Text
|
||||||
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedButton
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
|
||||||
import androidx.compose.ui.text.font.FontWeight
|
import androidx.compose.ui.text.font.FontWeight
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
import androidx.compose.ui.window.Dialog
|
import androidx.compose.ui.window.Dialog
|
||||||
import me.rhunk.snapenhance.bridge.wrapper.LocaleWrapper
|
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.setup.screens.SetupScreen
|
||||||
|
import me.rhunk.snapenhance.ui.util.ObservableMutableState
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
|
|
||||||
class PickLanguageScreen : SetupScreen(){
|
class PickLanguageScreen : SetupScreen(){
|
||||||
@Composable
|
private val availableLocales by lazy {
|
||||||
override fun Content() {
|
LocaleWrapper.fetchAvailableLocales(context.androidContext)
|
||||||
val androidContext = LocalContext.current
|
}
|
||||||
val availableLocales = remember { LocaleWrapper.fetchAvailableLocales(androidContext) }
|
|
||||||
|
|
||||||
allowNext(true)
|
private lateinit var selectedLocale: ObservableMutableState<String>
|
||||||
|
|
||||||
fun getLocaleDisplayName(locale: String): String {
|
private fun getLocaleDisplayName(locale: String): String {
|
||||||
locale.split("_").let {
|
locale.split("_").let {
|
||||||
return Locale(it[0], it[1]).getDisplayName(Locale.getDefault())
|
return Locale(it[0], it[1]).getDisplayName(Locale.getDefault())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val selectedLocale = remember {
|
private fun reloadTranslation(selectedLocale: String) {
|
||||||
val deviceLocale = Locale.getDefault().toString()
|
context.translation.reloadFromContext(context.androidContext, selectedLocale)
|
||||||
fun reloadTranslation(selectedLocale: String) {
|
}
|
||||||
context.translation.reloadFromContext(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(
|
ObservableMutableState(
|
||||||
defaultValue = availableLocales.firstOrNull {
|
defaultValue = availableLocales.firstOrNull {
|
||||||
locale -> locale == deviceLocale
|
locale -> locale == deviceLocale
|
||||||
} ?: LocaleWrapper.DEFAULT_LOCALE
|
} ?: LocaleWrapper.DEFAULT_LOCALE
|
||||||
) { _, newValue ->
|
) { _, newValue ->
|
||||||
context.config.locale = newValue
|
setLocale(newValue)
|
||||||
context.config.writeConfig()
|
|
||||||
reloadTranslation(newValue)
|
|
||||||
}.also { reloadTranslation(it.value) }
|
}.also { reloadTranslation(it.value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
override fun Content() {
|
||||||
|
allowNext(true)
|
||||||
|
|
||||||
DialogText(text = context.translation["setup.dialogs.select_language"])
|
DialogText(text = context.translation["setup.dialogs.select_language"])
|
||||||
|
|
||||||
@ -105,10 +121,11 @@ class PickLanguageScreen : SetupScreen(){
|
|||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
contentAlignment = Alignment.Center
|
contentAlignment = Alignment.Center
|
||||||
) {
|
) {
|
||||||
OutlinedButton(onClick = {
|
Button(onClick = {
|
||||||
isDialog.value = true
|
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
|
var userLocale = DEFAULT_LOCALE
|
||||||
|
|
||||||
private val translationMap = linkedMapOf<String, String>()
|
private val translationMap = linkedMapOf<String, String>()
|
||||||
private lateinit var _loadedLocaleString: String
|
|
||||||
|
|
||||||
val loadedLocale by lazy {
|
lateinit var loadedLocale: Locale
|
||||||
Locale(_loadedLocaleString.substring(0, 2), _loadedLocaleString.substring(3, 5))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun load(localePair: LocalePair) {
|
private fun load(localePair: LocalePair) {
|
||||||
if (!::_loadedLocaleString.isInitialized) {
|
loadedLocale = localePair.locale.let { Locale(it.substring(0, 2), it.substring(3, 5)) }
|
||||||
_loadedLocaleString = localePair.locale
|
|
||||||
}
|
|
||||||
|
|
||||||
val translations = JsonParser.parseString(localePair.content).asJsonObject
|
val translations = JsonParser.parseString(localePair.content).asJsonObject
|
||||||
if (translations == null || translations.isJsonNull) {
|
if (translations == null || translations.isJsonNull) {
|
||||||
|
Reference in New Issue
Block a user