weather: extend hourly forecasts, fix pressure units & 24 hour time based on android settings #215
- Commit
5ee752a05cd467ede4a9759d16369343d83591e6- Author
- Vayun Mathur <[email protected]>
- When
- 2026-06-14 23:33 UTC (5 months ago)
- Parents
c606249
Diff
weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt b/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.ktindex 794a665a95..3e007edf0d 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/HomePage.kt@@ -133,6 +133,8 @@ private fun LocationPage( val forecasts by viewModel.forecasts.collectAsState() val tempUnit = com.vayunmathur.weather.util.rememberTempUnit() val windUnit = com.vayunmathur.weather.util.rememberWindUnit()+ val pressureUnit = com.vayunmathur.weather.util.rememberPressureUnit()+ val use24Hour = com.vayunmathur.weather.util.rememberUse24Hour() LaunchedEffect(location.id) { viewModel.ensureForecast(location) } @@ -172,7 +174,7 @@ private fun LocationPage( ) { SummaryCard(forecast = forecast, tempUnit = tempUnit) if (forecast.hourly != null) {- HourlyCard(hourly = forecast.hourly, tempUnit = tempUnit, utcOffsetSeconds = forecast.utcOffsetSeconds)+ HourlyCard(hourly = forecast.hourly, tempUnit = tempUnit, utcOffsetSeconds = forecast.utcOffsetSeconds, use24Hour = use24Hour) } if (daily != null) { DailyCard(daily = daily, tempUnit = tempUnit)@@ -186,6 +188,8 @@ private fun LocationPage( sunsetEpochSec = sunsetEpoch, tempUnit = tempUnit, windUnit = windUnit,+ pressureUnit = pressureUnit,+ use24Hour = use24Hour, ) } CreditsBottomSection()
weather/src/main/java/com/vayunmathur/weather/ui/components/CreditsBottomSection.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/CreditsBottomSection.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/CreditsBottomSection.ktdeleted file mode 100644index 24f19d15c7..0000000000--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/CreditsBottomSection.kt+++ /dev/null@@ -1,38 +0,0 @@-package com.vayunmathur.weather.ui.components--import androidx.compose.foundation.layout.WindowInsets-import androidx.compose.foundation.layout.asPaddingValues-import androidx.compose.foundation.layout.fillMaxWidth-import androidx.compose.foundation.layout.height-import androidx.compose.foundation.layout.padding-import androidx.compose.foundation.layout.systemBars-import androidx.compose.material3.MaterialTheme-import androidx.compose.material3.Text-import androidx.compose.runtime.Composable-import androidx.compose.ui.Modifier-import androidx.compose.ui.text.font.FontWeight-import androidx.compose.ui.text.style.TextAlign-import androidx.compose.ui.text.style.TextDecoration-import androidx.compose.ui.unit.dp--/**- * Mirrors WeatherMaster's `CreditsBottomSection`: a single centered,- * bold-underlined line attributing the data source, with a trailing system- * insets spacer so the credit sits above the gesture bar.- */-@Composable-fun CreditsBottomSection() {- val bottomInset = WindowInsets.systemBars.asPaddingValues().calculateBottomPadding()- Text(- text = "Open-Meteo and more",- modifier = Modifier- .fillMaxWidth()- .padding(top = 4.dp),- style = MaterialTheme.typography.bodyLarge,- fontWeight = FontWeight.Bold,- textDecoration = TextDecoration.Underline,- color = MaterialTheme.colorScheme.onSurface,- textAlign = TextAlign.Center,- )- androidx.compose.foundation.layout.Spacer(Modifier.height(bottomInset))-}
weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.ktindex f592dd023d..c99dc28bde 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt@@ -28,11 +28,17 @@ import com.vayunmathur.weather.network.Hourly import com.vayunmathur.weather.util.TemperatureUnit import com.vayunmathur.weather.util.formatTemperatureCompact import com.vayunmathur.weather.util.weatherConditionForCode+import androidx.compose.ui.unit.sp+import kotlin.time.Clock+import kotlinx.datetime.DateTimeUnit+import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDateTime import kotlinx.datetime.TimeZone import kotlinx.datetime.UtcOffset+import kotlinx.datetime.plus import kotlinx.datetime.toInstant import kotlinx.datetime.toLocalDateTime+import kotlinx.datetime.todayIn import kotlin.time.Instant /**@@ -43,7 +49,7 @@ import kotlin.time.Instant * `primary`. */ @Composable-fun HourlyCard(hourly: Hourly, tempUnit: TemperatureUnit, utcOffsetSeconds: Int = 0) {+fun HourlyCard(hourly: Hourly, tempUnit: TemperatureUnit, utcOffsetSeconds: Int = 0, use24Hour: Boolean = false) { val nowSec = System.currentTimeMillis() / 1000 val cells = hourly.time.indices .mapNotNull { i ->@@ -57,7 +63,6 @@ fun HourlyCard(hourly: Hourly, tempUnit: TemperatureUnit, utcOffsetSeconds: Int isDay = (hourly.isDay.getOrNull(i) ?: 1) == 1, ) }- .take(24) if (cells.isEmpty()) return Surface(@@ -72,7 +77,8 @@ fun HourlyCard(hourly: Hourly, tempUnit: TemperatureUnit, utcOffsetSeconds: Int val cell = cells[index] if (index == 0) Spacer(Modifier.width(10.dp)) HourlyItem(- time = if (index == 0) "Now" else formatHour(cell.epochSec),+ time = if (index == 0) "Now" else formatHour(cell.epochSec, use24Hour),+ dayLabel = formatDayLabel(cell.epochSec), precipitationProbability = cell.precip, temperature = cell.temperature, isNow = index == 0,@@ -89,6 +95,7 @@ fun HourlyCard(hourly: Hourly, tempUnit: TemperatureUnit, utcOffsetSeconds: Int @Composable private fun HourlyItem( time: String,+ dayLabel: String, precipitationProbability: Int, temperature: Double, isNow: Boolean,@@ -96,7 +103,7 @@ private fun HourlyItem( tempUnit: TemperatureUnit, ) { Column(- modifier = Modifier.height(120.dp).width(45.dp),+ modifier = Modifier.height(135.dp).width(45.dp), verticalArrangement = Arrangement.Bottom, horizontalAlignment = Alignment.CenterHorizontally, ) {@@ -118,6 +125,11 @@ private fun HourlyItem( color = MaterialTheme.colorScheme.onSurfaceVariant, style = MaterialTheme.typography.labelLarge, )+ Text(+ dayLabel,+ color = Color.Gray,+ fontSize = 10.sp,+ ) } } @@ -161,10 +173,23 @@ private fun parseIsoToEpochSec(iso: String?, utcOffsetSeconds: Int = 0): Long? { ?: runCatching { Instant.parse(iso).epochSeconds }.getOrNull() } -private fun formatHour(epochSec: Long): String {+private fun formatHour(epochSec: Long, use24Hour: Boolean): String { val ldt = Instant.fromEpochSeconds(epochSec).toLocalDateTime(TimeZone.currentSystemDefault()) val h = ldt.hour+ if (use24Hour) return "$h" val ampm = if (h < 12) "AM" else "PM" val display = if (h % 12 == 0) 12 else h % 12 return "$display $ampm" }++private fun formatDayLabel(epochSec: Long): String {+ val tz = TimeZone.currentSystemDefault()+ val date = Instant.fromEpochSeconds(epochSec).toLocalDateTime(tz).date+ val today = Clock.System.todayIn(tz)+ val tomorrow = today.plus(1, DateTimeUnit.DAY)+ return when (date) {+ today -> "TDY"+ tomorrow -> "TMR"+ else -> date.dayOfWeek.name.take(3)+ }+}
weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.ktindex 9afb7a40f4..17632bda0a 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/WeatherBlocks.kt@@ -21,6 +21,7 @@ import com.vayunmathur.weather.ui.components.blocks.UvIndexBlock import com.vayunmathur.weather.ui.components.blocks.VisibilityBlock import com.vayunmathur.weather.ui.components.blocks.WindBlock import com.vayunmathur.weather.util.TemperatureUnit+import com.vayunmathur.weather.util.PressureUnit import com.vayunmathur.weather.util.WindUnit /**@@ -41,6 +42,8 @@ fun WeatherBlocks( sunsetEpochSec: Long?, tempUnit: TemperatureUnit, windUnit: WindUnit,+ pressureUnit: PressureUnit,+ use24Hour: Boolean, ) { LazyVerticalGrid( columns = GridCells.Adaptive(minSize = 140.dp),@@ -53,9 +56,9 @@ fun WeatherBlocks( item { HumidityBlock(current = current, tempUnit = tempUnit) } item { UvIndexBlock(uvIndex = today?.uvIndexMax?.firstOrNull()) } item { WindBlock(current = current, unit = windUnit) }- item { PressureBlock(current = current) }+ item { PressureBlock(current = current, pressureUnit = pressureUnit) } item { VisibilityBlock(current = current, useMiles = windUnit == WindUnit.Mph) }- item { SunBlock(sunriseEpochSec = sunriseEpochSec, sunsetEpochSec = sunsetEpochSec) }+ item { SunBlock(sunriseEpochSec = sunriseEpochSec, sunsetEpochSec = sunsetEpochSec, use24Hour = use24Hour) } item { AirQualityBlock(air = air) } item { PollenBlock(air = air) } }
weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/PressureBlock.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/PressureBlock.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/PressureBlock.ktindex b042e39ecc..2cea79c109 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/PressureBlock.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/PressureBlock.kt@@ -17,6 +17,7 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp import com.vayunmathur.weather.R import com.vayunmathur.weather.network.Current+import com.vayunmathur.weather.util.PressureUnit /** * Port of WeatherMaster's `PressureBlock`. Circular surface with two@@ -25,15 +26,41 @@ import com.vayunmathur.weather.network.Current * the hPa bucket. Big inHg value centered, unit bottom-center. */ @Composable-fun PressureBlock(current: Current) {- val pressureHpa = current.pressureMsl.toInt()- val inHg = current.pressureMsl * 0.02953- val progressDrawable = when {- pressureHpa < 980 -> R.drawable.pressure_progress_low- pressureHpa in 980..1005 -> R.drawable.pressure_progress_medium- pressureHpa in 1005..1020 -> R.drawable.pressure_progress_low_medium- pressureHpa in 1020..1035 -> R.drawable.pressure_progress_high- else -> R.drawable.pressure_progress_very_high+fun PressureBlock(current: Current, pressureUnit: PressureUnit) {+ val valueText: String+ val unitText: String+ when (pressureUnit) {+ PressureUnit.InHg -> {+ val inHg = current.pressureMsl * 0.02953+ valueText = String.format("%.2f", inHg)+ unitText = "inHg"+ }+ PressureUnit.Hpa -> {+ valueText = current.pressureMsl.toInt().toString()+ unitText = "hPa"+ }+ }+ val progressDrawable = when (pressureUnit) {+ PressureUnit.InHg -> {+ val inHg = current.pressureMsl * 0.02953+ when {+ inHg < 29.0 -> R.drawable.pressure_progress_low+ inHg in 29.0..29.7 -> R.drawable.pressure_progress_medium+ inHg in 29.7..30.2 -> R.drawable.pressure_progress_low_medium+ inHg in 30.2..31.0 -> R.drawable.pressure_progress_high+ else -> R.drawable.pressure_progress_very_high+ }+ }+ PressureUnit.Hpa -> {+ val pressureHpa = current.pressureMsl.toInt()+ when {+ pressureHpa < 980 -> R.drawable.pressure_progress_low+ pressureHpa in 980..1005 -> R.drawable.pressure_progress_medium+ pressureHpa in 1005..1020 -> R.drawable.pressure_progress_low_medium+ pressureHpa in 1020..1035 -> R.drawable.pressure_progress_high+ else -> R.drawable.pressure_progress_very_high+ }+ } } Surface(@@ -62,13 +89,13 @@ fun PressureBlock(current: Current) { ) } Text(- text = String.format("%.2f", inHg),+ text = valueText, style = MaterialTheme.typography.displaySmall, modifier = Modifier.align(Alignment.Center).offset(y = 10.dp), color = MaterialTheme.colorScheme.onSurface, ) Text(- text = "inHg",+ text = unitText, modifier = Modifier.align(Alignment.BottomCenter).offset(y = (-24).dp), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant,
weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.ktindex 72c691ef67..4901e05289 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/blocks/SunBlock.kt@@ -39,7 +39,7 @@ import kotlin.time.Instant * progress, and a translucent bottom panel with sunrise / sunset times. */ @Composable-fun SunBlock(sunriseEpochSec: Long?, sunsetEpochSec: Long?) {+fun SunBlock(sunriseEpochSec: Long?, sunsetEpochSec: Long?, use24Hour: Boolean) { Surface( color = MaterialTheme.colorScheme.surface, shape = MaterialTheme.shapes.extraLarge,@@ -97,11 +97,11 @@ fun SunBlock(sunriseEpochSec: Long?, sunsetEpochSec: Long?) { verticalArrangement = Arrangement.spacedBy(4.dp), ) { RiseSetTimeRow(- text = sunriseEpochSec?.let { formatTime(it) } ?: "—",+ text = sunriseEpochSec?.let { formatTime(it, use24Hour) } ?: "—", iconRes = R.drawable.outline_clear_day_24, ) RiseSetTimeRow(- text = sunsetEpochSec?.let { formatTime(it) } ?: "—",+ text = sunsetEpochSec?.let { formatTime(it, use24Hour) } ?: "—", iconRes = R.drawable.outline_clear_night_24, ) }@@ -124,12 +124,16 @@ private fun RiseSetTimeRow(text: String, iconRes: Int) { } } -private fun formatTime(epochSec: Long): String {+private fun formatTime(epochSec: Long, use24Hour: Boolean): String { val ldt = Instant.fromEpochSeconds(epochSec).toLocalDateTime(TimeZone.currentSystemDefault()) val h = ldt.hour val m = ldt.minute- val display = if (h % 12 == 0) 12 else h % 12- val ampm = if (h < 12) "AM" else "PM" val mm = m.toString().padStart(2, '0')- return "$display:$mm $ampm"+ return if (use24Hour) {+ "${h.toString().padStart(2, '0')}:$mm"+ } else {+ val display = if (h % 12 == 0) 12 else h % 12+ val ampm = if (h < 12) "AM" else "PM"+ "$display:$mm $ampm"+ } }
weather/src/main/java/com/vayunmathur/weather/util/SystemUnits.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/util/SystemUnits.kt b/weather/src/main/java/com/vayunmathur/weather/util/SystemUnits.ktindex 29f6ef3d01..772c64b824 100644--- a/weather/src/main/java/com/vayunmathur/weather/util/SystemUnits.kt+++ b/weather/src/main/java/com/vayunmathur/weather/util/SystemUnits.kt@@ -3,6 +3,7 @@ package com.vayunmathur.weather.util import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalConfiguration+import androidx.compose.ui.platform.LocalContext import androidx.core.text.util.LocalePreferences import java.util.Locale @@ -41,6 +42,23 @@ fun rememberWindUnit(): WindUnit { } } +@Composable+fun rememberPressureUnit(): PressureUnit {+ LocalConfiguration.current+ return remember(LocalConfiguration.current) {+ if (isImperialMeasurementSystem(Locale.getDefault())) PressureUnit.InHg else PressureUnit.Hpa+ }+}++@Composable+fun rememberUse24Hour(): Boolean {+ LocalConfiguration.current+ val context = LocalContext.current+ return remember(LocalConfiguration.current) {+ android.text.format.DateFormat.is24HourFormat(context)+ }+}+ /** * True when the locale opts into a non-metric measurement system, either * via the explicit `-u-ms-…` Unicode extension that Android 14's regional
weather/src/main/java/com/vayunmathur/weather/util/UnitFormat.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/util/UnitFormat.kt b/weather/src/main/java/com/vayunmathur/weather/util/UnitFormat.ktindex cd5d460924..eb4150a77e 100644--- a/weather/src/main/java/com/vayunmathur/weather/util/UnitFormat.kt+++ b/weather/src/main/java/com/vayunmathur/weather/util/UnitFormat.kt@@ -8,6 +8,9 @@ enum class TemperatureUnit { Celsius, Fahrenheit } /** Wind-speed display unit. Storage / API is always km/h. */ enum class WindUnit { KmH, Mph } +/** Pressure display unit. Storage / API is always hPa. */+enum class PressureUnit { Hpa, InHg }+ fun Double.celsiusTo(unit: TemperatureUnit): Double = when (unit) { TemperatureUnit.Celsius -> this TemperatureUnit.Fahrenheit -> this * 9.0 / 5.0 + 32.0@@ -23,6 +26,11 @@ fun formatTemperature(celsius: Double, unit: TemperatureUnit): String { fun formatTemperatureCompact(celsius: Double, unit: TemperatureUnit): String = "${celsius.celsiusTo(unit).roundToInt()}°" +fun formatPressure(hpa: Double, unit: PressureUnit): String = when (unit) {+ PressureUnit.InHg -> String.format("%.2f inHg", hpa * 0.02953)+ PressureUnit.Hpa -> "${hpa.roundToInt()} hPa"+}+ fun formatWind(kph: Double, unit: WindUnit): String = when (unit) { WindUnit.KmH -> "${kph.roundToInt()} km/h" WindUnit.Mph -> "${(kph * 0.621371).roundToInt()} mph"