added Exception handling

This commit is contained in:
Lorenz Hohermuth 2025-03-25 14:52:15 +01:00
parent fd15283975
commit e7a565aa80
1 changed files with 52 additions and 21 deletions

View File

@ -55,6 +55,8 @@ import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.module.breeze.ui.theme.BreezeTheme import com.module.breeze.ui.theme.BreezeTheme
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import retrofit2.HttpException
import java.net.UnknownHostException
import java.text.DecimalFormat import java.text.DecimalFormat
import java.time.LocalDate import java.time.LocalDate
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -129,6 +131,9 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
val ctx = LocalContext.current val ctx = LocalContext.current
var textColor = if (isSystemInDarkTheme()) textColorDarkMode else textColorLightMode var textColor = if (isSystemInDarkTheme()) textColorDarkMode else textColorLightMode
var forecast by remember { mutableStateOf(ForecastSummary(0.0, 0.0, false, false, false)) } var forecast by remember { mutableStateOf(ForecastSummary(0.0, 0.0, false, false, false)) }
var weatherHttpExceptionMessage = ""
var isCurrentTempOk = true
var currentTemp by remember { var currentTemp by remember {
mutableStateOf( mutableStateOf(
WeatherResponse(Main(0.0, 0.0, 0.0, 0.0, 0, 0), emptyList(), 0L), 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) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
forecast = fetchWeather(ctx) try {
forecast = fetchWeather(ctx)
} catch (exception: HttpException) {
weatherHttpExceptionMessage = exception.message()
} catch (exception: UnknownHostException) {
weatherHttpExceptionMessage = "No Internet Connection"
}
} }
Navigation( Navigation(
@ -203,31 +214,51 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
) )
} }
Row { Row {
Icon( if (weatherHttpExceptionMessage.equals("")) {
imageVector = iconStyle.ArrowDownward, Icon(
contentDescription = "Arrow down Icon", imageVector = iconStyle.ArrowDownward,
) contentDescription = "Arrow down Icon",
Text( )
text = numberFormat.format(forecast.minTemp.roundToInt()) + "°", var maxTemp = numberFormat.format(forecast.minTemp.roundToInt()) + "°"
fontSize = fontSizeUpper, var minTemp = numberFormat.format(forecast.maxTemp.roundToInt()) + "°"
fontWeight = breezeFontWeight, Text(
) text = maxTemp,
Icon( fontSize = fontSizeUpper,
imageVector = iconStyle.ArrowUpward, fontWeight = breezeFontWeight,
contentDescription = "Arrow up Icon", )
) Icon(
Text( imageVector = iconStyle.ArrowUpward,
text = numberFormat.format(forecast.maxTemp.roundToInt()) + "°", contentDescription = "Arrow up Icon",
fontSize = fontSizeUpper, )
fontWeight = breezeFontWeight, Text(
) text = minTemp,
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
} else {
Text(
text = weatherHttpExceptionMessage,
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
}
} }
if (hasLocationPermission) { if (hasLocationPermission) {
location?.let { location?.let {
currentTemp = fetchCurrentTemp(ctx, it.latitude, it.latitude) 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(
text = numberFormat.format(currentTemp.main.temp.roundToInt()) + "°", text = currentTempText,
fontSize = fontSizeCurrentTemp, fontSize = fontSizeCurrentTemp,
fontWeight = breezeFontWeight, fontWeight = breezeFontWeight,
) )