improve null handling of sun/moon block

This commit is contained in:
PranshulGG committed 2026-08-23 20:33:24 +05:30
1 parent 1bebbac4ae
commit f3efc3f73a
4 files changed
+21 -13

No files matched your search

@@ -143,12 +143,12 @@ data class WeatherDaily(
return pressureMsl != null && pressureMsl != -1.0
}
fun isMoonTimesValid(): Boolean {
fun isMoonValid(): Boolean {
return moonrise != null && moonset != null
}
fun isSunTimesValid(): Boolean {
fun isSunValid(): Boolean {
return sunrise != null && sunset != null
}
}
@@ -38,6 +38,7 @@ import com.pranshulgg.weather_master_app.core.prefs.AppPrefsState
import com.pranshulgg.weather_master_app.core.ui.components.Gap
import com.pranshulgg.weather_master_app.core.ui.components.Symbol
import com.pranshulgg.weather_master_app.core.ui.theme.ShadowElevation
import com.pranshulgg.weather_master_app.core.utils.formatters.getCurrentTimeFor
import com.pranshulgg.weather_master_app.core.utils.formatters.to12HourTimeString
import com.pranshulgg.weather_master_app.core.utils.formatters.to24HourTimeString
import com.pranshulgg.weather_master_app.core.utils.weather.cache.isWeatherDailyDomainSafe
@@ -76,9 +77,8 @@ fun MoonBlock(weather: Weather, dailyIndex: Int, prefs: AppPrefsState, onClickBl
val moonrise = daily.moonrise
val moonset = daily.moonset
val now = System.currentTimeMillis()
val progress = ((now - moonrise).toFloat() / (moonset - moonrise))
val now = getCurrentTimeFor(weather.location.timezone)
val progress = ((now - moonrise!!).toFloat() / (moonset!! - moonrise))
.coerceIn(0f, 1f)
@@ -138,15 +138,15 @@ fun MoonBlock(weather: Weather, dailyIndex: Int, prefs: AppPrefsState, onClickBl
moonrise,
weather.location.timezone
) else to12HourTimeString(
moonrise,
moonrise!!,
weather.location.timezone,
"hh:mm a"
)
val moonsetFormatted = if (is24hr) to24HourTimeString(
moonset,
moonset!!,
weather.location.timezone
) else to12HourTimeString(
moonset,
moonset!!,
weather.location.timezone,
"hh:mm a"
)
@@ -80,8 +80,8 @@ fun SunBlock(weather: Weather, dailyIndex: Int, prefs: AppPrefsState, onClickBlo
val now = getCurrentTimeFor(weather.location.timezone)
val progress =
if (now in daily.sunrise..daily.sunset) ((now - sunrise).toFloat() / (sunset - sunrise))
val progress = if (now in daily.sunrise!!..daily.sunset!!)
((now - sunrise).toFloat() / (sunset - sunrise))
.coerceIn(0f, 1f) else 1f
@@ -39,8 +39,9 @@ private data class BlockRules(
val isPressureValid: Boolean,
val isVisibilityValid: Boolean,
val isWindValid: Boolean,
val isHumidityDewPoint: Boolean
val isHumidityDewPoint: Boolean,
val isSunValid: Boolean,
val isMoonValid: Boolean
)
private fun shouldShow(block: WeatherBlock, rules: BlockRules): Boolean {
@@ -55,6 +56,8 @@ private fun shouldShow(block: WeatherBlock, rules: BlockRules): Boolean {
!rules.isVisibilityValid && block.type == WeatherBlockType.VISIBILITY_BLOCK -> false
!rules.isWindValid && block.type == WeatherBlockType.WIND_BLOCK -> false
!rules.isHumidityDewPoint && block.type == WeatherBlockType.HUMIDITY_BLOCK -> false
!rules.isSunValid && block.type == WeatherBlockType.SUN_BLOCK -> false
!rules.isMoonValid && block.type == WeatherBlockType.MOON_BLOCK -> false
else -> true
}
}
@@ -106,6 +109,9 @@ fun WeatherBlocks(
weather.daily[dailyIndex].isHumidityValid() || weather.daily[dailyIndex].isDewPointValid()
else weather.current.isHumidityValid() || weather.current.isDewPointValid()
val isSunValid = weather.daily[dailyIndex].isSunValid()
val isMoonValid = weather.daily[dailyIndex].isMoonValid()
val prefs = LocalAppPrefs.current
val rules = BlockRules(
@@ -117,7 +123,9 @@ fun WeatherBlocks(
isPressureValid,
isVisibilityValid,
isWindValid,
isHumidityDewPoint
isHumidityDewPoint,
isSunValid,
isMoonValid
)