Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Fixes #48 #49 Sync active tabs on back press #53

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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