Skip to content

Commit

Permalink
[feat] #62 set navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
lsakee authored and Sangwook123 committed Jul 19, 2024
1 parent 8e759be commit 6a75d71
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
5 changes: 2 additions & 3 deletions feature/home/src/main/java/com/record/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ fun HomeRoute(
modifier: Modifier = Modifier,
viewModel: HomeViewModel = hiltViewModel(),
navigateToVideoDetail: (VideoType, Long, String?, Long) -> Unit,
navigateToUpload: () -> Unit = {},
) {
val state by viewModel.uiState.collectAsStateWithLifecycle()

LaunchedEffectWithLifecycle {
viewModel.getVideos()
viewModel.sideEffect.collectLatest { sideEffect ->
when (sideEffect) {
HomeSideEffect.navigateToUpload -> {
}

HomeSideEffect.navigateToUpload -> navigateToUpload()
is HomeSideEffect.navigateToVideo -> {
navigateToVideoDetail(sideEffect.type, sideEffect.id, sideEffect.keyword, 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ fun NavGraphBuilder.homeNavGraph(
padding: PaddingValues,
modifier: Modifier = Modifier,
navigateToVideoDetail: (VideoType, Long, String?, Long) -> Unit,
navigateToUpload: () -> Unit = {},
) {
composable(route = HomeRoute.route) {
HomeRoute(
padding = padding,
modifier = modifier,
navigateToVideoDetail = navigateToVideoDetail,
navigateToUpload = navigateToUpload
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import com.record.login.navigation.LoginRoute
import com.record.login.navigation.SignupRoute
import com.record.model.VideoType
import com.record.mypage.navigation.navigateMypage
import com.record.profile.navigation.ProfileRoute
import com.record.mypage.navigation.navigateToFollower
import com.record.mypage.navigation.navigateToFollowing
import com.record.profile.navigation.navigateProfile
import com.record.setting.navigate.navigateSetting
import com.record.upload.navigation.navigateToUpload
import com.record.video.navigation.navigateVideo
import com.record.video.navigation.navigateVideoDetail

Expand All @@ -26,33 +29,20 @@ internal class MainNavigator(
@Composable get() = navController
.currentBackStackEntryAsState().value?.destination

private var _currentTab: MainNavTab? = null

val currentTab: MainNavTab?
@Composable get() {
val currentRoute = currentDestination?.route
val mainTab = currentRoute?.let(MainNavTab::find)
if (mainTab != null) {
_currentTab = mainTab
}
return _currentTab
}
@Composable get() = currentDestination
?.route
?.let(MainNavTab::find)

fun navigate(tab: MainNavTab) {
val navOptions = navOptions {
popUpTo(HomeRoute.route) {
saveState = true
}
launchSingleTop = true
restoreState = when (tab) {
MainNavTab.HOME -> false
MainNavTab.VIDEO -> true
MainNavTab.MYPAGE -> true
}
restoreState = true
}

_currentTab = tab

when (tab) {
MainNavTab.HOME -> navController.navigateHome(navOptions)
MainNavTab.VIDEO -> navController.navigateVideo(navOptions)
Expand All @@ -78,7 +68,7 @@ internal class MainNavigator(

fun navigateSignUp() {
navController.navigate(SignupRoute.route) {
popUpTo(navController.graph.startDestinationId) {
popUpTo(navController.graph.id) {
inclusive = true
}
}
Expand All @@ -96,6 +86,9 @@ internal class MainNavigator(
userId = userId,
)
}
fun navigateToUpload() {
navController.navigateToUpload()
}

fun navigateToFollowing() {
navController.navigateToFollowing()
Expand All @@ -114,11 +107,8 @@ internal class MainNavigator(
}

fun popBackStackIfNotHome() {
val homeRoute = HomeRoute.route
if (navController.currentDestination?.route == homeRoute) {
if (!isSameCurrentDestination(HomeRoute.route)) {
navController.popBackStack()
} else {
navController.popBackStack(homeRoute, inclusive = false)
}
}

Expand All @@ -128,9 +118,7 @@ internal class MainNavigator(
@Composable
fun shouldShowBottomBar(): Boolean {
val currentRoute = currentDestination?.route ?: return false
return currentRoute in MainNavTab || currentRoute in InMainNavTab || currentRoute.contains("detail") || currentRoute.contains(
ProfileRoute.route,
)
return currentRoute in MainNavTab
}
}

Expand All @@ -139,4 +127,4 @@ internal fun rememberMainNavigator(
navController: NavHostController = rememberNavController(),
): MainNavigator = remember(navController) {
MainNavigator(navController)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.currentBackStackEntryAsState
import com.record.designsystem.component.snackbar.RecordySnackBar
import com.record.designsystem.theme.RecordyTheme
import com.record.home.HomeSideEffect.navigateToUpload
import com.record.home.navigation.homeNavGraph
import com.record.login.navigation.loginNavGraph
import com.record.mypage.navigation.mypageNavGraph
Expand Down Expand Up @@ -83,6 +84,7 @@ internal fun MainScreen(
homeNavGraph(
padding = innerPadding,
navigateToVideoDetail = navigator::navigateVideoDetail,
navigateToUpload = navigator::navigateToUpload
)

profileNavGraph(
Expand All @@ -98,7 +100,6 @@ internal fun MainScreen(

uploadNavGraph(
padding = innerPadding,
navigateSelectedVideo = navigator::navigateSelectedVideo,
)

videoNavGraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import androidx.navigation.compose.composable
import com.record.upload.SelectedVideoRoute
import com.record.upload.VideoPickerRoute

fun NavController.navigateUpload(navOptions: NavOptions) {
navigate(UploadRoute.ROUTE, navOptions)
fun NavController.navigateToUpload() {
navigate(UploadRoute.ROUTE)
}

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
Expand Down

0 comments on commit 6a75d71

Please sign in to comment.