Skip to content

Commit

Permalink
#1 - State hoisting
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Jan 20, 2023
1 parent db7c1f0 commit bce72b6
Showing 1 changed file with 68 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ package jik.compose.basicscodelab
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ElevatedButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -31,7 +24,57 @@ class MainActivity : ComponentActivity() {
}

@Composable
private fun MyApp(
private fun MyApp(modifier: Modifier = Modifier) {
var shouldShowOnboarding by remember { mutableStateOf(true) }

Surface(modifier) {
if (shouldShowOnboarding) {
OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false})
} else {
Greetings()
}
}
}

@Preview
@Composable
private fun MyAppPreview() {
BasicsCodelabTheme {
MyApp(Modifier.fillMaxSize())
}
}

@Composable
fun OnboardingScreen(
onContinueClicked: () -> Unit,
modifier: Modifier = Modifier,
) {

Column(
modifier = modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text("Welcome to the Basics Codelab!")
Button(
modifier = Modifier.padding(vertical = 24.dp),
onClick = { onContinueClicked() },
) {
Text("Continue")
}
}
}

@Preview(showBackground = true, widthDp = 320, heightDp = 320)
@Composable
fun OnboardingPreview() {
BasicsCodelabTheme {
OnboardingScreen(onContinueClicked = {})
}
}

@Composable
private fun Greetings(
modifier: Modifier = Modifier,
names: List<String> = listOf("World", "compose")
) {
Expand All @@ -42,6 +85,14 @@ private fun MyApp(
}
}

@Preview(showBackground = true, widthDp = 320)
@Composable
fun GreetingsPreview() {
BasicsCodelabTheme {
Greetings()
}
}

@Composable
fun Greeting(name: String) {
val expanded = remember { mutableStateOf(false) }
Expand All @@ -52,9 +103,11 @@ fun Greeting(name: String) {
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
) {
Row(modifier = Modifier.padding(24.dp)) {
Column(modifier = Modifier
.weight(1f)
.padding(bottom = extraPadding)) {
Column(
modifier = Modifier
.weight(1f)
.padding(bottom = extraPadding)
) {
Text(text = "Hello,")
Text(text = name)
}
Expand All @@ -64,11 +117,3 @@ fun Greeting(name: String) {
}
}
}

@Preview(showBackground = true, widthDp = 320)
@Composable
fun DefaultPreview() {
BasicsCodelabTheme {
MyApp()
}
}

0 comments on commit bce72b6

Please sign in to comment.