weather: show temperature for search results
- Commit
c041dab6ba32608ad5258eed90a86c1d785da4fa- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-16 18:16 UTC (4 months ago)
- Parents
62bf998
Diff
weather/src/main/java/com/vayunmathur/weather/network/OpenMeteoDto.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/network/OpenMeteoDto.kt b/weather/src/main/java/com/vayunmathur/weather/network/OpenMeteoDto.ktindex 7926492402..d120c14e4e 100644--- a/weather/src/main/java/com/vayunmathur/weather/network/OpenMeteoDto.kt+++ b/weather/src/main/java/com/vayunmathur/weather/network/OpenMeteoDto.kt@@ -37,6 +37,21 @@ data class RegionTimezone( @SerialName("utc_offset_seconds") val utcOffsetSeconds: Int = 0, ) +/**+ * Minimal Open-Meteo `/v1/forecast` response used to show the current+ * temperature next to a city-search result. Requests only `temperature_2m`+ * so the per-result lookups stay cheap.+ */+@Serializable+data class CurrentTemperatureResponse(+ val current: CurrentTemperature? = null,+)++@Serializable+data class CurrentTemperature(+ @SerialName("temperature_2m") val temperature: Double,+)+ @Serializable data class Minutely15( val time: List<String> = emptyList(),
weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt b/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.ktindex c4fb07bb08..fad753d3a6 100644--- a/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt+++ b/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt@@ -101,6 +101,22 @@ object WeatherApi { return NetworkClient.getJson(url) } + /**+ * Fetch just the current temperature (°C) for a coordinate. Used by the+ * city-search list to show the current temp next to each result. Requests+ * a single variable to keep these on-demand lookups lightweight.+ */+ suspend fun currentTemperature(latitude: Double, longitude: Double): Double? {+ val url = buildString {+ append(FORECAST_BASE)+ append("?latitude=").append(latitude)+ append("&longitude=").append(longitude)+ append("¤t=temperature_2m")+ append("&forecast_days=1")+ }+ return NetworkClient.getJson<CurrentTemperatureResponse>(url).current?.temperature+ }+ /** * Resolve the IANA time zone for a coordinate via Open-Meteo's * `timezone=auto`. Requests a single trivial variable (the endpoint rejects
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 b1f2145e30..b0eea1433c 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt@@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items@@ -64,6 +65,9 @@ import com.vayunmathur.weather.ui.components.LocationItem import com.vayunmathur.weather.ui.components.UseDeviceLocationCard import com.vayunmathur.weather.util.LocationProvider import com.vayunmathur.weather.util.WeatherViewModel+import com.vayunmathur.weather.util.formatTemperatureCompact+import com.vayunmathur.weather.util.rememberTempUnit+import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest@@ -355,6 +359,8 @@ fun SearchLocationPage(backStack: NavBackStack<Route>, viewModel: WeatherViewMod var query by remember { mutableStateOf("") } var results by remember { mutableStateOf<List<GeocodingResult>>(emptyList()) } var searching by remember { mutableStateOf(false) }+ var temps by remember { mutableStateOf<Map<Long, Double?>>(emptyMap()) }+ val tempUnit = rememberTempUnit() LaunchedEffect(Unit) { snapshotFlow { query }@@ -375,6 +381,20 @@ fun SearchLocationPage(backStack: NavBackStack<Route>, viewModel: WeatherViewMod .collect { results = it } } + LaunchedEffect(results) {+ temps = emptyMap()+ coroutineScope {+ results.forEach { r ->+ launch {+ val temp = runCatching {+ WeatherApi.currentTemperature(r.latitude, r.longitude)+ }.getOrNull()+ temps = temps + (r.id to temp)+ }+ }+ }+ }+ androidx.compose.material3.Surface( shape = MaterialTheme.shapes.extraLarge, color = MaterialTheme.colorScheme.surfaceContainer,@@ -429,6 +449,22 @@ fun SearchLocationPage(backStack: NavBackStack<Route>, viewModel: WeatherViewMod val parts = listOfNotNull(r.admin1, r.country).filter { it.isNotBlank() } if (parts.isNotEmpty()) Text(parts.joinToString(", ")) },+ trailingContent = {+ if (temps.containsKey(r.id)) {+ temps[r.id]?.let { temp ->+ Text(+ formatTemperatureCompact(temp, tempUnit),+ style = MaterialTheme.typography.titleMedium,+ color = MaterialTheme.colorScheme.onSurface,+ )+ }+ } else {+ CircularProgressIndicator(+ modifier = Modifier.size(16.dp),+ strokeWidth = 2.dp,+ )+ }+ }, modifier = Modifier .fillMaxWidth() .padding(vertical = 2.dp)