weather: add Set units button in locations panel that opens OS regional units settings
- Commit
0747e1b2826012c772a9bf9293b8055603c1f9b3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-08 17:39 UTC (4 months ago)
- Parents
0c1ebc0
Diff
weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.ktindex ae0205a6e7..fbc5c3a2ca 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt@@ -1,7 +1,11 @@ package com.vayunmathur.weather.ui import android.Manifest+import android.content.ActivityNotFoundException+import android.content.Context+import android.content.Intent import android.content.res.Resources+import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.clickable@@ -30,6 +34,7 @@ import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.IconBack import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.ui.IconDragHandle+import com.vayunmathur.library.ui.IconRuler import com.vayunmathur.library.ui.IconSearch import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.ListItem@@ -255,6 +260,12 @@ fun LocationsScreen( IconBack(tint = MaterialTheme.colorScheme.onSurface) } },+ actions = {+ val context = LocalContext.current+ IconButton(onClick = { openRegionalUnitsSettings(context) }) {+ IconRuler(tint = MaterialTheme.colorScheme.onSurface)+ }+ }, ) }, bottomBar = {@@ -368,6 +379,30 @@ fun LocationsScreen( } } +/**+ * Opens the OS regional-units settings so the user can pick temperature/wind/etc. units.+ * The app itself stores no units config; it reads the system regional preferences (see+ * [com.vayunmathur.weather.util.rememberTempUnit]). Falls back to the general locale settings+ * on devices without a dedicated regional-preferences screen (pre-Android 14).+ */+private fun openRegionalUnitsSettings(context: Context) {+ // Value of Settings.ACTION_REGIONAL_PREFERENCES_SETTINGS (API 34); used as a literal so the+ // deep link still compiles/works when built against lower compile SDKs.+ val actions = listOf(+ "android.settings.REGIONAL_PREFERENCES_SETTINGS",+ Settings.ACTION_LOCALE_SETTINGS,+ Settings.ACTION_SETTINGS,+ )+ for (action in actions) {+ try {+ context.startActivity(Intent(action).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))+ return+ } catch (_: ActivityNotFoundException) {+ // Try the next, broader settings target.+ }+ }+}+ /** Format a "X ago" delta from [nowMs] to the given epoch ms. */ private fun formatAgo(resources: Resources, epochMs: Long, nowMs: Long = System.currentTimeMillis()): String { val deltaSec = ((nowMs - epochMs) / 1000L).coerceAtLeast(0L)
weather/src/main/res/values/strings.xml
diff --git a/weather/src/main/res/values/strings.xml b/weather/src/main/res/values/strings.xmlindex b4048914f9..d1aa7a842f 100644--- a/weather/src/main/res/values/strings.xml+++ b/weather/src/main/res/values/strings.xml@@ -24,6 +24,8 @@ <!-- Section header title for locations drawer sheet. Noun plural --> <string name="locations">Locations</string>+ <!-- Locations panel action: opens the OS regional units settings. Verb phrase -->+ <string name="set_units">Set units</string> <!-- Search dialog title and bottom bar button to open search. Verb phrase --> <string name="search_location">Search location</string> <!-- TextField placeholder/hint for entering city name. Noun phrase -->