-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
185 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/yapp/chaeum/navigation/HomeNavHost.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.yapp.chaeum.navigation | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.NavHost | ||
import com.example.etc.navigation.etcScreen | ||
import com.example.matching.navigation.MatchingGraph | ||
import com.example.matching.navigation.matchingNavGraph | ||
import com.example.mypage.navigation.myPageScreen | ||
|
||
@Composable | ||
fun HomeNavHost( | ||
navController: NavHostController, | ||
modifier: Modifier = Modifier, | ||
) { | ||
NavHost( | ||
navController = navController, | ||
startDestination = MatchingGraph, | ||
modifier = modifier, | ||
) { | ||
matchingNavGraph() | ||
myPageScreen() | ||
etcScreen() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,11 @@ | ||
package com.yapp.chaeum.ui | ||
|
||
import androidx.compose.foundation.layout.navigationBarsPadding | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.BottomNavigation | ||
import androidx.compose.material.BottomNavigationItem | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.Text | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.NavDestination | ||
import androidx.navigation.NavDestination.Companion.hierarchy | ||
import androidx.navigation.compose.currentBackStackEntryAsState | ||
import com.yapp.chaeum.navigation.AppNavHost | ||
import com.yapp.chaeum.navigation.TopLevelDestination | ||
import kotlin.reflect.KClass | ||
|
||
@Composable | ||
fun App(appState: AppState = rememberAppState()) { | ||
val navBackStackEntry by appState.navController.currentBackStackEntryAsState() | ||
val currentDestination = navBackStackEntry?.destination | ||
|
||
// 현재 화면이 TopLevelDestination 중 하나인지 확인 | ||
val isTopLevel = TopLevelDestination.entries.any { | ||
currentDestination?.route == it.route.qualifiedName | ||
} | ||
|
||
Scaffold( | ||
bottomBar = { | ||
if (isTopLevel) { | ||
BottomNavigation( | ||
modifier = Modifier.navigationBarsPadding() | ||
) { | ||
TopLevelDestination.entries.forEach { topLevelRoute -> | ||
BottomNavigationItem( | ||
icon = { | ||
Icon( | ||
topLevelRoute.selectedIcon, | ||
contentDescription = topLevelRoute.name | ||
) | ||
}, | ||
label = { Text(topLevelRoute.name) }, | ||
selected = currentDestination?.hierarchy?.any { destination -> | ||
destination.route == topLevelRoute.route.qualifiedName | ||
} == true, | ||
onClick = { | ||
appState.navigateToTopLevelDestination(topLevelRoute) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
) { innerPadding -> | ||
AppNavHost( | ||
appState = appState, | ||
modifier = Modifier.padding(innerPadding) | ||
) | ||
} | ||
AppNavHost( | ||
appState = appState, | ||
) | ||
} | ||
|
||
private fun NavDestination.hasRoute(routeClass: KClass<*>): Boolean { | ||
return this.route == routeClass.qualifiedName | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.yapp.chaeum.ui | ||
|
||
import androidx.compose.foundation.layout.navigationBarsPadding | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.BottomNavigation | ||
import androidx.compose.material.BottomNavigationItem | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.Text | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.navigation.NavDestination.Companion.hierarchy | ||
import androidx.navigation.compose.currentBackStackEntryAsState | ||
import com.yapp.chaeum.navigation.HomeNavHost | ||
import com.yapp.chaeum.navigation.TopLevelDestination | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data object HomeGraph | ||
|
||
@Composable | ||
fun HomeRoute( | ||
homeState: HomeState = rememberHomeState() | ||
) { | ||
Scaffold( | ||
bottomBar = { | ||
HomeBottomBar(homeState) | ||
} | ||
) { innerPadding -> | ||
HomeNavHost( | ||
navController = homeState.navController, | ||
modifier = Modifier.padding(innerPadding) | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun HomeBottomBar( | ||
homeState: HomeState, | ||
) { | ||
val navBackStackEntry by homeState.navController.currentBackStackEntryAsState() | ||
val currentDestination = navBackStackEntry?.destination | ||
BottomNavigation( | ||
modifier = Modifier.navigationBarsPadding() | ||
) { | ||
TopLevelDestination.entries.forEach { topLevelRoute -> | ||
BottomNavigationItem( | ||
icon = { | ||
Icon( | ||
imageVector = topLevelRoute.selectedIcon, | ||
contentDescription = topLevelRoute.name | ||
) | ||
}, | ||
label = { Text(topLevelRoute.name) }, | ||
selected = currentDestination?.hierarchy?.any { destination -> | ||
destination.route == topLevelRoute.route.qualifiedName | ||
} == true, | ||
onClick = { | ||
homeState.navigateToTopLevelDestination(topLevelRoute) | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.yapp.chaeum.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.navigation.NavGraph.Companion.findStartDestination | ||
import androidx.navigation.NavHostController | ||
import androidx.navigation.compose.rememberNavController | ||
import androidx.navigation.navOptions | ||
import com.example.etc.navigation.navigateToEtc | ||
import com.example.matching.navigation.navigateToMatching | ||
import com.example.mypage.navigation.navigateToMyPage | ||
import com.yapp.chaeum.navigation.TopLevelDestination | ||
|
||
@Composable | ||
fun rememberHomeState( | ||
navController: NavHostController = rememberNavController(), | ||
): HomeState { | ||
return remember(navController) { | ||
HomeState(navController = navController) | ||
} | ||
} | ||
|
||
class HomeState( | ||
val navController: NavHostController, | ||
) { | ||
fun navigateToTopLevelDestination(topLevelDestination: TopLevelDestination) { | ||
val topLevelNavOptions = navOptions { | ||
popUpTo(navController.graph.findStartDestination().id) { | ||
saveState = true | ||
} | ||
launchSingleTop = true | ||
restoreState = true | ||
} | ||
|
||
when (topLevelDestination) { | ||
TopLevelDestination.MATCHING -> navController.navigateToMatching(topLevelNavOptions) | ||
TopLevelDestination.MYPAGE -> navController.navigateToMyPage(topLevelNavOptions) | ||
TopLevelDestination.ETC -> navController.navigateToEtc(topLevelNavOptions) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.