weather-wear: temperatures and wind render in the watch's own units
- Commit
f2cba2fb559593707cb559c2584633676d16ab06- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 10:45 UTC (5 days ago)
- Parents
5744730
The protocol is canonical metric (temperatureC, windSpeedKmh) and the phone converts at render (UnitFormat + SystemUnits); the watch rendered verbatim, so under Fahrenheit/mph settings 20.0C / 36.0 km/h showed as '20° / 36 km/h' where the phone shows 68°F / 22 mph. Wind also truncated where the phone rounds. The formatting layer now converts at watch render time, driven by the watch's regional preferences (temperature from LocalePreferences — the phone's source — wind from locale measurement system incl. the ms-extension override). Six regression tests pin identity/conversion/rounding (20C->68, the -40 crossover, 10.9 km/h -> 11, 36 km/h -> 22 mph) and locale->wind-unit. The stateless locations content defaults to Celsius so previews stay deterministic. Daily alignment and snapshot plumbing were audited and cleared. Verified: :weather:wear:testDebugUnitTest 12/12 pass; lint 0 errors.
Diff
weather-wear/AGENTS.md
diff --git a/weather-wear/AGENTS.md b/weather-wear/AGENTS.mdindex 4fe07db1a4..bb28354fd2 100644--- a/weather-wear/AGENTS.md+++ b/weather-wear/AGENTS.md@@ -8,6 +8,7 @@ Wear OS companion of :weather. Same applicationId + signing (WearLink handshake `domain/WearWeatherProtocol.kt` is a copy of phone's file. Change both; golden tests pin same JSON. - Snapshot/watch-state: WearLink data items; request-sync: message watch->phone only. - Offline: renders locally stored snapshot; no network on watch.+- Units: protocol values stay Celsius / km/h; rendering converts to the watch's system regional preferences (`platform/WearSystemUnits`, formatting in `domain/WearWeatherFormatting`). ## Verify ./gradlew :weather:wear:assembleDev ./gradlew :weather:wear:testDebugUnitTest
weather-wear/build.gradle.kts
diff --git a/weather-wear/build.gradle.kts b/weather-wear/build.gradle.ktsindex 7abf7d7151..2e74661a8f 100644--- a/weather-wear/build.gradle.kts+++ b/weather-wear/build.gradle.kts@@ -12,6 +12,7 @@ android { } } dependencies {+ implementation(libs.androidx.core.ktx) implementation(libs.kotlinx.coroutines.android) implementation(project(":library:wearlink")) testImplementation(libs.kotlin.test)
weather-wear/src/main/java/com/vayunmathur/weather/wear/domain/WearWeatherFormatting.kt
diff --git a/weather-wear/src/main/java/com/vayunmathur/weather/wear/domain/WearWeatherFormatting.kt b/weather-wear/src/main/java/com/vayunmathur/weather/wear/domain/WearWeatherFormatting.ktindex 1ad94cc772..406a879cd9 100644--- a/weather-wear/src/main/java/com/vayunmathur/weather/wear/domain/WearWeatherFormatting.kt+++ b/weather-wear/src/main/java/com/vayunmathur/weather/wear/domain/WearWeatherFormatting.kt@@ -1,6 +1,39 @@ package com.vayunmathur.weather.wear.domain+import java.util.Locale import kotlin.math.roundToInt++/** Temperature display unit. The sync protocol always carries Celsius. */+enum class WearTemperatureUnit { Celsius, Fahrenheit }++/** Wind-speed display unit. The sync protocol always carries km/h. */+enum class WearWindUnit { KmH, Mph }+ fun wearConditionLabel(code:Int):String=when(code){0->"Clear";1->"Mostly clear";2->"Partly cloudy";3->"Cloudy";45,48->"Fog";51,53,55,56,57->"Drizzle";61,63,65,66,67,80,81,82->"Rain";71,73,75,77,85,86->"Snow";95,96,99->"Thunderstorm";else->"Cloudy"}-fun wearTemp(c:Double):String="${c.roundToInt()}°"++/** Formats a protocol Celsius value in [unit]; Fahrenheit converts, Celsius is the raw value. */+fun wearTemp(celsius:Double, unit:WearTemperatureUnit):String="${wearCelsiusTo(celsius, unit).roundToInt()}°"++fun wearCelsiusTo(celsius:Double, unit:WearTemperatureUnit):Double=when(unit){+ WearTemperatureUnit.Celsius->celsius+ WearTemperatureUnit.Fahrenheit->celsius*9.0/5.0+32.0+}++/** Formats a protocol km/h value in [unit], rounded like the phone (never truncated). */+fun wearWind(kmh:Double, unit:WearWindUnit):String=when(unit){+ WearWindUnit.KmH->"${kmh.roundToInt()} km/h"+ WearWindUnit.Mph->"${(kmh*0.621371).roundToInt()} mph"+}++/**+ * Imperial wind unit when the locale's measurement system is US/UK system+ * (Unicode `ms` extension), falling back to the predominantly-imperial+ * country codes when the extension is absent. Mirrors the phone's rule.+ */+fun wearWindUnitForLocale(locale:Locale):WearWindUnit{+ val extension=locale.getUnicodeLocaleType("ms")+ val imperial=if(extension!=null) extension=="ussystem"||extension=="uksystem" else locale.country in setOf("US","LR","MM")+ return if(imperial) WearWindUnit.Mph else WearWindUnit.KmH+}+ fun wearHourLabel(iso:String):String=iso.substringAfter("T","").take(5).ifBlank{"--:--"} fun wearDayLabel(iso:String):String=iso.take(10)
weather-wear/src/main/java/com/vayunmathur/weather/wear/platform/WearSystemUnits.kt
diff --git a/weather-wear/src/main/java/com/vayunmathur/weather/wear/platform/WearSystemUnits.kt b/weather-wear/src/main/java/com/vayunmathur/weather/wear/platform/WearSystemUnits.ktnew file mode 100644index 0000000000..79ab25c070--- /dev/null+++ b/weather-wear/src/main/java/com/vayunmathur/weather/wear/platform/WearSystemUnits.kt@@ -0,0 +1,20 @@+package com.vayunmathur.weather.wear.platform+import androidx.core.text.util.LocalePreferences+import com.vayunmathur.weather.wear.domain.WearTemperatureUnit+import com.vayunmathur.weather.wear.domain.WearWindUnit+import com.vayunmathur.weather.wear.domain.wearWindUnitForLocale+import java.util.Locale++/**+ * Display units from the watch's own regional preferences, mirroring the+ * phone's `SystemUnits`: temperature from [LocalePreferences] (Android 14+ * regional preferences, locale-inferred before that) and wind from the+ * locale measurement system. The protocol stays Celsius / km/h; only the+ * rendering converts.+ */+fun systemWearTemperatureUnit():WearTemperatureUnit=when(LocalePreferences.getTemperatureUnit()){+ LocalePreferences.TemperatureUnit.FAHRENHEIT->WearTemperatureUnit.Fahrenheit+ else->WearTemperatureUnit.Celsius+}++fun systemWearWindUnit():WearWindUnit=wearWindUnitForLocale(Locale.getDefault())
weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationScreen.kt
diff --git a/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationScreen.kt b/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationScreen.ktindex 6b49868944..8d3d12556d 100644--- a/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationScreen.kt+++ b/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationScreen.kt@@ -26,22 +26,26 @@ import com.vayunmathur.weather.wear.domain.wearConditionLabel import com.vayunmathur.weather.wear.domain.wearDayLabel import com.vayunmathur.weather.wear.domain.wearHourLabel import com.vayunmathur.weather.wear.domain.wearTemp+import com.vayunmathur.weather.wear.domain.wearWind+import com.vayunmathur.weather.wear.platform.systemWearTemperatureUnit+import com.vayunmathur.weather.wear.platform.systemWearWindUnit import com.vayunmathur.weather.wear.sync.requestWearSync import kotlinx.coroutines.launch @Composable fun WearLocationScreen(store:WearWeatherStore, locationId:Long, onBack:()->Unit){ val s by store.snapshot.collectAsState(); val loc=s.locations.firstOrNull{it.locationId==locationId}?:s.locations.firstOrNull() val ctx=LocalContext.current; val scope=rememberCoroutineScope(); var note by remember{mutableStateOf<String?>(null)}+ val tempUnit=remember{systemWearTemperatureUnit()}; val windUnit=remember{systemWearWindUnit()} val req=stringResource(R.string.wear_sync_requested); val no=stringResource(R.string.wear_no_phone); val st=rememberScalingLazyListState() ScalingLazyColumn(state=st){ if(loc==null){ item{ Text(stringResource(R.string.wear_empty),textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } } else{ item{ Text(loc.name.ifBlank{"Location"},style=MaterialTheme.typography.title3,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) }- loc.current?.let{c-> item{ Text("${wearTemp(c.temperatureC)} ${wearConditionLabel(c.weatherCode)}",style=MaterialTheme.typography.title2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) }- item{ Text("Feels ${wearTemp(c.apparentTemperatureC)} · ${c.humidityPercent}% · ${c.windSpeedKmh.toInt()} km/h",style=MaterialTheme.typography.body2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } }+ loc.current?.let{c-> item{ Text("${wearTemp(c.temperatureC,tempUnit)} ${wearConditionLabel(c.weatherCode)}",style=MaterialTheme.typography.title2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) }+ item{ Text("Feels ${wearTemp(c.apparentTemperatureC,tempUnit)} · ${c.humidityPercent}% · ${wearWind(c.windSpeedKmh,windUnit)}",style=MaterialTheme.typography.body2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } } if(loc.hourly.isNotEmpty()){ item{ Text(stringResource(R.string.wear_hourly),style=MaterialTheme.typography.caption1,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) }- items(loc.hourly){h-> Chip(onClick={}, enabled=false,label={Text("${wearHourLabel(h.timeIso)} ${wearTemp(h.temperatureC)}",maxLines=1)},secondaryLabel={Text("${wearConditionLabel(h.weatherCode)} ${h.precipitationProbabilityPercent}%",maxLines=1)},colors=ChipDefaults.secondaryChipColors(),modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp))} }+ items(loc.hourly){h-> Chip(onClick={}, enabled=false,label={Text("${wearHourLabel(h.timeIso)} ${wearTemp(h.temperatureC,tempUnit)}",maxLines=1)},secondaryLabel={Text("${wearConditionLabel(h.weatherCode)} ${h.precipitationProbabilityPercent}%",maxLines=1)},colors=ChipDefaults.secondaryChipColors(),modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp))} } if(loc.daily.isNotEmpty()){ item{ Text(stringResource(R.string.wear_daily),style=MaterialTheme.typography.caption1,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) }- items(loc.daily){d-> Chip(onClick={}, enabled=false,label={Text("${wearDayLabel(d.dateIso)} ${wearTemp(d.temperatureMinC)}–${wearTemp(d.temperatureMaxC)}",maxLines=1)},secondaryLabel={Text("${wearConditionLabel(d.weatherCode)} ${d.precipitationProbabilityMaxPercent}%",maxLines=1)},colors=ChipDefaults.secondaryChipColors(),modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp))} }+ items(loc.daily){d-> Chip(onClick={}, enabled=false,label={Text("${wearDayLabel(d.dateIso)} ${wearTemp(d.temperatureMinC,tempUnit)}–${wearTemp(d.temperatureMaxC,tempUnit)}",maxLines=1)},secondaryLabel={Text("${wearConditionLabel(d.weatherCode)} ${d.precipitationProbabilityMaxPercent}%",maxLines=1)},colors=ChipDefaults.secondaryChipColors(),modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp))} } } note?.let{n-> item{ Text(n,style=MaterialTheme.typography.body2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } } item{ Chip(onClick={scope.launch{note=if(requestWearSync(ctx))req else no}},label={Text(stringResource(R.string.wear_request_sync),maxLines=1)},colors=ChipDefaults.secondaryChipColors(),modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp)) }
weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationsScreen.kt
diff --git a/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationsScreen.kt b/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationsScreen.ktindex 85c2370007..56956b7af2 100644--- a/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationsScreen.kt+++ b/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearLocationsScreen.kt@@ -4,6 +4,7 @@ import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue+import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign@@ -17,11 +18,14 @@ import androidx.wear.compose.material.MaterialTheme import androidx.wear.compose.material.Text import com.vayunmathur.weather.wear.R import com.vayunmathur.weather.wear.data.WearWeatherStore+import com.vayunmathur.weather.wear.domain.WearTemperatureUnit import com.vayunmathur.weather.wear.domain.wearConditionLabel import com.vayunmathur.weather.wear.domain.wearTemp+import com.vayunmathur.weather.wear.platform.systemWearTemperatureUnit @Composable fun WearLocationsScreen(store:WearWeatherStore, onOpen:(Long)->Unit){ val s by store.snapshot.collectAsState()- WearLocationsContent(snapshot=s, onOpen=onOpen)+ val tempUnit=remember{systemWearTemperatureUnit()}+ WearLocationsContent(snapshot=s, onOpen=onOpen, tempUnit=tempUnit) } /**@@ -29,13 +33,13 @@ import com.vayunmathur.weather.wear.domain.wearTemp * parameter, so Compose previews (src/screenshotTest) render a literal fixture * with no store or phone. */-@Composable internal fun WearLocationsContent(snapshot:com.vayunmathur.weather.wear.domain.WearWeatherSnapshot, onOpen:(Long)->Unit){+@Composable internal fun WearLocationsContent(snapshot:com.vayunmathur.weather.wear.domain.WearWeatherSnapshot, onOpen:(Long)->Unit, tempUnit:WearTemperatureUnit=WearTemperatureUnit.Celsius){ val s=snapshot; val st=rememberScalingLazyListState() ScalingLazyColumn(state=st){ item{ Text(stringResource(R.string.app_name),style=MaterialTheme.typography.title3,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } if(s.locations.isEmpty()){ item{ Text(stringResource(R.string.wear_empty),style=MaterialTheme.typography.body2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } } else { items(s.locations,key={it.locationId}){ loc ->- Chip(onClick={onOpen(loc.locationId)}, label={Text(loc.name.ifBlank{"Location"},maxLines=1)}, secondaryLabel={Text(loc.current?.let{"${wearTemp(it.temperatureC)} ${wearConditionLabel(it.weatherCode)}"}?:stringResource(R.string.wear_locations),maxLines=1)}, colors=ChipDefaults.secondaryChipColors(), modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp))+ Chip(onClick={onOpen(loc.locationId)}, label={Text(loc.name.ifBlank{"Location"},maxLines=1)}, secondaryLabel={Text(loc.current?.let{"${wearTemp(it.temperatureC,tempUnit)} ${wearConditionLabel(it.weatherCode)}"}?:stringResource(R.string.wear_locations),maxLines=1)}, colors=ChipDefaults.secondaryChipColors(), modifier=Modifier.fillMaxWidth().padding(horizontal=8.dp)) } if(s.truncated) item{ Text(stringResource(R.string.wear_truncated),style=MaterialTheme.typography.body2,textAlign=TextAlign.Center,modifier=Modifier.fillMaxWidth().padding(horizontal=12.dp)) } }
weather-wear/src/test/java/com/vayunmathur/weather/wear/domain/WearWeatherFormattingTest.kt
diff --git a/weather-wear/src/test/java/com/vayunmathur/weather/wear/domain/WearWeatherFormattingTest.kt b/weather-wear/src/test/java/com/vayunmathur/weather/wear/domain/WearWeatherFormattingTest.ktnew file mode 100644index 0000000000..80fd921620--- /dev/null+++ b/weather-wear/src/test/java/com/vayunmathur/weather/wear/domain/WearWeatherFormattingTest.kt@@ -0,0 +1,18 @@+package com.vayunmathur.weather.wear.domain+import java.util.Locale+import kotlin.test.Test+import kotlin.test.assertEquals+class WearWeatherFormattingTest {+ // Regression: the protocol carries Celsius, but the watch rendered the raw+ // Celsius number regardless of the device temperature setting. Under+ // Fahrenheit settings 20.0C must render as 68, not 20.+ @Test fun temperatureCelsiusUnchanged(){ assertEquals("20°",wearTemp(20.0,WearTemperatureUnit.Celsius)); assertEquals("0°",wearTemp(0.0,WearTemperatureUnit.Celsius)); assertEquals("-40°",wearTemp(-40.0,WearTemperatureUnit.Celsius)) }+ @Test fun temperatureFahrenheitConverts(){ assertEquals("68°",wearTemp(20.0,WearTemperatureUnit.Fahrenheit)); assertEquals("32°",wearTemp(0.0,WearTemperatureUnit.Fahrenheit)); assertEquals("212°",wearTemp(100.0,WearTemperatureUnit.Fahrenheit)); assertEquals("-40°",wearTemp(-40.0,WearTemperatureUnit.Fahrenheit)); assertEquals("66°",wearTemp(19.0,WearTemperatureUnit.Fahrenheit)) }+ @Test fun temperatureRoundsAfterConversion(){ assertEquals("70°",wearTemp(21.0,WearTemperatureUnit.Fahrenheit)) }+ // Wind: protocol km/h rendered verbatim with a km/h label; under mph+ // settings 36 km/h must render as 22 mph, and values round (10.9 -> 11),+ // not truncate as the old toInt() rendering did.+ @Test fun windMetricUnchangedAndRounded(){ assertEquals("10 km/h",wearWind(10.0,WearWindUnit.KmH)); assertEquals("11 km/h",wearWind(10.9,WearWindUnit.KmH)) }+ @Test fun windImperialConverts(){ assertEquals("6 mph",wearWind(10.0,WearWindUnit.Mph)); assertEquals("22 mph",wearWind(36.0,WearWindUnit.Mph)) }+ @Test fun windUnitForLocale(){ assertEquals(WearWindUnit.Mph,wearWindUnitForLocale(Locale.US)); assertEquals(WearWindUnit.KmH,wearWindUnitForLocale(Locale.GERMANY)); assertEquals(WearWindUnit.Mph,wearWindUnitForLocale(Locale.Builder().setLanguage("en").setRegion("GB").setUnicodeLocaleKeyword("ms","uksystem").build())); assertEquals(WearWindUnit.KmH,wearWindUnitForLocale(Locale.Builder().setLanguage("en").setRegion("US").setUnicodeLocaleKeyword("ms","metric").build())) }+}