diff --git a/app/src/main/java/com/module/breeze/MainActivity.kt b/app/src/main/java/com/module/breeze/MainActivity.kt index d4963d2..3151f80 100644 --- a/app/src/main/java/com/module/breeze/MainActivity.kt +++ b/app/src/main/java/com/module/breeze/MainActivity.kt @@ -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, )