Skip to content

Commit

Permalink
#8 - Integrate RallyTabRow with navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Feb 23, 2023
1 parent f59b1e8 commit c72dff9
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ package com.example.compose.rally
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.compose.rally.ui.components.RallyTabRow
import com.example.compose.rally.ui.theme.RallyTheme
Expand All @@ -50,13 +49,19 @@ class RallyActivity : ComponentActivity() {
@Composable
fun RallyApp() {
RallyTheme {
var currentScreen: RallyDestination by remember { mutableStateOf(Overview) }
val navController = rememberNavController()

val currentBackStack by navController.currentBackStackEntryAsState()
val currentDestination = currentBackStack?.destination

val currentScreen = rallyTabRowScreens.find { it.route == currentDestination?.route } ?: Overview
Scaffold(
topBar = {
RallyTabRow(
allScreens = rallyTabRowScreens,
onTabSelected = { screen -> currentScreen = screen },
onTabSelected = { newScreen ->
navController.navigateSingleTopTo(newScreen.route)
},
currentScreen = currentScreen
)
}
Expand All @@ -79,3 +84,15 @@ fun RallyApp() {
}
}
}


fun NavHostController.navigateSingleTopTo(route: String) =
this.navigate(route) {
popUpTo(
this@navigateSingleTopTo.graph.findStartDestination().id
) {
saveState = true
}
launchSingleTop = true
restoreState = true
}

0 comments on commit c72dff9

Please sign in to comment.