From ea9fbad05895b8538e682f7a750ef2cfba32b9d6 Mon Sep 17 00:00:00 2001 From: Ran Nachmany Date: Mon, 7 Mar 2022 12:44:51 +0000 Subject: [PATCH] Adding device class breakopints (http://b/216019537) Adding NavRail for MEDIUM / EXPANDED classes (http://b/216019759) Change-Id: I19a8888de558e1466d186a48f2ea293bb1b8ba4b --- app/build.gradle | 4 +- .../apps/nowinandroid/ui/NavigationTest.kt | 2 + .../samples/apps/nowinandroid/MainActivity.kt | 5 +- .../samples/apps/nowinandroid/ui/NiaApp.kt | 58 +++++++++++++++---- build.gradle | 1 + gradle/libs.versions.toml | 4 ++ settings.gradle | 1 + 7 files changed, 62 insertions(+), 13 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9b2d7bfea..0bf36441e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -121,10 +121,12 @@ dependencies { coreLibraryDesugaring libs.android.desugarJdkLibs implementation libs.androidx.activity.compose - implementation libs.androidx.core.ktx implementation libs.androidx.appcompat + implementation libs.androidx.core.ktx + implementation libs.androidx.compose.material.window implementation libs.androidx.hilt.navigation.compose implementation libs.androidx.navigation.compose + implementation libs.androidx.window.manager implementation libs.material3 implementation libs.hilt.android diff --git a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt index 6d6f2c890..8d6d4b524 100644 --- a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt +++ b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt @@ -16,6 +16,7 @@ package com.google.samples.apps.nowinandroid.ui +import androidx.compose.material.window.ExperimentalMaterialWindowApi import androidx.compose.ui.test.assertIsOn import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithContentDescription @@ -36,6 +37,7 @@ import org.junit.rules.TemporaryFolder /** * Tests all the navigation flows that are handled by the navigation library. */ +@ExperimentalMaterialWindowApi @HiltAndroidTest class NavigationTest { diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt index 4a031f403..53a50719d 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/MainActivity.kt @@ -19,10 +19,13 @@ package com.google.samples.apps.nowinandroid import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.compose.material.window.ExperimentalMaterialWindowApi +import androidx.compose.material.window.rememberSizeClass import androidx.core.view.WindowCompat import com.google.samples.apps.nowinandroid.ui.NiaApp import dagger.hilt.android.AndroidEntryPoint +@ExperimentalMaterialWindowApi @AndroidEntryPoint class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -32,6 +35,6 @@ class MainActivity : ComponentActivity() { // including IME animations WindowCompat.setDecorFitsSystemWindows(window, false) - setContent { NiaApp() } + setContent { NiaApp(rememberSizeClass()) } } } diff --git a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt index 45e9437c6..0a24b2f50 100644 --- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt +++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaApp.kt @@ -17,6 +17,7 @@ package com.google.samples.apps.nowinandroid.ui import androidx.annotation.StringRes +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.captionBarPadding import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.navigationBarsPadding @@ -32,11 +33,15 @@ import androidx.compose.material.icons.outlined.Bookmarks import androidx.compose.material.icons.outlined.Grid3x3 import androidx.compose.material.icons.outlined.Upcoming import androidx.compose.material.ripple.LocalRippleTheme +import androidx.compose.material.window.SizeClass +import androidx.compose.material.window.WidthSizeClass import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.NavigationBar import androidx.compose.material3.NavigationBarItem +import androidx.compose.material3.NavigationRail +import androidx.compose.material3.NavigationRailItem import androidx.compose.material3.Scaffold import androidx.compose.material3.Surface import androidx.compose.material3.Text @@ -56,7 +61,7 @@ import com.google.samples.apps.nowinandroid.core.ui.theme.NiaTheme @OptIn(ExperimentalMaterial3Api::class) @Composable -fun NiaApp() { +fun NiaApp(windowSizeClass: SizeClass) { NiaTheme { val navController = rememberNavController() val navigationActions = remember(navController) { @@ -70,18 +75,49 @@ fun NiaApp() { Scaffold( modifier = Modifier, bottomBar = { - // TODO: Only show on small screens - NiABottomBar(navigationActions, currentRoute) - }, - ) { padding -> - Surface(Modifier.fillMaxSize()) { - NiaNavGraph( - navController = navController, - modifier = Modifier - .statusBarsPadding() - .padding(padding) + if (windowSizeClass.width == WidthSizeClass.Compact) NiABottomBar( + navigationActions = navigationActions, + currentRoute = currentRoute ) } + ) { padding -> + Surface(Modifier.fillMaxSize().statusBarsPadding()) { + Row { + if (windowSizeClass.width != WidthSizeClass.Compact) { + NiANavRail( + navigationActions = navigationActions, + currentRoute = currentRoute + ) + } + NiaNavGraph( + navController = navController, + modifier = Modifier.padding(padding) + ) + } + } + } + } +} + +@Composable +private fun NiANavRail( + navigationActions: NiaNavigationActions, + currentRoute: String +) { + NavigationRail { + TOP_LEVEL_DESTINATIONS.forEach { destination -> + val selected = currentRoute == destination.route + NavigationRailItem( + selected = selected, + onClick = { navigationActions.navigateToTopLevelDestination(destination.route) }, + icon = { + Icon( + if (selected) destination.selectedIcon else destination.unselectedIcon, + contentDescription = null + ) + }, + label = { Text(stringResource(destination.iconTextId)) } + ) } } } diff --git a/build.gradle b/build.gradle index 00eae597a..5149acc5e 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,7 @@ buildscript { repositories { google() mavenCentral() + maven { url 'https://androidx.dev/snapshots/builds/8273139/artifacts/repository' } } dependencies { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6ffcdf222..bd1650f37 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,6 +5,7 @@ androidGradlePlugin = "7.0.3" androidxActivity = "1.4.0" androidxAppCompat = "1.3.0" androidxCompose = "1.2.0-alpha03" +androidxMaterialWindow = "1.2.0-SNAPSHOT" androidxComposeMaterial3 = "1.0.0-alpha03" androidxCore = "1.7.0" androidxDataStore = "1.0.0" @@ -12,6 +13,7 @@ androidxEspresso = "3.3.0" androidxHiltNavigationCompose = "1.0.0-rc01" androidxLifecycle = "2.4.0" androidxNavigation = "2.4.0-rc01" +androidxWindowManager = "1.0.0" androidxTest = "1.4.0" androidxTestExt = "1.1.2" hilt = "2.41" @@ -42,6 +44,7 @@ androidx-activity-compose = { group = "androidx.activity", name = "activity-comp androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" } androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout", version.ref = "androidxCompose" } androidx-compose-material = { group = "androidx.compose.material", name = "material", version.ref = "androidxCompose" } +androidx-compose-material-window = {group = "androidx.compose.material", name = "material-window", version.ref="androidxMaterialWindow"} androidx-compose-material-iconsExtended = { group = "androidx.compose.material", name = "material-icons-extended", version.ref = "androidxCompose" } androidx-compose-material3 = { group = "androidx.compose.material3", name = "material3", version.ref = "androidxComposeMaterial3" } androidx-compose-runtime = { group = "androidx.compose.runtime", name = "runtime", version.ref = "androidxCompose" } @@ -56,6 +59,7 @@ androidx-dataStore = { group = "androidx.datastore", name = "datastore", version androidx-hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "androidxHiltNavigationCompose" } androidx-lifecycle-viewModelCompose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "androidxLifecycle" } androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "androidxNavigation" } +androidx-window-manager = {module = "androidx.window:window", version.ref = "androidxWindowManager"} androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidxTest" } androidx-test-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "androidxEspresso" } androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidxTest" } diff --git a/settings.gradle b/settings.gradle index 91016d1bd..8ed4c0d95 100644 --- a/settings.gradle +++ b/settings.gradle @@ -24,6 +24,7 @@ dependencyResolutionManagement { google() mavenCentral() gradlePluginPortal() + maven { url 'https://androidx.dev/snapshots/builds/8273139/artifacts/repository' } } } rootProject.name = "nowinandroid"