Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT/YAF-000] 네비게이션 로직을 통합합니다. #44

Merged
merged 11 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import com.yapp.convention.setNamespace
plugins {
id("orbit.android.library")
id("orbit.android.hilt")
id("orbit.android.compose")
}

android {
setNamespace("core.common")
}

dependencies {
implementation(projects.core.designsystem)
implementation(libs.compose.navigation)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.yapp.common.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.navigation.NavDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navOptions
import com.yapp.common.navigation.destination.OnboardingDestination
import com.yapp.common.navigation.destination.TopLevelDestination

class OrbitNavigator(
val navController: NavHostController,
) {
val startDestination = OnboardingDestination.Route.route

private val currentDestination: NavDestination?
@Composable get() = navController
.currentBackStackEntryAsState().value?.destination

val currentTab: TopLevelDestination?
@Composable get() = currentDestination
?.route
?.let(TopLevelDestination.Companion::find)

fun navigateTo(route: String, popUpTo: String? = null, inclusive: Boolean = false) {
navController.navigate(route) {
popUpTo?.let {
popUpTo(it) { this.inclusive = inclusive }
}
launchSingleTop = true
restoreState = true
}
}

fun navigateBack() {
navController.popBackStack()
}

fun navigateToTopLevelDestination(tab: TopLevelDestination) {
val navOptions = navOptions {
popUpTo(Routes.Home.ROUTE) {
saveState = true
}
launchSingleTop = true
restoreState = true
}

when (tab) {
TopLevelDestination.HOME -> navController.navigate(Routes.Home.ROUTE, navOptions)
TopLevelDestination.MYPAGE -> navController.navigate(Routes.MyPage.ROUTE, navOptions)
}
}

@Composable
fun shouldShowBottomBar(): Boolean {
val currentRoute = currentDestination?.route ?: return false
return currentRoute in TopLevelDestination.entries.map { it.route }
}
}

@Composable
fun rememberOrbitNavigator(
navController: NavHostController = rememberNavController(),
): OrbitNavigator = remember(navController) {
OrbitNavigator(navController)
}
27 changes: 27 additions & 0 deletions core/common/src/main/java/com/yapp/common/navigation/Routes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.yapp.common.navigation

object Routes {
object Onboarding {
const val ROUTE = "onboarding_route"
const val EXPLAIN = "onboarding_explain"
const val ALARM_TIME_SELECTION = "onboarding_alarm_time_selection"
const val BIRTHDAY = "onboarding_birthday"
const val TIME_OF_BIRTH = "onboarding_time_of_birth"
const val NAME = "onboarding_name"
const val GENDER = "onboarding_gender"
const val ACCESS = "onboarding_access"
const val COMPLETE_FIRST = "onboarding_complete_first"
const val COMPLETE_SECOND = "onboarding_complete_second"
}

object Home {
const val ROUTE = "home_route"
const val HOME = "home"
const val ALARM_ADD_EDIT = "alarm_add_edit"
}

object MyPage {
const val ROUTE = "mypage_route"
const val MYPAGE = "mypage"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.yapp.common.navigation.destination

import com.yapp.common.navigation.Routes

sealed class HomeDestination(val route: String) {
data object Route : HomeDestination(Routes.Home.ROUTE)
data object Home : HomeDestination(Routes.Home.HOME)
data object AlarmAddEdit : HomeDestination(Routes.Home.ALARM_ADD_EDIT)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.yapp.common.navigation.destination

import com.yapp.common.navigation.Routes

sealed class MyPageDestination(val route: String) {
data object Route : MyPageDestination(Routes.MyPage.ROUTE)
data object MyPage : MyPageDestination(Routes.MyPage.MYPAGE)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.yapp.common.navigation.destination

import com.yapp.common.navigation.Routes

sealed class OnboardingDestination(val route: String) {
data object Route : OnboardingDestination(Routes.Onboarding.ROUTE)
data object Explain : OnboardingDestination(Routes.Onboarding.EXPLAIN)
data object AlarmTimeSelection : OnboardingDestination(Routes.Onboarding.ALARM_TIME_SELECTION)
data object Birthday : OnboardingDestination(Routes.Onboarding.BIRTHDAY)
data object TimeOfBirth : OnboardingDestination(Routes.Onboarding.TIME_OF_BIRTH)
data object Name : OnboardingDestination(Routes.Onboarding.NAME)
data object Gender : OnboardingDestination(Routes.Onboarding.GENDER)
data object Access : OnboardingDestination(Routes.Onboarding.ACCESS)
data object Complete1 : OnboardingDestination(Routes.Onboarding.COMPLETE_FIRST)
data object Complete2 : OnboardingDestination(Routes.Onboarding.COMPLETE_SECOND)

companion object {
private val routes = listOf(Explain, AlarmTimeSelection, Birthday, TimeOfBirth, Name, Gender, Access, Complete1, Complete2)

fun nextRoute(currentStep: Int): String? {
return routes.getOrNull(currentStep)?.route
}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package com.yapp.navigator.navigation
package com.yapp.common.navigation.destination

import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.yapp.home.HomeRoute
import com.yapp.mypage.MypageRoute
import com.yapp.common.navigation.Routes

enum class MainNavTab(
enum class TopLevelDestination(
@DrawableRes val iconId: Int,
@StringRes val titleId: Int,
val route: String,
) {
HOME(
iconId = core.designsystem.R.drawable.ic_launcher_foreground,
titleId = core.designsystem.R.string.app_name,
route = HomeRoute.HOME,
route = Routes.Home.ROUTE,
),
MYPAGE(
iconId = core.designsystem.R.drawable.ic_launcher_foreground,
titleId = core.designsystem.R.string.app_name,
route = MypageRoute.MYPAGE,
route = Routes.MyPage.MYPAGE,
),
;

companion object {
operator fun contains(route: String): Boolean = entries.any { it.route == route }
fun find(route: String): MainNavTab? = entries.find { it.route == route }
fun find(route: String): TopLevelDestination? = entries.find { it.route == route }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.yapp.common.navigation.extensions

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController

@Composable
inline fun <reified T : ViewModel> NavBackStackEntry.sharedViewModel(navController: NavController): T {
val navGraphRoute = destination.parent?.route ?: return viewModel()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4
단순궁금!
혹시 null 반환되면 어떤 동작이 일어나나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null 이면 return viewmodel() 이 호출되서 해당 composable에 해당하는 뷰모델이 만들어지쥬

val parentEntry = remember(this) {
navController.getBackStackEntry(navGraphRoute)
}
return viewModel(parentEntry)
}
1 change: 1 addition & 0 deletions feature/home/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {

dependencies {
implementation(projects.core.ui)
implementation(projects.core.common)
implementation(libs.orbit.core)
implementation(libs.orbit.compose)
implementation(libs.orbit.viewmodel)
Expand Down
25 changes: 25 additions & 0 deletions feature/home/src/main/java/com/yapp/home/HomeNavGraph.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yapp.home

import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.compose.navigation
import com.yapp.alarm.AlarmAddEditRoute
import com.yapp.common.navigation.OrbitNavigator
import com.yapp.common.navigation.destination.HomeDestination

fun NavGraphBuilder.homeNavGraph(
navigator: OrbitNavigator,
) {
navigation(
route = HomeDestination.Route.route,
startDestination = HomeDestination.Home.route,
) {
composable(route = HomeDestination.Home.route) {
HomeRoute()
}

composable(route = HomeDestination.AlarmAddEdit.route) {
AlarmAddEditRoute()
}
}
}
21 changes: 0 additions & 21 deletions feature/home/src/main/java/com/yapp/home/HomeNavigation.kt

This file was deleted.

4 changes: 4 additions & 0 deletions feature/mypage/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ plugins {
android {
setNamespace("feature.myapge")
}

dependencies {
implementation(projects.core.common)
}
20 changes: 20 additions & 0 deletions feature/mypage/src/main/java/com/yapp/mypage/MyPageNavGraph.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.yapp.mypage

import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import androidx.navigation.navigation
import com.yapp.common.navigation.OrbitNavigator
import com.yapp.common.navigation.destination.MyPageDestination

fun NavGraphBuilder.myPageNavGraph(
navigator: OrbitNavigator,
) {
navigation(
route = MyPageDestination.Route.route,
startDestination = MyPageDestination.MyPage.route,
) {
composable(route = MyPageDestination.MyPage.route) {
MypageRoute()
}
}
}
14 changes: 0 additions & 14 deletions feature/mypage/src/main/java/com/yapp/mypage/MyPageNavigation.kt

This file was deleted.

1 change: 1 addition & 0 deletions feature/navigator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android {
}

dependencies {
implementation(projects.core.common)
implementation(projects.feature.home)
implementation(projects.feature.mypage)
implementation(projects.feature.onboarding)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class MainActivity : ComponentActivity() {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
setContent {
OrbitTheme {
// OnboardingRoute(onFinishOnboarding = {})
MainScreen()
OrbitNavHost()
}
}
}
Expand Down

This file was deleted.

Loading
Loading