update language selection logic to support system locale and refactor number formatting to use localized locales

This commit is contained in:
PranshulGG committed 2026-06-10 10:25:11 +05:30
1 parent 96ac827746
commit 63aa564086
6 files changed
+38 -13

No files matched your search

@@ -89,8 +89,8 @@ fun getAppLocalLocales(): List<Language> {
)
}
fun getCurrentAppLocale(): Locale? {
fun getCurrentAppLocale(): Locale {
val locale = AppCompatDelegate.getApplicationLocales()[0]
return locale
return locale ?: Locale.getDefault()
}
@@ -29,15 +29,18 @@ import com.pranshulgg.weather_master_app.core.ui.components.Gap
import com.pranshulgg.weather_master_app.core.ui.components.LargeTopBarScaffold
import com.pranshulgg.weather_master_app.core.ui.components.NavigateUpBtn
import com.pranshulgg.weather_master_app.core.ui.components.Symbol
import com.pranshulgg.weather_master_app.core.utils.formatters.formatLocalizedNumber
import com.pranshulgg.weather_master_app.core.utils.formatters.to12HourTimeString
import com.pranshulgg.weather_master_app.core.utils.formatters.to24HourTimeString
import com.pranshulgg.weather_master_app.core.utils.formatters.toDateString
import com.pranshulgg.weather_master_app.core.utils.locale.getCurrentAppLocale
import com.pranshulgg.weather_master_app.core.utils.weather.forecast.findMatchingHourly
import com.pranshulgg.weather_master_app.feature.blocks.BlocksScreenViewModel
import com.pranshulgg.weather_master_app.feature.blocks.components.AboutCard
import com.pranshulgg.weather_master_app.feature.blocks.components.AboutCardText
import com.pranshulgg.weather_master_app.feature.blocks.components.MatBarChart
import com.pranshulgg.weather_master_app.feature.blocks.components.NoHourlyDataAvailable
import java.util.Locale
import kotlin.math.max
import kotlin.math.roundToInt
@@ -150,9 +153,8 @@ private fun BarChart(
max((valuePercentage * 170).roundToInt(), 48)
}
val formatter: (Double) -> String = {
val value = PressureUnit.HPA.convert(it, unit)
if (unit != PressureUnit.HPA) "%.2f".format(value) else "${value?.roundToInt()}"
val formatter: (Double) -> Double? = {
PressureUnit.HPA.convert(it, unit)
}
val steps = 7
@@ -189,10 +191,14 @@ private fun BarChart(
Text(time, style = MaterialTheme.typography.labelMedium)
}
},
sideValues = sideValues.map { formatter(it.toDouble()).toDouble().roundToInt() },
sideValues = sideValues.map { formatter(it.toDouble())?.roundToInt()!! },
values = values,
barHeights = barHeights,
headerValue = formatter(values.average()),
headerValue = formatLocalizedNumber(
locale = getCurrentAppLocale(),
number = formatter(values.average())!!,
decimalPlaces = 1
),
headerSuffix = unit.toName(inShort = true, context),
barColor = barColor,
chartHeight = 220.dp
@@ -65,7 +65,6 @@ class LocationsScreenViewModel @Inject constructor(
_uiState.value = _uiState.value.copy(isDeviceLocationLoading = true)
viewModelScope.launch {
try {
Log.d("COOL HERE", "CALLED HERE HERE")
locationsRepo.saveDeviceLocation()
} catch (e: Exception) {
if (e is CancellationException) throw e
@@ -1,5 +1,6 @@
package com.pranshulgg.weather_master_app.feature.settings.language
import android.util.Log
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -21,6 +22,7 @@ import androidx.compose.ui.unit.dp
import androidx.core.os.LocaleListCompat
import androidx.navigation.NavController
import com.pranshulgg.weather_master_app.R
import com.pranshulgg.weather_master_app.core.prefs.helper.PreferencesHelper
import com.pranshulgg.weather_master_app.core.ui.components.AvatarCheck
import com.pranshulgg.weather_master_app.core.ui.components.AvatarMonogram
import com.pranshulgg.weather_master_app.core.ui.components.Gap
@@ -30,6 +32,7 @@ import com.pranshulgg.weather_master_app.core.ui.components.SettingSection
import com.pranshulgg.weather_master_app.core.ui.components.SettingTile
import com.pranshulgg.weather_master_app.core.utils.locale.getAppLocalLocales
import com.pranshulgg.weather_master_app.core.utils.locale.getCurrentAppLocale
import java.util.Locale
@Composable
@@ -42,10 +45,12 @@ fun LanguageScreen(navController: NavController) {
val currentAppLocale =
remember {
mutableStateOf(
getCurrentAppLocale()?.toLanguageTag() ?: "sys"
getCurrentAppLocale().toLanguageTag()
)
}
val isSystemLanguage = PreferencesHelper.getBool("isSystemLanguage") ?: true
LargeTopBarScaffold(
title = stringResource(R.string.setting_language),
@@ -73,7 +78,8 @@ fun LanguageScreen(navController: NavController) {
SettingSection(
tiles = languageList.map {
val selected = it.value == currentAppLocale.value
val selected =
if (isSystemLanguage) it.value == "sys" else it.value == currentAppLocale.value
SettingTile.ActionTile(
leading = {
if (selected) AvatarCheck(
@@ -89,7 +95,12 @@ fun LanguageScreen(navController: NavController) {
selected = selected,
onClick = {
currentAppLocale.value = it.value
setLanguage(it.value)
if (it.value == "sys") {
setSystemLanguage()
} else {
setLanguage(it.value)
}
}
)
}
@@ -103,8 +114,10 @@ fun LanguageScreen(navController: NavController) {
private fun setLanguage(languageCode: String) {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags(languageCode))
PreferencesHelper.setBool("isSystemLanguage", false)
}
private fun setSystemLanguage() {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList())
PreferencesHelper.setBool("isSystemLanguage", true)
}
@@ -32,6 +32,9 @@ import com.pranshulgg.weather_master_app.core.ui.components.Gap
import com.pranshulgg.weather_master_app.core.ui.components.Symbol
import com.pranshulgg.weather_master_app.core.ui.theme.ShadowElevation
import com.pranshulgg.weather_master_app.core.ui.theme.ShapeRadius
import com.pranshulgg.weather_master_app.core.utils.formatters.formatLocalizedNumber
import com.pranshulgg.weather_master_app.core.utils.locale.getCurrentAppLocale
import kotlin.collections.average
import kotlin.math.roundToInt
@Composable
@@ -88,7 +91,11 @@ fun PressureBlock(
Header()
Text(
if (units.pressureUnit != PressureUnit.HPA) "%.2f".format(pressureConverted) else "$pressureHpa",
formatLocalizedNumber(
locale = getCurrentAppLocale(),
number = pressureConverted!!,
decimalPlaces = 1
),
style = MaterialTheme.typography.displaySmall,
color = MaterialTheme.colorScheme.onSurface,
)
@@ -80,7 +80,7 @@ fun VisibilityBlock(
formatLocalizedNumber(
number = visibility!!,
decimalPlaces = 0,
locale = getCurrentAppLocale() ?: Locale.US
locale = getCurrentAppLocale()
),
style = MaterialTheme.typography.displaySmall,
color = MaterialTheme.colorScheme.onSurface,