fix: locale change bug and sort tunnels alphabetically

This commit is contained in:
Zane Schepke 2024-12-07 22:34:45 -05:00
parent cda747deee
commit bb3b05d224
4 changed files with 26 additions and 35 deletions

View File

@ -3,8 +3,6 @@ package com.zaneschepke.wireguardautotunnel
import android.app.Application import android.app.Application
import android.os.StrictMode import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy import android.os.StrictMode.ThreadPolicy
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import com.zaneschepke.logcatter.LogReader import com.zaneschepke.logcatter.LogReader
import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository import com.zaneschepke.wireguardautotunnel.data.repository.AppStateRepository
import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository import com.zaneschepke.wireguardautotunnel.data.repository.SettingsRepository
@ -65,11 +63,8 @@ class WireGuardAutoTunnel : Application() {
if (!settingsRepository.getSettings().isKernelEnabled) { if (!settingsRepository.getSettings().isKernelEnabled) {
tunnelService.setBackendState(BackendState.SERVICE_ACTIVE, emptyList()) tunnelService.setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
} }
appStateRepository.getLocale()?.let { appStateRepository.getLocale()?.let {
val locale = LocaleUtil.getLocaleFromPrefCode(it) LocaleUtil.changeLocale(it)
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(locale)
AppCompatDelegate.setApplicationLocales(appLocale)
} }
} }
if (!isRunningOnTv()) { if (!isRunningOnTv()) {

View File

@ -1,7 +1,5 @@
package com.zaneschepke.wireguardautotunnel.ui package com.zaneschepke.wireguardautotunnel.ui
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.wireguard.android.backend.WgQuickBackend 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.ui.common.snackbar.SnackbarController
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil.OPTION_PHONE_LANGUAGE
import com.zaneschepke.wireguardautotunnel.util.StringValue import com.zaneschepke.wireguardautotunnel.util.StringValue
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
@ -159,11 +156,11 @@ constructor(
} }
fun onLocaleChange(localeTag: String) = viewModelScope.launch { fun onLocaleChange(localeTag: String) = viewModelScope.launch {
val locale = LocaleUtil.getLocaleFromPrefCode(localeTag) appDataRepository.appState.setLocale(localeTag)
val storageLocale = if (localeTag == OPTION_PHONE_LANGUAGE) OPTION_PHONE_LANGUAGE else locale LocaleUtil.changeLocale(localeTag)
appDataRepository.appState.setLocale(storageLocale) _configurationChange.update {
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(locale) true
AppCompatDelegate.setApplicationLocales(appLocale) }
} }
fun onToggleRestartAtBoot() = viewModelScope.launch { fun onToggleRestartAtBoot() = viewModelScope.launch {

View File

@ -35,6 +35,7 @@ import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.os.ConfigurationCompat
import androidx.hilt.navigation.compose.hiltViewModel import androidx.hilt.navigation.compose.hiltViewModel
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.data.domain.TunnelConfig 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.isRunningOnTv
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight import com.zaneschepke.wireguardautotunnel.util.extensions.scaledHeight
import java.text.Collator
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
@ -72,6 +74,13 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) } var selectedTunnel by remember { mutableStateOf<TunnelConfig?>(null) }
val isRunningOnTv = remember { context.isRunningOnTv() } 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<Unit> { viewModel.onToggleAutoTunnel() } val startAutoTunnel = withVpnPermission<Unit> { viewModel.onToggleAutoTunnel() }
val startTunnel = withVpnPermission<TunnelConfig> { val startTunnel = withVpnPermission<TunnelConfig> {
viewModel.onTunnelStart(it, uiState.settings.isKernelEnabled) viewModel.onTunnelStart(it, uiState.settings.isKernelEnabled)
@ -213,7 +222,7 @@ fun MainScreen(viewModel: MainViewModel = hiltViewModel(), uiState: AppUiState)
} }
} }
items( items(
uiState.tunnels, sortedTunnels,
key = { tunnel -> tunnel.id }, key = { tunnel -> tunnel.id },
) { tunnel -> ) { tunnel ->
val expanded = uiState.generalState.isTunnelStatsExpanded val expanded = uiState.generalState.isTunnelStatsExpanded

View File

@ -1,30 +1,20 @@
package com.zaneschepke.wireguardautotunnel.util package com.zaneschepke.wireguardautotunnel.util
import android.content.res.Resources import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.ConfigurationCompat import androidx.core.os.LocaleListCompat
import com.zaneschepke.wireguardautotunnel.BuildConfig import com.zaneschepke.wireguardautotunnel.BuildConfig
object LocaleUtil { object LocaleUtil {
private const val DEFAULT_LANG = "en"
val supportedLocales: Array<String> = BuildConfig.LANGUAGES val supportedLocales: Array<String> = BuildConfig.LANGUAGES
const val OPTION_PHONE_LANGUAGE = "sys_def" const val OPTION_PHONE_LANGUAGE = "sys_def"
/** fun changeLocale(locale: String) {
* returns the locale to use depending on the preference value if (locale == OPTION_PHONE_LANGUAGE) return resetToSystemLanguage()
* when preference value = "sys_def" returns the locale of current system val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(locale)
* else it returns the locale code e.g. "en", "bn" etc. AppCompatDelegate.setApplicationLocales(appLocale)
*/ }
fun getLocaleFromPrefCode(prefCode: String): String {
val localeCode = if (prefCode != OPTION_PHONE_LANGUAGE) { private fun resetToSystemLanguage() {
prefCode AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList())
} else {
val systemLang = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)?.language ?: DEFAULT_LANG
if (systemLang in supportedLocales) {
systemLang
} else {
DEFAULT_LANG
}
}
return localeCode
} }
} }