Skip to content

Commit d2eafd9

Browse files
committed
Use tabs to split daily vs. hourly forecasts
1 parent 0f226d6 commit d2eafd9

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

app/src/main/java/io/github/tobyhs/weatherweight/forecast/ForecastScreenContent.kt

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import androidx.compose.foundation.layout.Column
44
import androidx.compose.foundation.layout.padding
55
import androidx.compose.foundation.rememberScrollState
66
import androidx.compose.foundation.verticalScroll
7-
import androidx.compose.material3.HorizontalDivider
7+
import androidx.compose.material3.ScrollableTabRow
8+
import androidx.compose.material3.Tab
89
import androidx.compose.material3.Text
910
import androidx.compose.runtime.Composable
11+
import androidx.compose.runtime.getValue
12+
import androidx.compose.runtime.mutableIntStateOf
13+
import androidx.compose.runtime.remember
14+
import androidx.compose.runtime.setValue
1015
import androidx.compose.ui.Modifier
1116
import androidx.compose.ui.platform.testTag
12-
import androidx.compose.ui.tooling.preview.Preview
17+
import androidx.compose.ui.res.stringResource
18+
import androidx.compose.ui.tooling.preview.PreviewScreenSizes
1319
import androidx.compose.ui.unit.dp
1420

21+
import io.github.tobyhs.weatherweight.R
1522
import io.github.tobyhs.weatherweight.data.model.ForecastResultSet
1623

1724
import java.time.ZoneId
@@ -32,15 +39,31 @@ fun ForecastScreenContent(forecastResultSet: ForecastResultSet) {
3239
modifier = Modifier.testTag("publicationTime"),
3340
)
3441

35-
Column(Modifier.verticalScroll(rememberScrollState())) {
36-
for (forecast in forecastResultSet.dailyForecasts) {
37-
DailyForecastCard(forecast)
42+
var tabIndex by remember { mutableIntStateOf(0) }
43+
val tabTitleResources = listOf(R.string.daily, R.string.hourly)
44+
ScrollableTabRow(tabIndex, modifier = Modifier.padding(top = 4.dp), edgePadding = 0.dp) {
45+
tabTitleResources.forEachIndexed { index, titleRes ->
46+
Tab(
47+
text = { Text(stringResource(titleRes)) },
48+
modifier = Modifier.testTag("forecastTab_${index}"),
49+
selected = tabIndex == index,
50+
onClick = { tabIndex = index },
51+
)
3852
}
53+
}
3954

40-
HorizontalDivider(Modifier.padding(vertical = 8.dp))
41-
42-
for (forecast in forecastResultSet.hourlyForecasts) {
43-
HourlyForecastCard(forecast)
55+
Column(Modifier.verticalScroll(rememberScrollState())) {
56+
when (tabTitleResources[tabIndex]) {
57+
R.string.daily -> {
58+
for (forecast in forecastResultSet.dailyForecasts) {
59+
DailyForecastCard(forecast)
60+
}
61+
}
62+
R.string.hourly -> {
63+
for (forecast in forecastResultSet.hourlyForecasts) {
64+
HourlyForecastCard(forecast)
65+
}
66+
}
4467
}
4568
}
4669
}
@@ -53,7 +76,7 @@ internal val previewDataForecastResultSet = ForecastResultSet(
5376
hourlyForecasts = previewDataHourlyForecasts,
5477
)
5578

56-
@Preview
79+
@PreviewScreenSizes
5780
@Composable
5881
private fun ForecastScreenContentPreview() {
5982
ForecastScreenContent(previewDataForecastResultSet)

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<resources>
22
<string name="app_name">Weather Weight</string>
3+
<string name="daily">Daily</string>
4+
<string name="hourly">Hourly</string>
35
<string name="location">Location</string>
46
<string name="locationSearchHint">Enter Location</string>
57
<string name="submitLocation">Submit Location</string>

app/src/test/java/io/github/tobyhs/weatherweight/forecast/ForecastScreenContentTest.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.compose.ui.test.junit4.ComposeTestRule
55
import androidx.compose.ui.test.junit4.createComposeRule
66
import androidx.compose.ui.test.onAllNodesWithTag
77
import androidx.compose.ui.test.onNodeWithTag
8+
import androidx.compose.ui.test.performClick
89
import androidx.test.ext.junit.runners.AndroidJUnit4
910

1011
import io.github.tobyhs.weatherweight.test.ForecastResultSetFactory
@@ -31,6 +32,18 @@ class ForecastScreenContentTest {
3132
composeRule.onNodeWithTag("publicationTime")
3233
.assertTextEquals("Fri, 1 Feb 2019 12:00:00 GMT")
3334

35+
composeRule.onNodeWithTag("forecastTab_1").performClick()
36+
HourlyForecastCardTest.checkContent(composeRule)
37+
listOf(
38+
"hour" to "12:00",
39+
"hourlyTemperature" to "67",
40+
"hourlyPrecipitationProbability" to "10%",
41+
"hourlyForecastDescription" to "Cloudy",
42+
).forEach { (testTag, expectedText) ->
43+
composeRule.onAllNodesWithTag(testTag)[1].assertTextEquals(expectedText)
44+
}
45+
46+
composeRule.onNodeWithTag("forecastTab_0").performClick()
3447
DailyForecastCardTest.checkContent(composeRule)
3548
listOf(
3649
"dayOfWeek" to "Sat",
@@ -42,16 +55,6 @@ class ForecastScreenContentTest {
4255
).forEach { (testTag, expectedText) ->
4356
composeRule.onAllNodesWithTag(testTag)[1].assertTextEquals(expectedText)
4457
}
45-
46-
HourlyForecastCardTest.checkContent(composeRule)
47-
listOf(
48-
"hour" to "12:00",
49-
"hourlyTemperature" to "67",
50-
"hourlyPrecipitationProbability" to "10%",
51-
"hourlyForecastDescription" to "Cloudy",
52-
).forEach { (testTag, expectedText) ->
53-
composeRule.onAllNodesWithTag(testTag)[1].assertTextEquals(expectedText)
54-
}
5558
}
5659
}
5760
}

0 commit comments

Comments
 (0)