Avoid if-null Checks.

This commit is contained in:
Erik C. Thauvin 2021-11-14 15:38:38 -08:00
parent f1b50d741c
commit 29b8888eca
2 changed files with 30 additions and 25 deletions

View file

@ -144,31 +144,34 @@ class Weather2 : ThreadedModule() {
country.name.replace('_', ' ').capitalizeWords() + " [${country.value}]" country.name.replace('_', ' ').capitalizeWords() + " [${country.value}]"
) )
) )
with(cwd.mainData) { cwd.mainData?.let {
if (this != null) { with(it) {
if (hasTemp()) { if (hasTemp()) {
val t = ftoC(temp) val t = ftoC(temp)
messages.add(PublicMessage("Temperature: ${t.first}°F, ${t.second}°C")) messages.add(PublicMessage("Temperature: ${t.first}°F, ${t.second}°C"))
} }
if (hasHumidity() && humidity != null) { if (hasHumidity()) {
messages.add(NoticeMessage("Humidity: ${(humidity!!).roundToInt()}%")) humidity?.let { h ->
messages.add(NoticeMessage("Humidity: ${h.roundToInt()}%"))
}
} }
} }
} }
if (cwd.hasWindData()) { if (cwd.hasWindData()) {
with(cwd.windData) { cwd.windData?.let {
if (this != null && hasSpeed() && speed != null) { if (it.hasSpeed()) {
val w = mphToKmh(speed!!) it.speed?.let { s ->
messages.add(NoticeMessage("Wind: ${w.first} mph, ${w.second} km/h")) val w = mphToKmh(s)
messages.add(NoticeMessage("Wind: ${w.first} mph, ${w.second} km/h"))
}
} }
} }
} }
if (cwd.hasWeatherList()) { if (cwd.hasWeatherList()) {
val condition = StringBuilder("Condition:") val condition = StringBuilder("Condition:")
val list = cwd.weatherList cwd.weatherList?.let {
if (list != null) { for (w in it) {
for (w in list) { w?.let {
if (w != null) {
condition.append(' ') condition.append(' ')
.append(w.getDescription().capitalise()) .append(w.getDescription().capitalise())
.append('.') .append('.')
@ -177,19 +180,21 @@ class Weather2 : ThreadedModule() {
messages.add(NoticeMessage(condition.toString())) messages.add(NoticeMessage(condition.toString()))
} }
} }
if (cwd.hasCityId() && cwd.cityId != null) { if (cwd.hasCityId()) {
if (cwd.cityId!! > 0) { cwd.cityId?.let {
messages.add( if (it > 0) {
NoticeMessage("https://openweathermap.org/city/${cwd.cityId}", Colors.GREEN) messages.add(
) NoticeMessage("https://openweathermap.org/city/$it", Colors.GREEN)
} else {
messages.add(
NoticeMessage(
"https://openweathermap.org/find?q="
+ encodeUrl("$city,${code.uppercase()}"),
Colors.GREEN
) )
) } else {
messages.add(
NoticeMessage(
"https://openweathermap.org/find?q="
+ encodeUrl("$city,${code.uppercase()}"),
Colors.GREEN
)
)
}
} }
} }
} }

View file

@ -61,7 +61,7 @@ open class LocalProperties {
localProps.getProperty(key) localProps.getProperty(key)
} else { } else {
val env = System.getenv(keyToEnv(key)) val env = System.getenv(keyToEnv(key))
if (env != null) { env?.let {
localProps.setProperty(key, env) localProps.setProperty(key, env)
} }
env env