Skip to content

Commit 95f72f2

Browse files
Merge pull request android#293 from googlecodelabs/jv/basics-m3
Update Basics codelab code to M3
2 parents 9c36e9a + 7ea57cb commit 95f72f2

File tree

14 files changed

+263
-180
lines changed

14 files changed

+263
-180
lines changed

BasicsCodelab/app/build.gradle

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,31 @@
1616

1717
plugins {
1818
id 'com.android.application'
19-
id 'kotlin-android'
19+
id 'org.jetbrains.kotlin.android'
2020
}
2121

2222
android {
23-
compileSdkVersion 33
23+
namespace 'com.codelab.basics'
24+
compileSdk 33
2425

2526
defaultConfig {
2627
applicationId "com.codelab.basics"
27-
minSdkVersion 21
28-
targetSdkVersion 33
28+
minSdk 21
29+
targetSdk 33
2930
versionCode 1
3031
versionName "1.0"
3132

3233
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
34+
vectorDrawables {
35+
useSupportLibrary true
36+
}
37+
}
38+
39+
buildTypes {
40+
release {
41+
minifyEnabled false
42+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
43+
}
3344
}
3445
compileOptions {
3546
sourceCompatibility JavaVersion.VERSION_1_8
@@ -43,22 +54,22 @@ android {
4354
compose true
4455
}
4556
composeOptions {
46-
kotlinCompilerExtensionVersion "${composeCompiler_version}"
57+
kotlinCompilerExtensionVersion "1.3.2"
4758
}
4859
}
4960

5061
dependencies {
51-
implementation 'androidx.core:core-ktx:1.9.0'
52-
implementation 'androidx.appcompat:appcompat:1.5.1'
53-
implementation 'com.google.android.material:material:1.6.1'
62+
implementation 'androidx.core:core-ktx:1.7.0'
63+
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
64+
implementation 'androidx.activity:activity-compose:1.5.1'
5465
implementation "androidx.compose.ui:ui:$compose_version"
55-
implementation 'androidx.activity:activity-compose:1.6.0'
56-
implementation "androidx.compose.material:material:$compose_version"
5766
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
67+
implementation 'androidx.compose.material3:material3:1.0.0-SNAPSHOT'
5868
implementation "androidx.compose.material:material-icons-extended:$compose_version"
59-
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
60-
6169
testImplementation 'junit:junit:4.13.2'
6270
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
6371
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
72+
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
73+
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
74+
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
6475
}

BasicsCodelab/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
or implied. See the License for the specific language governing permissions and limitations under
1313
the License.
1414
-->
15-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
16-
package="com.codelab.basics">
15+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
1716

1817
<application
1918
android:allowBackup="true"

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

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ import androidx.compose.foundation.layout.fillMaxSize
3030
import androidx.compose.foundation.layout.padding
3131
import androidx.compose.foundation.lazy.LazyColumn
3232
import androidx.compose.foundation.lazy.items
33-
import androidx.compose.material.Button
34-
import androidx.compose.material.Card
35-
import androidx.compose.material.Icon
36-
import androidx.compose.material.IconButton
37-
import androidx.compose.material.MaterialTheme
38-
import androidx.compose.material.Surface
39-
import androidx.compose.material.Text
4033
import androidx.compose.material.icons.Icons.Filled
4134
import androidx.compose.material.icons.filled.ExpandLess
4235
import androidx.compose.material.icons.filled.ExpandMore
36+
import androidx.compose.material3.Button
37+
import androidx.compose.material3.Card
38+
import androidx.compose.material3.CardDefaults
39+
import androidx.compose.material3.Icon
40+
import androidx.compose.material3.IconButton
41+
import androidx.compose.material3.MaterialTheme
42+
import androidx.compose.material3.Surface
43+
import androidx.compose.material3.Text
4344
import androidx.compose.runtime.Composable
4445
import androidx.compose.runtime.getValue
4546
import androidx.compose.runtime.mutableStateOf
@@ -52,52 +53,58 @@ import androidx.compose.ui.res.stringResource
5253
import androidx.compose.ui.text.font.FontWeight
5354
import androidx.compose.ui.tooling.preview.Preview
5455
import androidx.compose.ui.unit.dp
55-
import com.codelab.basics.ui.BasicsCodelabTheme
56+
import com.codelab.basics.ui.theme.BasicsCodelabTheme
5657

5758
class MainActivity : ComponentActivity() {
5859
override fun onCreate(savedInstanceState: Bundle?) {
5960
super.onCreate(savedInstanceState)
6061
setContent {
6162
BasicsCodelabTheme {
62-
MyApp()
63+
MyApp(modifier = Modifier.fillMaxSize())
6364
}
6465
}
6566
}
6667
}
6768

6869
@Composable
69-
private fun MyApp() {
70+
fun MyApp(modifier: Modifier = Modifier) {
7071
var shouldShowOnboarding by rememberSaveable { mutableStateOf(true) }
7172

72-
if (shouldShowOnboarding) {
73-
OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false })
74-
} else {
75-
Greetings()
73+
Surface(modifier, color = MaterialTheme.colorScheme.background) {
74+
if (shouldShowOnboarding) {
75+
OnboardingScreen(onContinueClicked = { shouldShowOnboarding = false })
76+
} else {
77+
Greetings()
78+
}
7679
}
7780
}
7881

7982
@Composable
80-
private fun OnboardingScreen(onContinueClicked: () -> Unit) {
81-
Surface {
82-
Column(
83-
modifier = Modifier.fillMaxSize(),
84-
verticalArrangement = Arrangement.Center,
85-
horizontalAlignment = Alignment.CenterHorizontally
83+
fun OnboardingScreen(
84+
onContinueClicked: () -> Unit,
85+
modifier: Modifier = Modifier
86+
) {
87+
Column(
88+
modifier = modifier.fillMaxSize(),
89+
verticalArrangement = Arrangement.Center,
90+
horizontalAlignment = Alignment.CenterHorizontally
91+
) {
92+
Text("Welcome to the Basics Codelab!")
93+
Button(
94+
modifier = Modifier.padding(vertical = 24.dp),
95+
onClick = onContinueClicked
8696
) {
87-
Text("Welcome to the Basics Codelab!")
88-
Button(
89-
modifier = Modifier.padding(vertical = 24.dp),
90-
onClick = onContinueClicked
91-
) {
92-
Text("Continue")
93-
}
97+
Text("Continue")
9498
}
9599
}
96100
}
97101

98102
@Composable
99-
private fun Greetings(names: List<String> = List(1000) { "$it" } ) {
100-
LazyColumn(modifier = Modifier.padding(vertical = 4.dp)) {
103+
private fun Greetings(
104+
modifier: Modifier = Modifier,
105+
names: List<String> = List(1000) { "$it" }
106+
) {
107+
LazyColumn(modifier = modifier.padding(vertical = 4.dp)) {
101108
items(items = names) { name ->
102109
Greeting(name = name)
103110
}
@@ -107,7 +114,9 @@ private fun Greetings(names: List<String> = List(1000) { "$it" } ) {
107114
@Composable
108115
private fun Greeting(name: String) {
109116
Card(
110-
backgroundColor = MaterialTheme.colors.primary,
117+
colors = CardDefaults.cardColors(
118+
containerColor = MaterialTheme.colorScheme.primary
119+
),
111120
modifier = Modifier.padding(vertical = 4.dp, horizontal = 8.dp)
112121
) {
113122
CardContent(name)
@@ -135,8 +144,7 @@ private fun CardContent(name: String) {
135144
) {
136145
Text(text = "Hello, ")
137146
Text(
138-
text = name,
139-
style = MaterialTheme.typography.h4.copy(
147+
text = name, style = MaterialTheme.typography.headlineMedium.copy(
140148
fontWeight = FontWeight.ExtraBold
141149
)
142150
)
@@ -155,7 +163,6 @@ private fun CardContent(name: String) {
155163
} else {
156164
stringResource(R.string.show_more)
157165
}
158-
159166
)
160167
}
161168
}
@@ -182,3 +189,11 @@ fun OnboardingPreview() {
182189
OnboardingScreen(onContinueClicked = {})
183190
}
184191
}
192+
193+
@Preview
194+
@Composable
195+
fun MyAppPreview() {
196+
BasicsCodelabTheme {
197+
MyApp(Modifier.fillMaxSize())
198+
}
199+
}

BasicsCodelab/app/src/main/java/com/codelab/basics/ui/Shape.kt

Lines changed: 0 additions & 27 deletions
This file was deleted.

BasicsCodelab/app/src/main/java/com/codelab/basics/ui/Theme.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

BasicsCodelab/app/src/main/java/com/codelab/basics/ui/Color.kt renamed to BasicsCodelab/app/src/main/java/com/codelab/basics/ui/theme/Color.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.codelab.basics.ui
17+
package com.codelab.basics.ui.theme
1818

1919
import androidx.compose.ui.graphics.Color
2020

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2021 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.codelab.basics.ui.theme
18+
19+
import android.app.Activity
20+
import android.os.Build
21+
import androidx.compose.foundation.isSystemInDarkTheme
22+
import androidx.compose.material3.MaterialTheme
23+
import androidx.compose.material3.darkColorScheme
24+
import androidx.compose.material3.dynamicDarkColorScheme
25+
import androidx.compose.material3.dynamicLightColorScheme
26+
import androidx.compose.material3.lightColorScheme
27+
import androidx.compose.runtime.Composable
28+
import androidx.compose.runtime.SideEffect
29+
import androidx.compose.ui.graphics.Color
30+
import androidx.compose.ui.graphics.toArgb
31+
import androidx.compose.ui.platform.LocalContext
32+
import androidx.compose.ui.platform.LocalView
33+
import androidx.core.view.ViewCompat
34+
35+
private val DarkColorScheme = darkColorScheme(
36+
surface = Blue,
37+
onSurface = Navy,
38+
primary = Navy,
39+
onPrimary = Chartreuse
40+
)
41+
42+
private val LightColorScheme = lightColorScheme(
43+
surface = Blue,
44+
onSurface = Color.White,
45+
primary = LightBlue,
46+
onPrimary = Navy
47+
)
48+
49+
@Suppress("DEPRECATION")
50+
@Composable
51+
fun BasicsCodelabTheme(
52+
darkTheme: Boolean = isSystemInDarkTheme(),
53+
// Dynamic color is available on Android 12+
54+
dynamicColor: Boolean = true,
55+
content: @Composable () -> Unit
56+
) {
57+
val colorScheme = when {
58+
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
59+
val context = LocalContext.current
60+
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
61+
}
62+
darkTheme -> DarkColorScheme
63+
else -> LightColorScheme
64+
}
65+
val view = LocalView.current
66+
if (!view.isInEditMode) {
67+
SideEffect {
68+
(view.context as Activity).window.statusBarColor = colorScheme.primary.toArgb()
69+
ViewCompat.getWindowInsetsController(view)?.isAppearanceLightStatusBars = darkTheme
70+
}
71+
}
72+
73+
MaterialTheme(
74+
colorScheme = colorScheme,
75+
typography = Typography,
76+
content = content
77+
)
78+
}

0 commit comments

Comments
 (0)