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
app/src/main/java/com/module/breeze

View File

@ -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) {
forecast = fetchWeather(ctx)
try {
forecast = fetchWeather(ctx)
} catch (exception: HttpException) {
weatherHttpExceptionMessage = exception.message()
} catch (exception: UnknownHostException) {
weatherHttpExceptionMessage = "No Internet Connection"
}
}
Navigation(
@ -203,31 +214,51 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
)
}
Row {
Icon(
imageVector = iconStyle.ArrowDownward,
contentDescription = "Arrow down Icon",
)
Text(
text = numberFormat.format(forecast.minTemp.roundToInt()) + "°",
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
Icon(
imageVector = iconStyle.ArrowUpward,
contentDescription = "Arrow up Icon",
)
Text(
text = numberFormat.format(forecast.maxTemp.roundToInt()) + "°",
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
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 = maxTemp,
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
Icon(
imageVector = iconStyle.ArrowUpward,
contentDescription = "Arrow up Icon",
)
Text(
text = minTemp,
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
} else {
Text(
text = weatherHttpExceptionMessage,
fontSize = fontSizeUpper,
fontWeight = breezeFontWeight,
)
}
}
if (hasLocationPermission) {
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 = numberFormat.format(currentTemp.main.temp.roundToInt()) + "°",
text = currentTempText,
fontSize = fontSizeCurrentTemp,
fontWeight = breezeFontWeight,
)