diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/WireGuardAutoTunnel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/WireGuardAutoTunnel.kt index e038990..37633fa 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/WireGuardAutoTunnel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/WireGuardAutoTunnel.kt @@ -3,8 +3,6 @@ package com.zaneschepke.wireguardautotunnel import android.app.Application import android.os.StrictMode import android.os.StrictMode.ThreadPolicy -import androidx.appcompat.app.AppCompatDelegate -import androidx.core.os.LocaleListCompat import com.zaneschepke.logcatter.LogReader import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository @@ -65,11 +63,8 @@ class WireGuardAutoTunnel : Application() { if (!settingsRepository.getSettings().isKernelEnabled) { tunnelService.setBackendState(BackendState.SERVICE_ACTIVE, emptyList()) } - appStateRepository.getLocale()?.let { - val locale = LocaleUtil.getLocaleFromPrefCode(it) - val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(locale) - AppCompatDelegate.setApplicationLocales(appLocale) + LocaleUtil.changeLocale(it) } } if (!isRunningOnTv()) { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt index 5b65a1d..e34b0e9 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/AppViewModel.kt @@ -1,7 +1,5 @@ package com.zaneschepke.wireguardautotunnel.ui -import androidx.appcompat.app.AppCompatDelegate -import androidx.core.os.LocaleListCompat import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.wireguard.android.backend.WgQuickBackend @@ -20,7 +18,6 @@ import com.zaneschepke.wireguardautotunnel.service.tunnel.TunnelState import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.SnackbarController import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.LocaleUtil -import com.zaneschepke.wireguardautotunnel.util.LocaleUtil.OPTION_PHONE_LANGUAGE import com.zaneschepke.wireguardautotunnel.util.StringValue import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.CoroutineDispatcher @@ -159,11 +156,11 @@ constructor( } fun onLocaleChange(localeTag: String) = viewModelScope.launch { - val locale = LocaleUtil.getLocaleFromPrefCode(localeTag) - val storageLocale = if (localeTag == OPTION_PHONE_LANGUAGE) OPTION_PHONE_LANGUAGE else locale - appDataRepository.appState.setLocale(storageLocale) - val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(locale) - AppCompatDelegate.setApplicationLocales(appLocale) + appDataRepository.appState.setLocale(localeTag) + LocaleUtil.changeLocale(localeTag) + _configurationChange.update { + true + } } fun onToggleRestartAtBoot() = viewModelScope.launch { diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt index 5e24bf1..678c2c3 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainScreen.kt @@ -35,6 +35,7 @@ import androidx.compose.ui.platform.LocalClipboardManager import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp +import androidx.core.os.ConfigurationCompat import androidx.hilt.navigation.compose.hiltViewModel import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig @@ -57,6 +58,7 @@ import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight +import java.text.Collator @OptIn(ExperimentalFoundationApi::class) @Composable @@ -72,6 +74,13 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) var selectedTunnel by remember { mutableStateOf(null) } val isRunningOnTv = remember { context.isRunningOnTv() } + val currentLocale = ConfigurationCompat.getLocales(context.resources.configuration)[0] + val collator = Collator.getInstance(currentLocale) + + val sortedTunnels = remember(uiState.tunnels) { + uiState.tunnels.sortedWith(compareBy(collator) { it.name }) + } + val startAutoTunnel = withVpnPermission { viewModel.onToggleAutoTunnel() } val startTunnel = withVpnPermission { viewModel.onTunnelStart(it, uiState.settings.isKernelEnabled) @@ -213,7 +222,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState) } } items( - uiState.tunnels, + sortedTunnels, key = { tunnel -> tunnel.id }, ) { tunnel -> val expanded = uiState.generalState.isTunnelStatsExpanded diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/LocaleUtil.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/LocaleUtil.kt index 22d433e..b9e2d3a 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/LocaleUtil.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/LocaleUtil.kt @@ -1,30 +1,20 @@ package com.zaneschepke.wireguardautotunnel.util -import android.content.res.Resources -import androidx.core.os.ConfigurationCompat +import androidx.appcompat.app.AppCompatDelegate +import androidx.core.os.LocaleListCompat import com.zaneschepke.wireguardautotunnel.BuildConfig object LocaleUtil { - private const val DEFAULT_LANG = "en" val supportedLocales: Array = BuildConfig.LANGUAGES const val OPTION_PHONE_LANGUAGE = "sys_def" - /** - * returns the locale to use depending on the preference value - * when preference value = "sys_def" returns the locale of current system - * else it returns the locale code e.g. "en", "bn" etc. - */ - fun getLocaleFromPrefCode(prefCode: String): String { - val localeCode = if (prefCode != OPTION_PHONE_LANGUAGE) { - prefCode - } else { - val systemLang = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)?.language ?: DEFAULT_LANG - if (systemLang in supportedLocales) { - systemLang - } else { - DEFAULT_LANG - } - } - return localeCode + fun changeLocale(locale: String) { + if (locale == OPTION_PHONE_LANGUAGE) return resetToSystemLanguage() + val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(locale) + AppCompatDelegate.setApplicationLocales(appLocale) + } + + private fun resetToSystemLanguage() { + AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList()) } }