Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Stable
- @Composable
- private fun NavController.currentScreenAsState(): State<Screen> {
- val selectedItem = remember { mutableStateOf<Screen>(Screen.Home) }
- DisposableEffect(this) {
- val listener = NavController.OnDestinationChangedListener { _, destination, _ ->
- when {
- destination.hierarchy.any {
- it.route == Screen.Home.createRoute(Screen.Main)
- } -> {
- selectedItem.value = Screen.Home
- }
- destination.hierarchy.any {
- it.route == Screen.Search.createRoute(Screen.Main)
- } -> {
- selectedItem.value = Screen.Search
- }
- destination.hierarchy.any {
- it.route == Screen.Favorite.createRoute(Screen.Main)
- } -> {
- selectedItem.value = Screen.Favorite
- }
- destination.hierarchy.any {
- it.route == Screen.Profile.createRoute(Screen.Main)
- } -> {
- selectedItem.value = Screen.Profile
- }
- }
- }
- addOnDestinationChangedListener(listener)
- onDispose {
- removeOnDestinationChangedListener(listener)
- }
- }
- return selectedItem
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement