added Exception handling
This commit is contained in:
parent
fd15283975
commit
e7a565aa80
|
@ -55,6 +55,8 @@ import androidx.compose.ui.unit.sp
|
|||
import androidx.core.content.ContextCompat
|
||||
import com.module.breeze.ui.theme.BreezeTheme
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import retrofit2.HttpException
|
||||
import java.net.UnknownHostException
|
||||
import java.text.DecimalFormat
|
||||
import java.time.LocalDate
|
||||
import kotlin.math.roundToInt
|
||||
|
@ -129,6 +131,9 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
|
|||
val ctx = LocalContext.current
|
||||
var textColor = if (isSystemInDarkTheme()) textColorDarkMode else textColorLightMode
|
||||
var forecast by remember { mutableStateOf(ForecastSummary(0.0, 0.0, false, false, false)) }
|
||||
var weatherHttpExceptionMessage = ""
|
||||
var isCurrentTempOk = true
|
||||
|
||||
var currentTemp by remember {
|
||||
mutableStateOf(
|
||||
WeatherResponse(Main(0.0, 0.0, 0.0, 0.0, 0, 0), emptyList(), 0L),
|
||||
|
@ -163,7 +168,13 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
|
|||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
try {
|
||||
forecast = fetchWeather(ctx)
|
||||
} catch (exception: HttpException) {
|
||||
weatherHttpExceptionMessage = exception.message()
|
||||
} catch (exception: UnknownHostException) {
|
||||
weatherHttpExceptionMessage = "No Internet Connection"
|
||||
}
|
||||
}
|
||||
|
||||
Navigation(
|
||||
|
@ -203,12 +214,15 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
|
|||
)
|
||||
}
|
||||
Row {
|
||||
if (weatherHttpExceptionMessage.equals("")) {
|
||||
Icon(
|
||||
imageVector = iconStyle.ArrowDownward,
|
||||
contentDescription = "Arrow down Icon",
|
||||
)
|
||||
var maxTemp = numberFormat.format(forecast.minTemp.roundToInt()) + "°"
|
||||
var minTemp = numberFormat.format(forecast.maxTemp.roundToInt()) + "°"
|
||||
Text(
|
||||
text = numberFormat.format(forecast.minTemp.roundToInt()) + "°",
|
||||
text = maxTemp,
|
||||
fontSize = fontSizeUpper,
|
||||
fontWeight = breezeFontWeight,
|
||||
)
|
||||
|
@ -217,17 +231,34 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
|
|||
contentDescription = "Arrow up Icon",
|
||||
)
|
||||
Text(
|
||||
text = numberFormat.format(forecast.maxTemp.roundToInt()) + "°",
|
||||
text = minTemp,
|
||||
fontSize = fontSizeUpper,
|
||||
fontWeight = breezeFontWeight,
|
||||
)
|
||||
} else {
|
||||
Text(
|
||||
text = weatherHttpExceptionMessage,
|
||||
fontSize = fontSizeUpper,
|
||||
fontWeight = breezeFontWeight,
|
||||
)
|
||||
}
|
||||
}
|
||||
if (hasLocationPermission) {
|
||||
location?.let {
|
||||
try {
|
||||
currentTemp = fetchCurrentTemp(ctx, it.latitude, it.latitude)
|
||||
} catch (exception: UnknownHostException) {
|
||||
isCurrentTempOk = false
|
||||
}
|
||||
}
|
||||
var currentTempText = ""
|
||||
if (isCurrentTempOk) {
|
||||
currentTempText = numberFormat.format(currentTemp.main.temp.roundToInt()) + "°"
|
||||
} else {
|
||||
currentTempText = "XX°"
|
||||
}
|
||||
Text(
|
||||
text = numberFormat.format(currentTemp.main.temp.roundToInt()) + "°",
|
||||
text = currentTempText,
|
||||
fontSize = fontSizeCurrentTemp,
|
||||
fontWeight = breezeFontWeight,
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue