Skip to content

Commit 9cf1f16

Browse files
Merge pull request #406 from android/fm/basics_new_ui
[Basics] Update to use new AS UI. Fixing bugs
2 parents 95c5bed + 970625d commit 9cf1f16

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

BasicsCodelab/app/src/main/java/com/codelab/basics/MainActivity.kt

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import androidx.compose.material3.Text
4444
import androidx.compose.runtime.Composable
4545
import androidx.compose.runtime.getValue
4646
import androidx.compose.runtime.mutableStateOf
47-
import androidx.compose.runtime.remember
4847
import androidx.compose.runtime.saveable.rememberSaveable
4948
import androidx.compose.runtime.setValue
5049
import androidx.compose.ui.Alignment
@@ -68,9 +67,10 @@ class MainActivity : ComponentActivity() {
6867

6968
@Composable
7069
fun MyApp(modifier: Modifier = Modifier) {
70+
7171
var shouldShowOnboarding by rememberSaveable { mutableStateOf(true) }
7272

73-
Surface(modifier, color = MaterialTheme.colorScheme.background) {
73+
Surface(modifier) {
7474
if (shouldShowOnboarding) {
7575
OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false })
7676
} else {
@@ -84,19 +84,22 @@ fun OnboardingScreen(
8484
onContinueClicked: () -> Unit,
8585
modifier: Modifier = Modifier
8686
) {
87+
8788
Column(
8889
modifier = modifier.fillMaxSize(),
8990
verticalArrangement = Arrangement.Center,
9091
horizontalAlignment = Alignment.CenterHorizontally
9192
) {
9293
Text("Welcome to the Basics Codelab!")
9394
Button(
94-
modifier = Modifier.padding(vertical = 24.dp),
95+
modifier = Modifier
96+
.padding(vertical = 24.dp),
9597
onClick = onContinueClicked
9698
) {
9799
Text("Continue")
98100
}
99101
}
102+
100103
}
101104

102105
@Composable
@@ -112,20 +115,20 @@ private fun Greetings(
112115
}
113116

114117
@Composable
115-
private fun Greeting(name: String) {
118+
private fun Greeting(name: String, modifier: Modifier = Modifier) {
116119
Card(
117120
colors = CardDefaults.cardColors(
118121
containerColor = MaterialTheme.colorScheme.primary
119122
),
120-
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
123+
modifier = modifier.padding(vertical = 4.dp, horizontal = 8.dp)
121124
) {
122125
CardContent(name)
123126
}
124127
}
125128

126129
@Composable
127130
private fun CardContent(name: String) {
128-
var expanded by remember { mutableStateOf(false) }
131+
var expanded by rememberSaveable { mutableStateOf(false) }
129132

130133
Row(
131134
modifier = Modifier
@@ -151,7 +154,7 @@ private fun CardContent(name: String) {
151154
if (expanded) {
152155
Text(
153156
text = ("Composem ipsum color sit lazy, " +
154-
"padding theme elit, sed do bouncy. ").repeat(4),
157+
"padding theme elit, sed do bouncy. ").repeat(4),
155158
)
156159
}
157160
}
@@ -168,27 +171,29 @@ private fun CardContent(name: String) {
168171
}
169172
}
170173

174+
175+
@Preview(showBackground = true, widthDp = 320, heightDp = 320)
176+
@Composable
177+
fun OnboardingPreview() {
178+
BasicsCodelabTheme {
179+
OnboardingScreen(onContinueClicked = {}) // Do nothing on click.
180+
}
181+
}
182+
171183
@Preview(
172184
showBackground = true,
173185
widthDp = 320,
174186
uiMode = UI_MODE_NIGHT_YES,
175-
name = "DefaultPreviewDark"
187+
name = "GreetingPreviewDark"
176188
)
177189
@Preview(showBackground = true, widthDp = 320)
178190
@Composable
179-
fun DefaultPreview() {
191+
fun GreetingPreview() {
180192
BasicsCodelabTheme {
181193
Greetings()
182194
}
183195
}
184196

185-
@Preview(showBackground = true, widthDp = 320, heightDp = 320)
186-
@Composable
187-
fun OnboardingPreview() {
188-
BasicsCodelabTheme {
189-
OnboardingScreen(onContinueClicked = {})
190-
}
191-
}
192197

193198
@Preview
194199
@Composable

0 commit comments

Comments
 (0)