A fully functional Android app built entirely with Kotlin and Jetpack Compose
Merge remote-tracking branch 'github/main' into jul22automerger
Jolanda Verhoef37ad3e629992
diff --cc app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NavigationTest.ktindex b68211eb1,b55c9b953..90d5d92dc--- 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@@@ -46,19 -46,19 +46,19 @@@ class NavigationTest@get:Rule(order = 0)val hiltRule = HiltAndroidRule(this)-- /**-- * Use the primary activity to initialize the app normally.-- */-- @get:Rule(order = 1)-- val composeTestRule = createAndroidComposeRule<MainActivity>()--/*** Create a temporary folder used to create a Data Store file. This guarantees that* the file is removed in between each test, preventing a crash.*/-- @BindValue @get:Rule(order = 2)++ @BindValue @get:Rule(order = 1)val tmpFolder: TemporaryFolder = TemporaryFolder.builder().assureDeletion().build()++ /**++ * Use the primary activity to initialize the app normally.++ */++ @get:Rule(order = 2)++ val composeTestRule = createAndroidComposeRule<MainActivity>()++// The strings used for matching in these testsprivate lateinit var done: Stringprivate lateinit var navigateUp: Stringdiff --cc app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NiaAppStateTest.ktindex 000000000,597c17bcf..2814fdbc9mode 000000,100644..100644--- a/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NiaAppStateTest.kt+++ b/app/src/androidTest/java/com/google/samples/apps/nowinandroid/ui/NiaAppStateTest.kt@@@ -1,0 -1,145 +1,146 @@@+ /*+ * Copyright 2022 The Android Open Source Project+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * https://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++ package com.google.samples.apps.nowinandroid.ui++ import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi+ import androidx.compose.material3.windowsizeclass.WindowSizeClass+ import androidx.compose.runtime.Composable+ import androidx.compose.runtime.LaunchedEffect+ import androidx.compose.runtime.remember+ import androidx.compose.ui.platform.LocalContext+ import androidx.compose.ui.test.junit4.createComposeRule+ import androidx.compose.ui.unit.DpSize+ import androidx.compose.ui.unit.dp+ import androidx.navigation.NavHostController+ import androidx.navigation.compose.ComposeNavigator+ import androidx.navigation.compose.composable+ import androidx.navigation.createGraph+ import androidx.navigation.testing.TestNavHostController+ import org.junit.Assert.assertEquals+ import org.junit.Assert.assertFalse+ import org.junit.Assert.assertTrue+ import org.junit.Rule+ import org.junit.Test++ /**+ * Tests [NiaAppState].+ *+ * Note: This could become an unit test if Robolectric is added to the project and the Context+ * is faked.+ */+ @OptIn(ExperimentalMaterial3WindowSizeClassApi::class)+ class NiaAppStateTest {++ @get:Rule+ val composeTestRule = createComposeRule()++ private lateinit var state: NiaAppState++ @Test+ fun niaAppState_currentDestination() {+ var currentDestination: String? = null++ composeTestRule.setContent {+ val navController = rememberTestNavController()+ state = remember(navController) {+ NiaAppState(+ windowSizeClass = getCompactWindowClass(),+ navController = navController+ )+ }++ // Update currentDestination whenever it changes+ currentDestination = state.currentDestination?.route++ // Navigate to destination b once+ LaunchedEffect(Unit) {+ navController.setCurrentDestination("b")+ }+ }++ assertEquals("b", currentDestination)+ }++ @Test+ fun niaAppState_destinations() {+ composeTestRule.setContent {+ state = rememberNiaAppState(getCompactWindowClass())+ }+- assertEquals(2, state.topLevelDestinations.size)++ assertEquals(3, state.topLevelDestinations.size)+ assertTrue(state.topLevelDestinations[0].destination.contains("for_you"))- assertTrue(state.topLevelDestinations[1].destination.contains("interests"))++ assertTrue(state.topLevelDestinations[1].destination.contains("bookmarks"))++ assertTrue(state.topLevelDestinations[2].destination.contains("interests"))+ }++ @Test+ fun niaAppState_showBottomBar_compact() {+ composeTestRule.setContent {+ state = NiaAppState(+ windowSizeClass = getCompactWindowClass(),+ navController = NavHostController(LocalContext.current)+ )+ }++ assertTrue(state.shouldShowBottomBar)+ assertFalse(state.shouldShowNavRail)+ }++ @Test+ fun niaAppState_showNavRail_medium() {+ composeTestRule.setContent {+ state = NiaAppState(+ windowSizeClass = WindowSizeClass.calculateFromSize(DpSize(800.dp, 800.dp)),+ navController = NavHostController(LocalContext.current)+ )+ }++ assertTrue(state.shouldShowNavRail)+ assertFalse(state.shouldShowBottomBar)+ }++ @Test+ fun niaAppState_showNavRail_large() {+ composeTestRule.setContent {+ state = NiaAppState(+ windowSizeClass = WindowSizeClass.calculateFromSize(DpSize(900.dp, 1200.dp)),+ navController = NavHostController(LocalContext.current)+ )+ }++ assertTrue(state.shouldShowNavRail)+ assertFalse(state.shouldShowBottomBar)+ }++ private fun getCompactWindowClass() = WindowSizeClass.calculateFromSize(DpSize(500.dp, 300.dp))+ }++ @Composable+ private fun rememberTestNavController(): TestNavHostController {+ val context = LocalContext.current+ val navController = remember {+ TestNavHostController(context).apply {+ navigatorProvider.addNavigator(ComposeNavigator())+ graph = createGraph(startDestination = "a") {+ composable("a") { }+ composable("b") { }+ composable("c") { }+ }+ }+ }+ return navController+ }diff --cc app/src/main/java/com/google/samples/apps/nowinandroid/navigation/NiaNavHost.ktindex 76e28ce22,3fb189b1e..92c0000e2--- a/app/src/main/java/com/google/samples/apps/nowinandroid/navigation/NiaNavHost.kt+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/navigation/NiaNavHost.kt@@@ -21,10 -21,9 +21,10 @@@ import androidx.compose.runtime.Composaimport androidx.compose.ui.Modifierimport androidx.navigation.NavHostControllerimport androidx.navigation.compose.NavHost- import androidx.navigation.compose.rememberNavController+ import com.google.samples.apps.nowinandroid.core.navigation.NiaNavigationDestinationimport com.google.samples.apps.nowinandroid.feature.author.navigation.AuthorDestinationimport com.google.samples.apps.nowinandroid.feature.author.navigation.authorGraph+import com.google.samples.apps.nowinandroid.feature.bookmarks.navigation.bookmarksGraphimport com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouDestinationimport com.google.samples.apps.nowinandroid.feature.foryou.navigation.forYouGraphimport com.google.samples.apps.nowinandroid.feature.interests.navigation.interestsGraph@@@ -53,13 -54,20 +55,21 @@@ fun NiaNavHostforYouGraph(windowSizeClass = windowSizeClass)+ bookmarksGraph(windowSizeClass)interestsGraph(- navigateToTopic = { navController.navigate("${TopicDestination.route}/$it") },- navigateToAuthor = { navController.navigate("${AuthorDestination.route}/$it") },+ navigateToTopic = {+ onNavigateToDestination(+ TopicDestination, TopicDestination.createNavigationRoute(it)+ )+ },+ navigateToAuthor = {+ onNavigateToDestination(+ AuthorDestination, AuthorDestination.createNavigationRoute(it)+ )+ },nestedGraphs = {- topicGraph(onBackClick = { navController.popBackStack() })- authorGraph(onBackClick = { navController.popBackStack() })+ topicGraph(onBackClick)+ authorGraph(onBackClick)})}diff --cc app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.ktindex 000000000,076d8d762..dc620490amode 000000,100644..100644--- a/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt+++ b/app/src/main/java/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt@@@ -1,0 -1,146 +1,155 @@@+ /*+ * Copyright 2022 The Android Open Source Project+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * https://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++ package com.google.samples.apps.nowinandroid.ui++ import androidx.compose.material3.windowsizeclass.WindowHeightSizeClass+ import androidx.compose.material3.windowsizeclass.WindowSizeClass+ import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass+ import androidx.compose.runtime.Composable+ import androidx.compose.runtime.Stable+ import androidx.compose.runtime.remember+ import androidx.navigation.NavController+ import androidx.navigation.NavDestination+ import androidx.navigation.NavGraph.Companion.findStartDestination+ import androidx.navigation.NavHostController+ import androidx.navigation.compose.currentBackStackEntryAsState+ import androidx.navigation.compose.rememberNavController+ import androidx.tracing.trace+ import com.google.samples.apps.nowinandroid.core.designsystem.icon.Icon.DrawableResourceIcon+ import com.google.samples.apps.nowinandroid.core.designsystem.icon.Icon.ImageVectorIcon+ import com.google.samples.apps.nowinandroid.core.designsystem.icon.NiaIcons+ import com.google.samples.apps.nowinandroid.core.navigation.NiaNavigationDestination+ import com.google.samples.apps.nowinandroid.core.ui.JankMetricDisposableEffect++import com.google.samples.apps.nowinandroid.feature.bookmarks.R as bookmarksR++import com.google.samples.apps.nowinandroid.feature.bookmarks.navigation.BookmarksDestination+ import com.google.samples.apps.nowinandroid.feature.foryou.R as forYouR+ import com.google.samples.apps.nowinandroid.feature.foryou.navigation.ForYouDestination+ import com.google.samples.apps.nowinandroid.feature.interests.R as interestsR+ import com.google.samples.apps.nowinandroid.feature.interests.navigation.InterestsDestination+ import com.google.samples.apps.nowinandroid.navigation.TopLevelDestination++ @Composable+ fun rememberNiaAppState(+ windowSizeClass: WindowSizeClass,+ navController: NavHostController = rememberNavController()+ ): NiaAppState {+ NavigationTrackingSideEffect(navController)+ return remember(navController, windowSizeClass) {+ NiaAppState(navController, windowSizeClass)+ }+ }++ @Stable+ class NiaAppState(+ val navController: NavHostController,+ val windowSizeClass: WindowSizeClass+ ) {+ val currentDestination: NavDestination?+ @Composable get() = navController+ .currentBackStackEntryAsState().value?.destination++ val shouldShowBottomBar: Boolean+ get() = windowSizeClass.widthSizeClass == WindowWidthSizeClass.Compact ||+ windowSizeClass.heightSizeClass == WindowHeightSizeClass.Compact++ val shouldShowNavRail: Boolean+ get() = !shouldShowBottomBar++ /**+ * Top level destinations to be used in the BottomBar and NavRail+ */+ val topLevelDestinations: List<TopLevelDestination> = listOf(+ TopLevelDestination(+ route = ForYouDestination.route,+ destination = ForYouDestination.destination,+ selectedIcon = DrawableResourceIcon(NiaIcons.Upcoming),+ unselectedIcon = DrawableResourceIcon(NiaIcons.UpcomingBorder),+ iconTextId = forYouR.string.for_you+ ),++ TopLevelDestination(++ route = BookmarksDestination.route,++ destination = BookmarksDestination.destination,++ selectedIcon = DrawableResourceIcon(NiaIcons.Bookmarks),++ unselectedIcon = DrawableResourceIcon(NiaIcons.BookmarksBorder),++ iconTextId = bookmarksR.string.saved++ ),+ TopLevelDestination(+ route = InterestsDestination.route,+ destination = InterestsDestination.destination,+ selectedIcon = ImageVectorIcon(NiaIcons.Grid3x3),+ unselectedIcon = ImageVectorIcon(NiaIcons.Grid3x3),+ iconTextId = interestsR.string.interests+ )+ )++ /**+ * UI logic for navigating to a particular destination in the app. The NavigationOptions to+ * navigate with are based on the type of destination, which could be a top level destination or+ * just a regular destination.+ *+ * Top level destinations have only one copy of the destination of the back stack, and save and+ * restore state whenever you navigate to and from it.+ * Regular destinations can have multiple copies in the back stack and state isn't saved nor+ * restored.+ *+ * @param destination: The [NiaNavigationDestination] the app needs to navigate to.+ * @param route: Optional route to navigate to in case the destination contains arguments.+ */+ fun navigate(destination: NiaNavigationDestination, route: String? = null) {+ trace("Navigation: $destination") {+ if (destination is TopLevelDestination) {+ navController.navigate(route ?: destination.route) {+ // Pop up to the start destination of the graph to+ // avoid building up a large stack of destinations+ // on the back stack as users select items+ popUpTo(navController.graph.findStartDestination().id) {+ saveState = true+ }+ // Avoid multiple copies of the same destination when+ // reselecting the same item+ launchSingleTop = true+ // Restore state when reselecting a previously selected item+ restoreState = true+ }+ } else {+ navController.navigate(route ?: destination.route)+ }+ }+ }++ fun onBackClick() {+ navController.popBackStack()+ }+ }++ /**+ * Stores information about navigation events to be used with JankStats+ */+ @Composable+ private fun NavigationTrackingSideEffect(navController: NavHostController) {+ JankMetricDisposableEffect(navController) { metricsHolder ->+ val listener = NavController.OnDestinationChangedListener { _, destination, _ ->+ metricsHolder.state?.addState("Navigation", destination.route.toString())+ }++ navController.addOnDestinationChangedListener(listener)++ onDispose {+ navController.removeOnDestinationChangedListener(listener)+ }+ }+ }diff --cc feature-bookmarks/src/main/res/values/strings.xmlindex 10f83cae0,000000000..bf59e2ec8mode 100644,000000..100644--- a/feature-bookmarks/src/main/res/values/strings.xml+++ b/feature-bookmarks/src/main/res/values/strings.xml@@@ -1,22 -1,0 +1,23 @@@+<?xml version="1.0" encoding="utf-8"?>+<!--+ Copyright 2022 The Android Open Source Project++ Licensed under the Apache License, Version 2.0 (the "License");+ you may not use this file except in compliance with the License.+ You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->+<resources>++ <string name="saved">Saved</string>+ <string name="saved_loading">Loading saved…</string>+ <string name="top_app_bar_title_saved">Saved</string>+ <string name="top_app_bar_action_search">Search</string>+ <string name="top_app_bar_action_menu">Menu</string>+</resources>diff --cc feature-foryou/src/main/res/values/strings.xmlindex fb8b02307,fb8b02307..7ed3683d9--- a/feature-foryou/src/main/res/values/strings.xml+++ b/feature-foryou/src/main/res/values/strings.xml@@@ -17,7 -17,7 +17,6 @@@<resources><string name="for_you">For you</string><string name="episodes">Episodes</string>-- <string name="saved">Saved</string><string name="done">Done</string><string name="for_you_loading">Loading for you…</string><string name="navigate_up">Navigate up</string>