Skip to content

Commit

Permalink
BugFix:
Browse files Browse the repository at this point in the history
- Active tab and back press sync
- Back press reselects the start destination
  • Loading branch information
jaydeep-p-simform authored and yashwantgowla-simform committed Sep 19, 2024
1 parent 42ce89e commit db14899
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class MainActivity : AppCompatActivity() {
binding.bottomNavigation.apply {
// If you don't pass activeIndex then by default it will take 0 position
setMenuItems(menuItems, activeIndex)
setupWithNavController(navController)
setupWithNavController(navController = navController, exitOnBack = false)

// manually set the active item, so from which you can control which position item should be active when it is initialized.
// onMenuItemClick(4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,18 @@ class SSCustomBottomNavigation : FrameLayout {
}

// function to setup with navigation controller just like in BottomNavigationView
fun setupWithNavController(navController: NavController) {
fun setupWithNavController(navController: NavController, exitOnBack: Boolean = false) {
// check for menu initialization
if (!isMenuInitialized) {
throw RuntimeException("initialize menu by calling setMenuItems() before setting up with NavController")
}

// initialize the menu
setOnMenuItemClickListener { item, _ ->
navigateToDestination(navController, item)
navigateToDestination(navController, item, exitOnBack)
}
// setup destination change listener to properly sync the back button press
navController.addOnDestinationChangedListener { _, destination, _ ->
navController.addOnDestinationChangedListener { controller, destination, _ ->
for (i in cbnMenuItems.indices) {
if (matchDestination(destination, cbnMenuItems[i].destinationId)) {
if (selectedIndex != i && isAnimating) {
Expand All @@ -331,6 +331,7 @@ class SSCustomBottomNavigation : FrameLayout {
animatorSet.cancel()
isAnimating = false
}
show(id = cbnMenuItems[i].id, isMenuClicked = false)
}
}
}
Expand All @@ -356,7 +357,7 @@ class SSCustomBottomNavigation : FrameLayout {

// source code referenced from the actual JetPack Navigation Component
// refer to the original source code
private fun navigateToDestination(navController: NavController, itemCbn: Model) {
private fun navigateToDestination(navController: NavController, itemCbn: Model, exitOnBack: Boolean) {
if (itemCbn.destinationId == -1) {
throw RuntimeException("please set a valid id, unable the navigation!")
}
Expand All @@ -366,11 +367,14 @@ class SSCustomBottomNavigation : FrameLayout {
.setExitAnim(android.R.anim.fade_out)
.setPopEnterAnim(android.R.anim.fade_in)
.setPopExitAnim(android.R.anim.fade_out)
// pop to the navigation graph's start destination
builder.setPopUpTo(findStartDestination(navController.graph).id, false)
// pop to the navigation graph's start destination
val startDestination = findStartDestination(navController.graph)
if (itemCbn.destinationId == startDestination.id)
builder.setPopUpTo(startDestination.id, false)
val options = builder.build()
try {
navController.popBackStack()
if (exitOnBack)
navController.popBackStack()
navController.navigate(itemCbn.destinationId, null, options)
} catch (e: IllegalArgumentException) {
Log.w("TAG", "unable to navigate!", e)
Expand Down Expand Up @@ -516,13 +520,14 @@ class SSCustomBottomNavigation : FrameLayout {
}
}

fun show(id: Int, enableAnimation: Boolean = true) {
fun show(id: Int, enableAnimation: Boolean = true, isMenuClicked: Boolean = true) {
for (i in models.indices) {
val model = models[i]
val cell = cells[i]
if (model.id == id) {
onShowListener(model)
menuItemClickListener?.invoke(cbnMenuItems[i], i)
if (isMenuClicked)
menuItemClickListener?.invoke(cbnMenuItems[i], i)
anim(cell, id, enableAnimation)
cell.enableCell()
} else {
Expand Down

0 comments on commit db14899

Please sign in to comment.