From 90ce5890276fa2d684b31f54a085a6b893032218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Alc=C3=A9rreca?= Date: Sun, 23 Oct 2022 07:42:00 -0700 Subject: [PATCH] solution --- .../ui/location/LocationScreen.kt | 20 ++++++++++--------- .../ui/location/LocationViewModel.kt | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationScreen.kt b/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationScreen.kt index ed77a60..69a0fe2 100644 --- a/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationScreen.kt +++ b/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationScreen.kt @@ -112,7 +112,7 @@ fun LocationScreenForecast( onLocationChange: (String) -> Unit, modifier: Modifier = Modifier ) { - val expandedDays = remember { mutableStateMapOf() } + val expandedDays = rememberSaveable(saver = indexSaver) { mutableStateMapOf() } LocationScreenForecast( forecast = locationForecast, onLocationChange = onLocationChange, @@ -154,11 +154,13 @@ fun LocationScreenForecast( contentDescription = label }) { val index = 0 - WeekForecastRow( - forecast.forecastWeek[index], - expanded = index == expandedDayIndex, - onClick = { onExpandedChanged(if (expandedDayIndex == index) -1 else index) } - ) + forecast.forecastWeek.forEachIndexed { index, forecastDay -> + WeekForecastRow( + forecast.forecastWeek[index], + expanded = index == expandedDayIndex, + onClick = { onExpandedChanged(if (expandedDayIndex == index) -1 else index) } + ) + } } } } @@ -175,7 +177,7 @@ fun TopAppBar( title = { Row(verticalAlignment = Alignment.CenterVertically) { IconButton( - onClick = { onLocationChange("Mountain View") } + onClick = { onLocationChange("Sunnyvale") } ) { Icon( Icons.Default.KeyboardArrowLeft, @@ -183,11 +185,11 @@ fun TopAppBar( ) } Text( - "//TODO", + locationName, modifier = Modifier.padding(horizontal = 32.dp) ) IconButton( - onClick = { onLocationChange("Sunnyvale") } + onClick = { onLocationChange("Mountain View") } ) { Icon( Icons.Default.KeyboardArrowRight, diff --git a/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationViewModel.kt b/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationViewModel.kt index b9f141c..720d490 100644 --- a/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationViewModel.kt +++ b/app/src/main/java/com/google/ads22/speedchallenge/ui/location/LocationViewModel.kt @@ -45,6 +45,7 @@ class LocationViewModel @Inject constructor( val uiState = locationId.flatMapLatest { location -> forecastRepository.getForecast(location) .map { Success(it) } + .catch { emit(Error(it)) } }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), Loading) fun changeLocation(id: String) {