Skip to content

Commit 30f5ef7

Browse files
authored
Migrate to material3-adaptive WindowSizeClass method (#264)
* Migrate to material3-adaptive WindowSizeClass method * Update comments to be single line
1 parent ef29bb6 commit 30f5ef7

File tree

5 files changed

+27
-31
lines changed

5 files changed

+27
-31
lines changed

compose/snippets/build.gradle.kts

+2-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ dependencies {
101101

102102
implementation(libs.androidx.compose.runtime)
103103
implementation(libs.androidx.compose.runtime.livedata)
104-
implementation(libs.androidx.compose.materialWindow)
105104
implementation(libs.androidx.compose.material.iconsExtended)
106105
implementation(libs.androidx.compose.material.ripple)
107106
implementation(libs.androidx.constraintlayout.compose)
@@ -117,6 +116,8 @@ dependencies {
117116
implementation(libs.androidx.glance.appwidget)
118117
implementation(libs.androidx.glance.material3)
119118

119+
implementation(libs.androidx.window.core)
120+
120121
implementation(libs.accompanist.theme.adapter.appcompat)
121122
implementation(libs.accompanist.theme.adapter.material3)
122123
implementation(libs.accompanist.theme.adapter.material)

compose/snippets/src/main/java/com/example/compose/snippets/glance/GlanceSnippets.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ import androidx.glance.material3.ColorProviders
8585
import androidx.glance.text.Text
8686
import androidx.work.CoroutineWorker
8787
import androidx.work.WorkerParameters
88+
import com.example.compose.snippets.MyActivity
8889
import com.example.compose.snippets.R
89-
import com.example.compose.snippets.layouts.MainActivity
9090
import kotlinx.coroutines.flow.StateFlow
9191
import kotlinx.coroutines.launch
9292

@@ -167,11 +167,11 @@ private object CreateUI {
167167
Row(horizontalAlignment = Alignment.CenterHorizontally) {
168168
Button(
169169
text = "Home",
170-
onClick = actionStartActivity<MainActivity>()
170+
onClick = actionStartActivity<MyActivity>()
171171
)
172172
Button(
173173
text = "Work",
174-
onClick = actionStartActivity<MainActivity>()
174+
onClick = actionStartActivity<MyActivity>()
175175
)
176176
}
177177
}
@@ -188,7 +188,7 @@ private object ActionLaunchActivity {
188188
// ..
189189
Button(
190190
text = "Go Home",
191-
onClick = actionStartActivity<MainActivity>()
191+
onClick = actionStartActivity<MyActivity>()
192192
)
193193
}
194194
// [END android_compose_glance_launchactivity]

compose/snippets/src/main/java/com/example/compose/snippets/layouts/AdaptiveLayoutSnippets.kt

+18-24
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,19 @@
1818

1919
package com.example.compose.snippets.layouts
2020

21-
import android.os.Bundle
22-
import androidx.activity.ComponentActivity
23-
import androidx.activity.compose.setContent
2421
import androidx.compose.foundation.layout.BoxWithConstraints
2522
import androidx.compose.foundation.layout.Column
2623
import androidx.compose.foundation.layout.Row
27-
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi
28-
import androidx.compose.material3.windowsizeclass.WindowHeightSizeClass
29-
import androidx.compose.material3.windowsizeclass.WindowSizeClass
30-
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass
24+
import androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi
25+
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
3126
import androidx.compose.runtime.Composable
3227
import androidx.compose.runtime.getValue
3328
import androidx.compose.runtime.mutableStateOf
3429
import androidx.compose.runtime.remember
3530
import androidx.compose.runtime.setValue
3631
import androidx.compose.ui.unit.dp
32+
import androidx.window.core.layout.WindowHeightSizeClass
33+
import androidx.window.core.layout.WindowSizeClass
3734

3835
/*
3936
* Copyright 2023 The Android Open Source Project
@@ -51,25 +48,15 @@ import androidx.compose.ui.unit.dp
5148
* limitations under the License.
5249
*/
5350
// [START android_compose_adaptive_layouts_basic]
54-
class MainActivity : ComponentActivity() {
55-
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class)
56-
override fun onCreate(savedInstanceState: Bundle?) {
57-
super.onCreate(savedInstanceState)
58-
59-
setContent {
60-
val windowSizeClass = calculateWindowSizeClass(this)
61-
MyApp(windowSizeClass)
62-
}
63-
}
64-
}
51+
@OptIn(ExperimentalMaterial3AdaptiveApi::class)
6552
@Composable
66-
fun MyApp(windowSizeClass: WindowSizeClass) {
67-
// Perform logic on the size class to decide whether to show
68-
// the top app bar.
69-
val showTopAppBar = windowSizeClass.heightSizeClass != WindowHeightSizeClass.Compact
53+
fun MyApp(
54+
windowSizeClass: WindowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
55+
) {
56+
// Perform logic on the size class to decide whether to show the top app bar.
57+
val showTopAppBar = windowSizeClass.windowHeightSizeClass != WindowHeightSizeClass.COMPACT
7058

71-
// MyScreen knows nothing about window sizes, and performs logic
72-
// based on a Boolean flag.
59+
// MyScreen knows nothing about window sizes, and performs logic based on a Boolean flag.
7360
MyScreen(
7461
showTopAppBar = showTopAppBar,
7562
/* ... */
@@ -95,6 +82,13 @@ fun AdaptivePane(
9582
}
9683
// [END android_compose_layouts_adaptive_pane]
9784

85+
@Composable
86+
private fun WindowSizeClassSnippet() {
87+
// [START android_compose_windowsizeclass]
88+
val windowSizeClass = currentWindowAdaptiveInfo().windowSizeClass
89+
// [END android_compose_windowsizeclass]
90+
}
91+
9892
@Composable
9993
fun OnePane() {
10094
// your content here

compose/snippets/src/main/java/com/example/compose/snippets/state/StateOverviewSnippets.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import androidx.compose.foundation.layout.padding
2727
import androidx.compose.material3.MaterialTheme
2828
import androidx.compose.material3.OutlinedTextField
2929
import androidx.compose.material3.Text
30-
import androidx.compose.material3.windowsizeclass.WindowSizeClass
3130
import androidx.compose.runtime.Composable
3231
import androidx.compose.runtime.Stable
3332
import androidx.compose.runtime.getValue
@@ -46,6 +45,7 @@ import androidx.compose.ui.res.imageResource
4645
import androidx.compose.ui.text.TextRange
4746
import androidx.compose.ui.text.input.TextFieldValue
4847
import androidx.compose.ui.unit.dp
48+
import androidx.window.core.layout.WindowSizeClass
4949

5050
// [START android_compose_state_overview]
5151
@Composable

gradle/libs.versions.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ androidx-navigation = "2.7.7"
1818
androidx-paging = "3.2.1"
1919
androidx-test = "1.5.0"
2020
androidx-test-espresso = "3.5.1"
21+
androidx-window = "1.3.0-beta02"
2122
androidxHiltNavigationCompose = "1.2.0"
2223
coil = "2.5.0"
2324
# @keep
@@ -63,7 +64,6 @@ androidx-compose-material3-adaptive = { module = "androidx.compose.material3.ada
6364
androidx-compose-material3-adaptive-layout = { module = "androidx.compose.material3.adaptive:adaptive-layout", version.ref = "material3-adaptive" }
6465
androidx-compose-material3-adaptive-navigation = { module = "androidx.compose.material3.adaptive:adaptive-navigation", version.ref = "material3-adaptive" }
6566
androidx-compose-material3-adaptive-navigation-suite = { module = "androidx.compose.material3:material3-adaptive-navigation-suite", version.ref = "material3-adaptive-navigation-suite" }
66-
androidx-compose-materialWindow = { module = "androidx.compose.material3:material3-window-size-class" }
6767
androidx-compose-runtime = { module = "androidx.compose.runtime:runtime" }
6868
androidx-compose-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata" }
6969
androidx-compose-ui = { module = "androidx.compose.ui:ui" }
@@ -98,6 +98,7 @@ androidx-recyclerview = { module = "androidx.recyclerview:recyclerview", version
9898
androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-test" }
9999
androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" }
100100
androidx-test-runner = "androidx.test:runner:1.5.2"
101+
androidx-window-core = { module = "androidx.window:window-core", version.ref = "androidx-window" }
101102
androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.9.0"
102103
coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" }
103104
google-android-material = { module = "com.google.android.material:material", version.ref = "material" }

0 commit comments

Comments
 (0)