Skip to content

Commit

Permalink
[feature/droidknights#300] Apply RouteModel on MainNavigator
Browse files Browse the repository at this point in the history
  • Loading branch information
l2hyunwoo committed May 30, 2024
1 parent 32d8d8c commit b0a6d73
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ package com.droidknights.app.core.navigation

import kotlinx.serialization.Serializable

interface Route

interface MainTabRoute : Route

@Serializable
data object Contributor
data object Contributor : Route

@Serializable
data object Home
data object Home : MainTabRoute

@Serializable
data object Setting
data object Setting : MainTabRoute

@Serializable
data object Bookmark
data object Bookmark : MainTabRoute

@Serializable
data object Session
data object Session : Route

@Serializable
data class SessionDetail(val sessionId: String)
data class SessionDetail(val sessionId: String) : Route
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package com.droidknights.app.feature.main
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navOptions
import com.droidknights.app.core.navigation.Home
import com.droidknights.app.core.navigation.Route
import com.droidknights.app.feature.bookmark.navigation.navigateBookmark
import com.droidknights.app.feature.contributor.navigation.navigateContributor
import com.droidknights.app.feature.home.navigation.HomeRoute
import com.droidknights.app.feature.home.navigation.navigateHome
import com.droidknights.app.feature.session.navigation.navigateSession
import com.droidknights.app.feature.session.navigation.navigateSessionDetail
Expand All @@ -26,9 +28,9 @@ internal class MainNavigator(
val startDestination = MainTab.HOME.route

val currentTab: MainTab?
@Composable get() = currentDestination
?.route
?.let(MainTab::find)
@Composable get() = MainTab.find { tab ->
currentDestination?.hasRoute(tab::class) == true
}

fun navigate(tab: MainTab) {
val navOptions = navOptions {
Expand Down Expand Up @@ -63,18 +65,18 @@ internal class MainNavigator(
}

fun popBackStackIfNotHome() {
if (!isSameCurrentDestination(HomeRoute.ROUTE)) {
if (!isSameCurrentDestination<Home>()) {
popBackStack()
}
}

private fun isSameCurrentDestination(route: String) =
navController.currentDestination?.route == route
private inline fun <reified T : Route> isSameCurrentDestination(): Boolean {
return navController.currentDestination?.hasRoute<T>() == true
}

@Composable
fun shouldShowBottomBar(): Boolean {
val currentRoute = currentDestination?.route ?: return false
return currentRoute in MainTab
fun shouldShowBottomBar() = MainTab.contains {
currentDestination?.hasRoute(it::class) == true
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package com.droidknights.app.feature.main

import com.droidknights.app.feature.bookmark.navigation.BookmarkRoute
import com.droidknights.app.feature.home.navigation.HomeRoute
import com.droidknights.app.feature.setting.navigation.SettingRoute
import androidx.compose.runtime.Composable
import com.droidknights.app.core.navigation.Bookmark
import com.droidknights.app.core.navigation.Home
import com.droidknights.app.core.navigation.MainTabRoute
import com.droidknights.app.core.navigation.Route
import com.droidknights.app.core.navigation.Setting

internal enum class MainTab(
val iconResId: Int,
internal val contentDescription: String,
val route: String,
val route: MainTabRoute,
) {
SETTING(
iconResId = R.drawable.ic_setting,
contentDescription = "설정",
SettingRoute.ROUTE_SETTING,
Setting,
),
HOME(
iconResId = R.drawable.ic_home,
contentDescription = "",
HomeRoute.ROUTE,
Home
),
BOOKMARK(
iconResId = R.drawable.ic_bookmark,
contentDescription = "북마크",
BookmarkRoute.ROUTE,
Bookmark,
);

companion object {

operator fun contains(route: String): Boolean {
return entries.map { it.route }.contains(route)
@Composable
fun find(predicate: @Composable (MainTabRoute) -> Boolean): MainTab? {
return entries.find { predicate(it.route) }
}

fun find(route: String): MainTab? {
return entries.find { it.route == route }
@Composable
fun contains(predicate: @Composable (Route) -> Boolean): Boolean {
return entries.map { it.route }.any { predicate(it) }
}
}
}

0 comments on commit b0a6d73

Please sign in to comment.