weather: add Wear Tile for current conditions
- Commit
6a1f0e96f91460d9b38f2c9767d080e23058bda8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-06 03:47 UTC (9 hours ago)
- Parents
143b6d8
First per-app Wear Tile on the shared :library:weartiles seam. WeatherTileService (in service/, where the PackageStructure lint expects services rather than widget/ Glance files) renders the primary location's current temperature and condition from WearWeatherStore — the conduit-synced wear snapshot already in app-private storage — so the tile renders offline from cache alone like the phone Glance widgets. Tap launches the watch entry (WearMainActivity). Refresh follows Tiles conventions: WearWeatherStore requests a tile update the moment a fresh snapshot lands, and the timeline carries a 15-minute freshness interval otherwise. The manifest declares the BIND_TILE_PROVIDER service in this app (tiles belong to each app; the Watch conduit app owns no other app's tile) with the launcher icon as preview. The snapshot-to-tile mapping is a pure function (weatherTileSnapshotFor) covered by WeatherTileServiceTest, which also asserts the ProtoLayout proto carries the rendered temperature; the preview snapshot supplies the picker sample. Verified: :weather:testDevUnitTest, :weather:lint and :weather:lintVitalRelease green in the worktree.
Diff
weather/AGENTS.md
diff --git a/weather/AGENTS.md b/weather/AGENTS.mdindex 0826599449..4f5c006d3b 100644--- a/weather/AGENTS.md+++ b/weather/AGENTS.md@@ -153,3 +153,4 @@ receivers are a phone-only surface and early-return on a watch. ./gradlew :weather:lint ./gradlew :weather:checkMetadata ```+- Wear Tile: `service/WeatherTileService` (a `MaTileService` in `:library:weartiles`) renders the primary location's current temperature/condition from `WearWeatherStore`; the store requests a tile update on every new snapshot, plus 15-minute freshness. Tap opens `WearMainActivity`.
weather/build.gradle.kts
diff --git a/weather/build.gradle.kts b/weather/build.gradle.ktsindex 5144cc7ed8..02c2338ff3 100644--- a/weather/build.gradle.kts+++ b/weather/build.gradle.kts@@ -109,6 +109,7 @@ dependencies { // sync/WearWeatherSync publishes the phone snapshot, sync/WearLinkBridge // receives it on a watch. FormFactor picks the side at runtime. implementation(project(":library:wearlink"))+ implementation(project(":library:weartiles")) // Watch experience (ui/Wear*): Wear Compose + the repo log facade the // watch platform/sync code writes through. implementation(libs.androidx.wear.compose.material)
weather/src/main/AndroidManifest.xml
diff --git a/weather/src/main/AndroidManifest.xml b/weather/src/main/AndroidManifest.xmlindex ff350c3b5d..ab2559a162 100644--- a/weather/src/main/AndroidManifest.xml+++ b/weather/src/main/AndroidManifest.xml@@ -148,5 +148,19 @@ <action android:name="com.vayunmathur.watch.action.SYNC_TRIGGER" /> </intent-filter> </receiver>++ <!-- Wear OS Tile (watch-home card): renders from the wear snapshot store. -->+ <service+ android:name=".service.WeatherTileService"+ android:exported="true"+ android:icon="@mipmap/ic_launcher"+ android:label="Weather">+ <intent-filter>+ <action android:name="androidx.wear.tiles.action.BIND_TILE_PROVIDER" />+ </intent-filter>+ <meta-data+ android:name="androidx.wear.tiles.PREVIEW"+ android:resource="@mipmap/ic_launcher" />+ </service> </application> </manifest>
weather/src/main/java/com/vayunmathur/weather/data/WearWeatherStore.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/data/WearWeatherStore.kt b/weather/src/main/java/com/vayunmathur/weather/data/WearWeatherStore.ktindex 9c45e70329..dd4c6bca70 100644--- a/weather/src/main/java/com/vayunmathur/weather/data/WearWeatherStore.kt+++ b/weather/src/main/java/com/vayunmathur/weather/data/WearWeatherStore.kt@@ -3,7 +3,9 @@ import android.content.Context import com.vayunmathur.library.log.Log import com.vayunmathur.library.wearlink.weather.WearWeatherProtocol import com.vayunmathur.library.wearlink.weather.WearWeatherSnapshot+import com.vayunmathur.library.weartiles.MaTileService import com.vayunmathur.weather.sync.WearLinkBridge+import com.vayunmathur.weather.service.WeatherTileService import java.io.File import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow@@ -14,7 +16,7 @@ class WearWeatherStore private constructor(private val context: Context) { fun get(c:Context):WearWeatherStore=i?:synchronized(this){i?:WearWeatherStore(c.applicationContext).also{i=it}} } private val file=File(context.filesDir,"wear-weather-snapshot.json") private val _s=MutableStateFlow(load()); val snapshot:StateFlow<WearWeatherSnapshot> = _s.asStateFlow()- fun setSnapshot(s:WearWeatherSnapshot){ _s.value=s; runCatching{file.writeText(WearWeatherProtocol.json.encodeToString(s))}.onFailure{Log.status(TAG,it.message?:"persist failed")}; publishWatchState() }+ fun setSnapshot(s:WearWeatherSnapshot){ _s.value=s; MaTileService.requestUpdate(context, WeatherTileService::class.java); runCatching{file.writeText(WearWeatherProtocol.json.encodeToString(s))}.onFailure{Log.status(TAG,it.message?:"persist failed")}; publishWatchState() } fun publishWatchState(){ WearLinkBridge.publishWatchState(context,_s.value) } private fun load():WearWeatherSnapshot=runCatching{WearWeatherProtocol.json.decodeFromString<WearWeatherSnapshot>(file.readText())}.getOrDefault(WearWeatherSnapshot()) }
weather/src/main/java/com/vayunmathur/weather/service/WeatherTileService.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/service/WeatherTileService.kt b/weather/src/main/java/com/vayunmathur/weather/service/WeatherTileService.ktnew file mode 100644index 0000000000..ed14bb7aae--- /dev/null+++ b/weather/src/main/java/com/vayunmathur/weather/service/WeatherTileService.kt@@ -0,0 +1,48 @@+package com.vayunmathur.weather.service++import android.content.Context+import com.vayunmathur.library.weartiles.MaTileService+import com.vayunmathur.library.weartiles.TileSnapshot+import com.vayunmathur.library.wearlink.weather.WearWeatherSnapshot+import com.vayunmathur.weather.data.WearWeatherStore+import com.vayunmathur.weather.domain.weatherConditionForCode+import kotlin.math.roundToInt++/** Wear Tile for Weather: current conditions of the primary location, from the wear snapshot. */+class WeatherTileService : MaTileService() {+ override val launchActivityName: String = "com.vayunmathur.weather.ui.WearMainActivity"++ override fun loadSnapshot(context: Context): TileSnapshot {+ val snapshot = WearWeatherStore.get(context).snapshot.value+ val location = snapshot.locations.firstOrNull { it.isPrimary } ?: snapshot.locations.firstOrNull()+ val current = location?.current ?: return previewSnapshot().copy(primary = "—", secondary = "Open app to sync")+ val condition = weatherConditionForCode(current.weatherCode)+ return weatherTileSnapshot(+ locationName = location.name,+ temperatureC = current.temperatureC,+ conditionLabel = condition.name,+ iconResId = condition.iconRes(current.isDay),+ )+ }++ override fun previewSnapshot(): TileSnapshot = weatherTileSnapshot("Home", 21.0, "Clear", iconResId = 0)+}++/** Pure mapping from the wear weather snapshot's primary current conditions to a tile. */+fun weatherTileSnapshot(+ locationName: String,+ temperatureC: Double,+ conditionLabel: String,+ iconResId: Int,+): TileSnapshot = TileSnapshot(+ appName = "Weather",+ primary = "${temperatureC.roundToInt()}°",+ secondary = listOf(conditionLabel, locationName).filter { it.isNotEmpty() }.joinToString(" · "),+ iconResId = iconResId,+)++internal fun weatherTileSnapshotFor(snapshot: WearWeatherSnapshot): TileSnapshot {+ val location = snapshot.locations.firstOrNull { it.isPrimary } ?: snapshot.locations.firstOrNull()+ val current = location?.current ?: return TileSnapshot.empty("Weather")+ return weatherTileSnapshot(location.name, current.temperatureC, weatherConditionForCode(current.weatherCode).name, 0)+}
weather/src/test/java/com/vayunmathur/weather/service/WeatherTileServiceTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/service/WeatherTileServiceTest.kt b/weather/src/test/java/com/vayunmathur/weather/service/WeatherTileServiceTest.ktnew file mode 100644index 0000000000..3e40ab3b71--- /dev/null+++ b/weather/src/test/java/com/vayunmathur/weather/service/WeatherTileServiceTest.kt@@ -0,0 +1,34 @@+package com.vayunmathur.weather.service++import com.vayunmathur.library.weartiles.TileLayout+import com.vayunmathur.library.wearlink.weather.WearWeatherCurrent+import com.vayunmathur.library.wearlink.weather.WearWeatherLocation+import com.vayunmathur.library.wearlink.weather.WearWeatherSnapshot+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class WeatherTileServiceTest {+ @Test+ fun primaryLocationRendersTemperatureAndCondition() {+ val snapshot = WearWeatherSnapshot(+ generatedAtMillis = 1_700_000_000_000L,+ locations = listOf(+ WearWeatherLocation(+ name = "Home", isPrimary = true,+ current = WearWeatherCurrent(temperatureC = 21.4, weatherCode = 0),+ ),+ ),+ )+ val tile = weatherTileSnapshotFor(snapshot)+ assertEquals("21°", tile.primary)+ assertTrue(tile.secondary.contains("Home"), tile.secondary)+ val proto = TileLayout.build(tile, "com.vayunmathur.weather", "X").toString()+ assertTrue(proto.contains("21°"), proto)+ }++ @Test+ fun emptySnapshotFallsBackToDash() {+ assertEquals("—", weatherTileSnapshotFor(WearWeatherSnapshot()).primary)+ }+}