-
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
1 parent
bc1f5ac
commit fe71c4e
Showing
8 changed files
with
249 additions
and
24 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
9 changes: 9 additions & 0 deletions
9
app/src/main/java/org/sopt/dateroad/presentation/model/RouteModel.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,9 @@ | ||
package org.sopt.dateroad.presentation.model | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
sealed interface Route | ||
|
||
sealed interface MainNavigationBarRoute : Route { | ||
data object Dummy : MainNavigationBarRoute | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/org/sopt/dateroad/presentation/type/MainNavigationBarItemType.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,32 @@ | ||
package org.sopt.dateroad.presentation.type | ||
|
||
import androidx.annotation.DrawableRes | ||
import androidx.annotation.StringRes | ||
import androidx.compose.runtime.Composable | ||
import org.sopt.dateroad.R | ||
import org.sopt.dateroad.presentation.model.MainNavigationBarRoute | ||
import org.sopt.dateroad.presentation.model.Route | ||
|
||
enum class MainNavigationBarItemType( | ||
@DrawableRes val iconRes: Int, | ||
@StringRes val label: Int, | ||
val route: MainNavigationBarRoute | ||
) { | ||
DUMMY( | ||
iconRes = R.drawable.ic_launcher_foreground, | ||
label = R.string.app_name, | ||
route = MainNavigationBarRoute.Dummy | ||
); | ||
|
||
companion object { | ||
@Composable | ||
fun find(predicate: @Composable (MainNavigationBarRoute) -> Boolean): MainNavigationBarItemType? { | ||
return entries.find { predicate(it.route) } | ||
} | ||
|
||
@Composable | ||
fun contains(predicate: @Composable (Route) -> Boolean): Boolean { | ||
return entries.map { it.route }.any { predicate(it) } | ||
} | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
app/src/main/java/org/sopt/dateroad/presentation/ui/navigator/MainNavigator.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,66 @@ | ||
package org.sopt.dateroad.presentation.ui.navigator | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.navigation.NavDestination | ||
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 org.sopt.dateroad.presentation.model.MainNavigationBarRoute | ||
import org.sopt.dateroad.presentation.model.Route | ||
import org.sopt.dateroad.presentation.type.MainNavigationBarItemType | ||
|
||
class MainNavigator( | ||
val navHostController: NavHostController | ||
) { | ||
private val currentDestination: NavDestination? | ||
@Composable get() = navHostController.currentBackStackEntryAsState().value?.destination | ||
|
||
val startDestination = MainNavigationBarItemType.DUMMY.route | ||
|
||
val currentMainNavigationBarItem: MainNavigationBarItemType? | ||
@Composable get() = MainNavigationBarItemType.find { mainNavigationBarRoute -> | ||
currentDestination?.route == mainNavigationBarRoute::class.simpleName | ||
} | ||
|
||
fun navigateMainNavigation(mainNavigationBarItemType: MainNavigationBarItemType) { | ||
navOptions { | ||
popUpTo(navHostController.graph.findStartDestination().id) { | ||
saveState = true | ||
} | ||
launchSingleTop = true | ||
restoreState = true | ||
}.let { navOptions -> | ||
when (mainNavigationBarItemType) { | ||
MainNavigationBarItemType.DUMMY -> navHostController | ||
} | ||
} | ||
} | ||
|
||
private fun popBackStack() { | ||
navHostController.popBackStack() | ||
} | ||
|
||
fun popBackStackIfNotHome() { | ||
if (!isSameCurrentDestination<MainNavigationBarRoute.Dummy>()) { | ||
popBackStack() | ||
} | ||
} | ||
|
||
private inline fun <reified T : Route> isSameCurrentDestination(): Boolean = | ||
navHostController.currentDestination?.route == T::class.simpleName | ||
|
||
@Composable | ||
fun showBottomBar(): Boolean = MainNavigationBarItemType.contains { | ||
currentDestination?.route == it::class.simpleName | ||
} | ||
} | ||
|
||
@Composable | ||
fun rememberMainNavigator( | ||
navHostController: NavHostController = rememberNavController() | ||
): MainNavigator = remember(navHostController) { | ||
MainNavigator(navHostController = navHostController) | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/org/sopt/dateroad/presentation/ui/navigator/MainScreen.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,51 @@ | ||
package org.sopt.dateroad.presentation.ui.navigator | ||
|
||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import org.sopt.dateroad.presentation.type.MainNavigationBarItemType | ||
import org.sopt.dateroad.presentation.ui.navigator.component.MainBottomBar | ||
import org.sopt.dateroad.presentation.ui.navigator.component.MainNavHost | ||
import org.sopt.dateroad.ui.theme.DATEROADTheme | ||
|
||
@Composable | ||
fun MainScreen( | ||
navigator: MainNavigator = rememberMainNavigator() | ||
) { | ||
MainScreenContent( | ||
navigator = navigator | ||
) | ||
} | ||
|
||
@Composable | ||
private fun MainScreenContent( | ||
modifier: Modifier = Modifier, | ||
navigator: MainNavigator | ||
) { | ||
Scaffold( | ||
modifier = modifier, | ||
content = { padding -> | ||
MainNavHost( | ||
navigator = navigator, | ||
padding = padding | ||
) | ||
}, | ||
bottomBar = { | ||
MainBottomBar( | ||
isVisible = navigator.showBottomBar(), | ||
navigationBarItems = MainNavigationBarItemType.entries.toList(), | ||
currentNavigationBarItem = navigator.currentMainNavigationBarItem, | ||
onNavigationBarItemSelected = { navigator.navigateMainNavigation(it) } | ||
) | ||
} | ||
) | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun MainPreview() { | ||
DATEROADTheme { | ||
MainScreen() | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/org/sopt/dateroad/presentation/ui/navigator/component/MainBottomBar.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,56 @@ | ||
package org.sopt.dateroad.presentation.ui.navigator.component | ||
|
||
import android.content.Context | ||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.NavigationBar | ||
import androidx.compose.material3.NavigationBarItem | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import org.sopt.dateroad.presentation.type.MainNavigationBarItemType | ||
import org.sopt.dateroad.ui.theme.DATEROADTheme | ||
|
||
@Composable | ||
fun MainBottomBar( | ||
modifier: Modifier = Modifier, | ||
context: Context = LocalContext.current, | ||
isVisible: Boolean, | ||
navigationBarItems: List<MainNavigationBarItemType>, | ||
currentNavigationBarItem: MainNavigationBarItemType?, | ||
onNavigationBarItemSelected: (MainNavigationBarItemType) -> Unit | ||
) { | ||
AnimatedVisibility(visible = isVisible) { | ||
NavigationBar { | ||
navigationBarItems.forEachIndexed { _, mainNavigationBarItemType -> | ||
NavigationBarItem( | ||
selected = currentNavigationBarItem == mainNavigationBarItemType, | ||
onClick = { onNavigationBarItemSelected(mainNavigationBarItemType) }, | ||
icon = { | ||
Icon( | ||
painter = painterResource(id = mainNavigationBarItemType.iconRes), | ||
contentDescription = context.getString(mainNavigationBarItemType.label) | ||
) | ||
}, | ||
label = { Text(text = context.getString(mainNavigationBarItemType.label)) } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun MainBottomBarPreview() { | ||
DATEROADTheme { | ||
MainBottomBar( | ||
isVisible = true, | ||
navigationBarItems = MainNavigationBarItemType.entries.toList(), | ||
currentNavigationBarItem = MainNavigationBarItemType.DUMMY, | ||
onNavigationBarItemSelected = {} | ||
) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/src/main/java/org/sopt/dateroad/presentation/ui/navigator/component/MainNavHost.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,25 @@ | ||
package org.sopt.dateroad.presentation.ui.navigator.component | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import org.sopt.dateroad.presentation.ui.navigator.MainNavigator | ||
|
||
@Composable | ||
fun MainNavHost( | ||
modifier: Modifier = Modifier, | ||
navigator: MainNavigator, | ||
padding: PaddingValues | ||
) { | ||
Box( | ||
modifier = modifier | ||
.fillMaxSize() | ||
.background(MaterialTheme.colorScheme.surfaceDim) | ||
) { | ||
|
||
} | ||
} |