Open OmarLkhalil opened 7 months ago
and you may need to update observeviewmodel function in home fragment also to handle if networkWeatherDescription is empty
weather.observe(viewLifecycleOwner) { weather -> weather?.let { prefs.saveCityId(it.cityId)
if (prefs.getSelectedTemperatureUnit() == activity?.resources?.getString(R.string.temp_unit_fahrenheit))
it.networkWeatherCondition.temp =
convertCelsiusToFahrenheit(it.networkWeatherCondition.temp)
binding.weather = it
if (it.networkWeatherDescription.isNotEmpty()) {
binding.networkWeatherDescription = it.networkWeatherDescription.first()
} else {
binding.networkWeatherDescription = null
}
}
}
class WeatherLocalDataSourceImpl
is thrown a null pointer exception
so returning an empty DBWeather if it's null fixing the issue
override suspend fun getWeather(): DBWeather? = withContext(ioDispatcher) { if (weatherDao.getWeather() == null) { return@withContext DBWeather( cityId = 0, cityName = "None", wind = Wind( speed = 0.0, deg = 0 ), networkWeatherDescription = listOf(), networkWeatherCondition = NetworkWeatherCondition( temp = 0.0, pressure = 0.0, humidity = 0.0 ) ) } else { return@withContext weatherDao.getWeather() } }