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.route
- } -> {
- selectedItem.value = Screen.Home
- }
- destination.hierarchy.any {
- it.route == Screen.Search.route
- } -> {
- selectedItem.value = Screen.Search
- }
- destination.hierarchy.any {
- it.route == Screen.Favorite.route
- } -> {
- selectedItem.value = Screen.Favorite
- }
- destination.hierarchy.any {
- it.route == Screen.Profile.route
- } -> {
- selectedItem.value = Screen.Profile
- }
- }
- }
- addOnDestinationChangedListener(listener)
- onDispose {
- removeOnDestinationChangedListener(listener)
- }
- }
- return selectedItem
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement