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 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) {
|
||||||
|
try {
|
||||||
forecast = fetchWeather(ctx)
|
forecast = fetchWeather(ctx)
|
||||||
|
} catch (exception: HttpException) {
|
||||||
|
weatherHttpExceptionMessage = exception.message()
|
||||||
|
} catch (exception: UnknownHostException) {
|
||||||
|
weatherHttpExceptionMessage = "No Internet Connection"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Navigation(
|
Navigation(
|
||||||
|
@ -203,12 +214,15 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Row {
|
Row {
|
||||||
|
if (weatherHttpExceptionMessage.equals("")) {
|
||||||
Icon(
|
Icon(
|
||||||
imageVector = iconStyle.ArrowDownward,
|
imageVector = iconStyle.ArrowDownward,
|
||||||
contentDescription = "Arrow down Icon",
|
contentDescription = "Arrow down Icon",
|
||||||
)
|
)
|
||||||
|
var maxTemp = numberFormat.format(forecast.minTemp.roundToInt()) + "°"
|
||||||
|
var minTemp = numberFormat.format(forecast.maxTemp.roundToInt()) + "°"
|
||||||
Text(
|
Text(
|
||||||
text = numberFormat.format(forecast.minTemp.roundToInt()) + "°",
|
text = maxTemp,
|
||||||
fontSize = fontSizeUpper,
|
fontSize = fontSizeUpper,
|
||||||
fontWeight = breezeFontWeight,
|
fontWeight = breezeFontWeight,
|
||||||
)
|
)
|
||||||
|
@ -217,17 +231,34 @@ fun WeatherInfo(modifier: Modifier = Modifier) {
|
||||||
contentDescription = "Arrow up Icon",
|
contentDescription = "Arrow up Icon",
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = numberFormat.format(forecast.maxTemp.roundToInt()) + "°",
|
text = minTemp,
|
||||||
|
fontSize = fontSizeUpper,
|
||||||
|
fontWeight = breezeFontWeight,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Text(
|
||||||
|
text = weatherHttpExceptionMessage,
|
||||||
fontSize = fontSizeUpper,
|
fontSize = fontSizeUpper,
|
||||||
fontWeight = breezeFontWeight,
|
fontWeight = breezeFontWeight,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (hasLocationPermission) {
|
if (hasLocationPermission) {
|
||||||
location?.let {
|
location?.let {
|
||||||
|
try {
|
||||||
currentTemp = fetchCurrentTemp(ctx, it.latitude, it.latitude)
|
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,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue