Skip to content

Commit

Permalink
Add current locale to language picker
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeRedDev committed Jan 27, 2025
1 parent 75f1932 commit b692723
Showing 1 changed file with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pr0gramm.app.ui.dialogs

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -24,7 +25,7 @@ import androidx.compose.material.RadioButtonDefaults
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -46,19 +47,21 @@ class LanguagePickerDialog : BaseDialogFragment("LanguagePickerDialog") {
savedInstanceState: Bundle?
): View {
val supportedLocales = getSupportedLocales()
val currentLocale = getCurrentLocale(supportedLocales)

return ComposeView(requireContext()).apply {
setContent {
DialogContent(supportedLocales)
DialogContent(supportedLocales, currentLocale)
}
}
}

@Composable
fun DialogContent(
supportedLocales: List<Locale>,
initialLocale: Locale
) {
val checkedState = remember { mutableIntStateOf(-1) }
val selectedLocale = remember { mutableStateOf(initialLocale) }

Dialog(
onDismissRequest = { dismiss() }
Expand Down Expand Up @@ -89,12 +92,12 @@ class LanguagePickerDialog : BaseDialogFragment("LanguagePickerDialog") {
.align(Alignment.Start),
)
}
supportedLocales.forEachIndexed { idx, it ->
supportedLocales.forEach {
LanguageItem(
it,
selected = idx == checkedState.intValue,
selected = it == selectedLocale.value,
onClick = {
checkedState.intValue = idx
selectedLocale.value = it
},
)
}
Expand All @@ -114,19 +117,13 @@ class LanguagePickerDialog : BaseDialogFragment("LanguagePickerDialog") {
}
TextButton(
onClick = {
val stateIdx = checkedState.intValue
if (stateIdx != -1) {
val locale = supportedLocales[stateIdx]
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.create(
locale
)
AppCompatDelegate.setApplicationLocales(
LocaleListCompat.create(
selectedLocale.value
)
activity?.recreate()
}
)
dismiss()
},
enabled = checkedState.intValue != -1,
modifier = Modifier.padding(8.dp),
colors = textButtonColors(
contentColor = colorResource(R.color.orange_primary)
Expand Down Expand Up @@ -190,4 +187,31 @@ class LanguagePickerDialog : BaseDialogFragment("LanguagePickerDialog") {

return locales
}

private fun getCurrentLocale(supportedLocales: List<Locale>): Locale {
val applicationLocales = AppCompatDelegate.getApplicationLocales()
val appLocale = if (!applicationLocales.isEmpty) {
applicationLocales.get(0)!!
} else {
getCurrentAppLocale()
}

val candidate = supportedLocales.firstOrNull { it.isO3Language == appLocale.isO3Language }

if (candidate != null) {
return candidate
}

return Locale.forLanguageTag("en-US")
}

private fun getCurrentAppLocale(): Locale {
val config = requireContext().resources.configuration
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.locales[0]
} else {
@Suppress("DEPRECATION")
config.locale
}
}
}

0 comments on commit b692723

Please sign in to comment.