Skip to content

Commit

Permalink
[Jetsnack] Implement Navigation Library
Browse files Browse the repository at this point in the history
  • Loading branch information
JolandaVerhoef committed May 19, 2021
1 parent 1dd1113 commit 7748f48
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 73 deletions.
1 change: 1 addition & 0 deletions Jetsnack/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ dependencies {
implementation Libs.AndroidX.coreKtx
implementation Libs.AndroidX.Activity.activityCompose
implementation Libs.AndroidX.Lifecycle.viewModelCompose
implementation Libs.AndroidX.Navigation.navigationCompose
implementation Libs.AndroidX.ConstraintLayout.constraintLayoutCompose

implementation Libs.AndroidX.Compose.runtime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.example.jetsnack

import androidx.activity.OnBackPressedDispatcher
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
Expand All @@ -35,7 +34,7 @@ class AppTest {
@Before
fun setUp() {
composeTestRule.setContent {
JetsnackApp(OnBackPressedDispatcher())
JetsnackApp()
}
}

Expand All @@ -62,6 +61,12 @@ class AppTest {

// Navigate to Profile
composeTestRule.onNodeWithText("PROFILE").performClick().assertIsDisplayed()
composeTestRule.onNodeWithText("Profile").assertIsDisplayed()
composeTestRule.onNodeWithText("This is currently work in progress").assertIsDisplayed()
}

@Test
fun app_canNavigateToDetailPage() {
composeTestRule.onNodeWithText("Chips").performClick()
composeTestRule.onNodeWithText("Lorem ipsum", substring = true).assertIsDisplayed()
}
}
29 changes: 6 additions & 23 deletions Jetsnack/app/src/main/java/com/example/jetsnack/ui/JetsnackApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,19 @@

package com.example.jetsnack.ui

import androidx.activity.OnBackPressedDispatcher
import androidx.compose.animation.Crossfade
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import com.example.jetsnack.ui.home.Home
import com.example.jetsnack.ui.snackdetail.SnackDetail
import androidx.navigation.compose.rememberNavController
import com.example.jetsnack.ui.theme.JetsnackTheme
import com.example.jetsnack.ui.utils.Navigator
import com.google.accompanist.insets.ProvideWindowInsets

@Composable
fun JetsnackApp(backDispatcher: OnBackPressedDispatcher) {
val navigator: Navigator<Destination> = rememberSaveable(
saver = Navigator.saver(backDispatcher)
) {
Navigator(Destination.Home, backDispatcher)
}
val actions = remember(navigator) { Actions(navigator) }
fun JetsnackApp() {
ProvideWindowInsets {
JetsnackTheme {
Crossfade(navigator.current) { destination ->
when (destination) {
Destination.Home -> Home(actions.selectSnack)
is Destination.SnackDetail -> SnackDetail(
snackId = destination.snackId,
upPress = actions.upPress
)
}
}
val navController = rememberNavController()
JetsnackNavGraph(
navController = navController
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2021 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.example.jetsnack.ui

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
import androidx.navigation.compose.navigate
import androidx.navigation.compose.rememberNavController
import com.example.jetsnack.ui.MainDestinations.SNACK_ID_KEY
import com.example.jetsnack.ui.home.Home
import com.example.jetsnack.ui.snackdetail.SnackDetail

/**
* Destinations used in the ([JetsnackApp]).
*/
object MainDestinations {
const val HOME_ROUTE = "home"
const val SNACK_DETAIL_ROUTE = "snack"
const val SNACK_ID_KEY = "snackId"
}

@Composable
fun JetsnackNavGraph(
navController: NavHostController = rememberNavController(),
startDestination: String = MainDestinations.HOME_ROUTE
) {
val actions = remember(navController) { MainActions(navController) }
NavHost(
navController = navController,
startDestination = startDestination
) {
composable(MainDestinations.HOME_ROUTE) {
Home(
onSnackSelected = actions.navigateToSnackDetail
)
}
composable(
"${MainDestinations.SNACK_DETAIL_ROUTE}/{$SNACK_ID_KEY}",
arguments = listOf(navArgument(SNACK_ID_KEY) { type = NavType.LongType })
) { backStackEntry ->
val arguments = requireNotNull(backStackEntry.arguments)
val snackId = arguments.getLong(SNACK_ID_KEY)
SnackDetail(
snackId = snackId,
upPress = actions.upPress
)
}
}
}

/**
* Models the navigation actions in the app.
*/
class MainActions(navController: NavHostController) {
val navigateToSnackDetail: (Long) -> Unit = { snackId: Long ->
navController.navigate("${MainDestinations.SNACK_DETAIL_ROUTE}/$snackId")
}
val upPress: () -> Unit = {
navController.navigateUp()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MainActivity : ComponentActivity() {
setContent {
val systemUiController = remember { SystemUiController(window) }
CompositionLocalProvider(LocalSysUiController provides systemUiController) {
JetsnackApp(onBackPressedDispatcher)
JetsnackApp()
}
}
}
Expand Down
46 changes: 0 additions & 46 deletions Jetsnack/app/src/main/java/com/example/jetsnack/ui/NavGraph.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ object Libs {
"androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha05"
}

object Navigation {
const val navigationCompose = "androidx.navigation:navigation-compose:1.0.0-alpha10"
}

object ConstraintLayout {
const val constraintLayoutCompose =
"androidx.constraintlayout:constraintlayout-compose:1.0.0-alpha07"
Expand Down

0 comments on commit 7748f48

Please sign in to comment.