Weather: use null to indicate invalid values
This commit is contained in:
parent
065e6b828d
commit
3f7c2dbb8a
@ -249,18 +249,13 @@ fun CurrentWeather(forecast: Forecast, imperialUnits: Boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
val validHumidity = forecast.humidity != -1.0
|
||||
val validWindDirection = forecast.windDirection != -1.0
|
||||
val validWindSpeed = forecast.windSpeed != -1.0
|
||||
val validPrecip = forecast.precipitation != -1.0
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.padding(start = 16.dp, end = 16.dp, bottom = 12.dp, top = 8.dp)
|
||||
.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
if (validHumidity) {
|
||||
if (forecast.humidity != null) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
@ -271,18 +266,18 @@ fun CurrentWeather(forecast: Forecast, imperialUnits: Boolean) {
|
||||
)
|
||||
Spacer(modifier = Modifier.padding(3.dp))
|
||||
Text(
|
||||
text = "${forecast.humidity.roundToInt()} %",
|
||||
text = "${forecast.humidity!!.roundToInt()} %",
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
}
|
||||
if (validWindDirection || validWindSpeed) {
|
||||
if (forecast.windDirection != null || forecast.windSpeed != null) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (validWindDirection) {
|
||||
if (forecast.windDirection != null) {
|
||||
// windDirection is "fromDirection"; Wind (arrow) blows into opposite direction
|
||||
val angle by animateFloatAsState(forecast.windDirection.toFloat() + 180f)
|
||||
val angle by animateFloatAsState(forecast.windDirection!!.toFloat() + 180f)
|
||||
Icon(
|
||||
imageVector = Icons.Rounded.North,
|
||||
modifier = Modifier
|
||||
@ -292,23 +287,23 @@ fun CurrentWeather(forecast: Forecast, imperialUnits: Boolean) {
|
||||
)
|
||||
} else {
|
||||
Icon(
|
||||
imageVector = Icons.Rounded.WindPower,
|
||||
imageVector = Icons.Rounded.Air,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(20.dp)
|
||||
)
|
||||
}
|
||||
Spacer(modifier = Modifier.padding(3.dp))
|
||||
Text(
|
||||
text = if (validWindSpeed) {
|
||||
text = if (forecast.windSpeed != null) {
|
||||
formatWindSpeed(imperialUnits, forecast)
|
||||
} else {
|
||||
windDirectionAsWord(forecast.windDirection)
|
||||
windDirectionAsWord(forecast.windDirection!!)
|
||||
},
|
||||
style = MaterialTheme.typography.bodySmall
|
||||
)
|
||||
}
|
||||
}
|
||||
if (validPrecip) {
|
||||
if (forecast.precipitation != null) {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
@ -454,8 +449,9 @@ private fun convertTemperature(imperialUnits: Boolean, temp: Double): Int {
|
||||
|
||||
@Composable
|
||||
private fun formatWindSpeed(imperialUnits: Boolean, forecast: Forecast): String {
|
||||
if (forecast.windSpeed == null) return ""
|
||||
val formatter = DecimalFormat("0.#")
|
||||
val speedValue = formatter.format(forecast.windSpeed * if (imperialUnits) 0.621371 else 1.0)
|
||||
val speedValue = formatter.format(forecast.windSpeed!! * if (imperialUnits) 0.621371 else 1.0)
|
||||
val speedUnit =
|
||||
stringResource(id = if (imperialUnits) R.string.unit_mile_per_hour_symbol else R.string.unit_meter_per_second_symbol)
|
||||
return "$speedValue $speedUnit"
|
||||
@ -463,6 +459,9 @@ private fun formatWindSpeed(imperialUnits: Boolean, forecast: Forecast): String
|
||||
|
||||
@Composable
|
||||
private fun formatPrecipitation(imperialUnits: Boolean, forecast: Forecast): String {
|
||||
if (forecast.precipProbability != null) {
|
||||
return "${forecast.precipProbability} %"
|
||||
}
|
||||
val formatter = if (imperialUnits) DecimalFormat("#.##") else DecimalFormat("#.#")
|
||||
val precipUnit =
|
||||
if (imperialUnits) stringResource(id = R.string.unit_inch_symbol) else stringResource(id = R.string.unit_millimeter_symbol)
|
||||
|
||||
@ -28,13 +28,13 @@ data class Forecast(
|
||||
/** The temperature, in Kelvin **/
|
||||
val temperature: Double,
|
||||
/** The min temperature, in Kelvin **/
|
||||
val minTemp: Double = -1.0,
|
||||
val minTemp: Double? = null,
|
||||
/** The max temperature, in Kelvin **/
|
||||
val maxTemp: Double = -1.0,
|
||||
val maxTemp: Double? = null,
|
||||
/** The temperature, in hPa **/
|
||||
val pressure: Double = -1.0,
|
||||
val pressure: Double? = null,
|
||||
/** The temperature, in percent **/
|
||||
val humidity: Double = -1.0,
|
||||
val humidity: Double? = null,
|
||||
/** The icon, one of [NONE], [CLEAR], [CLOUDY], [COLD], [DRIZZLE], [HAZE], [FOG],
|
||||
* [HAIL], [HEAVY_THUNDERSTORM], [HEAVY_THUNDERSTORM_WITH_RAIN], [HOT], [MOSTLY_CLOUDY],
|
||||
* [PARTLY_CLOUDY], [SHOWERS], [SLEET], [SNOW], [STORM], [THUNDERSTORM],
|
||||
@ -43,13 +43,13 @@ data class Forecast(
|
||||
/** A text describing the current weather condition **/
|
||||
val condition: String,
|
||||
/** The clouds, percentage **/
|
||||
val clouds: Int = -1,
|
||||
val clouds: Int? = null,
|
||||
/** Wind speed, in m/s **/
|
||||
val windSpeed: Double = -1.0,
|
||||
val windSpeed: Double? = null,
|
||||
/** wind direction, in degrees **/
|
||||
val windDirection: Double = -1.0,
|
||||
val windDirection: Double? = null,
|
||||
/** rain, in mm per hour **/
|
||||
val precipitation: Double = -1.0,
|
||||
val precipitation: Double? = null,
|
||||
/** whether this forecast is during night time (whether a moon icon should be used instead of sun) **/
|
||||
val night: Boolean = false,
|
||||
/** Location string **/
|
||||
@ -58,31 +58,31 @@ data class Forecast(
|
||||
val provider: String,
|
||||
/** Url to the provider and more weather information **/
|
||||
val providerUrl: String = "",
|
||||
/** Rain probability, in percent [0..100]. -1 if not available **/
|
||||
val precipProbability: Int = -1,
|
||||
/** Rain probability, in percent [0..100]. null if not available **/
|
||||
val precipProbability: Int? = null,
|
||||
/** Timestamp (in millis) when when this forecast was created **/
|
||||
val updateTime: Long
|
||||
) {
|
||||
fun toDatabaseEntity(): ForecastEntity {
|
||||
return ForecastEntity(
|
||||
timestamp = timestamp,
|
||||
clouds = clouds,
|
||||
clouds = clouds ?: -1,
|
||||
condition = condition,
|
||||
humidity = humidity,
|
||||
humidity = humidity ?: -1.0,
|
||||
icon = icon,
|
||||
location = location,
|
||||
maxTemp = maxTemp,
|
||||
minTemp = minTemp,
|
||||
maxTemp = maxTemp ?: -1.0,
|
||||
minTemp = minTemp ?: -1.0,
|
||||
night = night,
|
||||
pressure = pressure,
|
||||
pressure = pressure ?: -1.0,
|
||||
provider = provider,
|
||||
providerUrl = providerUrl,
|
||||
precipitation = precipitation,
|
||||
precipProbability = precipProbability,
|
||||
precipitation = precipitation ?: -1.0,
|
||||
precipProbability = precipProbability ?: -1,
|
||||
temperature = temperature,
|
||||
updateTime = updateTime,
|
||||
windDirection = windDirection,
|
||||
windSpeed = windSpeed,
|
||||
windDirection = windDirection ?: -1.0,
|
||||
windSpeed = windSpeed ?: -1.0,
|
||||
snow = -1.0,
|
||||
snowProbability = -1
|
||||
)
|
||||
@ -90,23 +90,23 @@ data class Forecast(
|
||||
|
||||
constructor(entity: ForecastEntity) : this(
|
||||
timestamp = entity.timestamp,
|
||||
clouds = entity.clouds,
|
||||
clouds = entity.clouds.takeIf { it >= 0 },
|
||||
condition = entity.condition,
|
||||
humidity = entity.humidity,
|
||||
humidity = entity.humidity.takeIf { it >= 0.0 },
|
||||
icon = entity.icon,
|
||||
location = entity.location,
|
||||
maxTemp = entity.maxTemp,
|
||||
minTemp = entity.minTemp,
|
||||
maxTemp = entity.maxTemp.takeIf { it >= 0.0 },
|
||||
minTemp = entity.minTemp.takeIf { it >= 0.0 },
|
||||
night = entity.night,
|
||||
pressure = entity.pressure,
|
||||
pressure = entity.pressure.takeIf { it >= 0.0 },
|
||||
provider = entity.provider,
|
||||
providerUrl = entity.providerUrl,
|
||||
precipitation = entity.precipitation,
|
||||
precipProbability = entity.precipProbability,
|
||||
precipitation = entity.precipitation.takeIf { it >= 0.0 },
|
||||
precipProbability = entity.precipProbability.takeIf { it >= 0 },
|
||||
temperature = entity.temperature,
|
||||
updateTime = entity.updateTime,
|
||||
windDirection = entity.windDirection,
|
||||
windSpeed = entity.windSpeed
|
||||
windDirection = entity.windDirection.takeIf { it >= 0.0 },
|
||||
windSpeed = entity.windSpeed.takeIf { it >= 0.0 },
|
||||
)
|
||||
|
||||
companion object {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user