Skip to content
Permalink
Browse files

Revert changes to MultiBackStackNavigationExtensions

  • Loading branch information...
chrisbanes committed Nov 7, 2019
1 parent a24580b commit e52f2fc84b1b59e48850bad4465e70d0e198bf12
Showing with 46 additions and 34 deletions.
  1. +46 −34 common-ui/src/main/java/app/tivi/extensions/MultipleBackStackNavigationExtensions.kt
@@ -21,8 +21,6 @@ import android.util.SparseArray
import androidx.core.util.forEach
import androidx.core.util.set
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import androidx.fragment.app.commitNow
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.navigation.NavController
@@ -41,12 +39,13 @@ fun BottomNavigationView.setupWithNavController(
containerId: Int,
intent: Intent? = null
): LiveData<NavController> {

// Map of tags
val graphIdToTagMap = SparseArray<String>()
// Result. Mutable live data with the selected controlled
val selectedNavController = MutableLiveData<NavController>()

var firstFragmentGraphId = navGraphIds[0]
var firstFragmentGraphId = 0

// First create a NavHostFragment for each NavGraph ID
navGraphIds.forEachIndexed { index, navGraphId ->
@@ -63,11 +62,15 @@ fun BottomNavigationView.setupWithNavController(
// Obtain its id
val graphId = navHostFragment.navController.graph.id

if (index == 0) {
firstFragmentGraphId = graphId
}

// Save to the map
graphIdToTagMap[graphId] = fragmentTag

// Attach or detach nav host fragment depending on whether it's the selected item.
if (selectedItemId == graphId) {
if (this.selectedItemId == graphId) {
// Update livedata with the selected graph
selectedNavController.value = navHostFragment.navController
attachNavHostFragment(fragmentManager, navHostFragment, index == 0)
@@ -77,7 +80,7 @@ fun BottomNavigationView.setupWithNavController(
}

// Now connect selecting an item with swapping Fragments
var selectedItemTag = graphIdToTagMap[selectedItemId]
var selectedItemTag = graphIdToTagMap[this.selectedItemId]
val firstFragmentTag = graphIdToTagMap[firstFragmentGraphId]
var isOnFirstFragment = selectedItemTag == firstFragmentTag

@@ -99,23 +102,22 @@ fun BottomNavigationView.setupWithNavController(
if (firstFragmentTag != newlySelectedItemTag) {
// Commit a transaction that cleans the back stack and adds the first fragment
// to it, creating the fixed started destination.
fragmentManager.commit {
setCustomAnimations(R.anim.tivi_enter_anim, R.anim.tivi_exit_anim,
R.anim.tivi_pop_enter_anim, R.anim.tivi_pop_exit_anim)

attach(selectedFragment)
setPrimaryNavigationFragment(selectedFragment)

// Detach all other Fragments
graphIdToTagMap.forEach { _, fragmentTagIter ->
if (fragmentTagIter != newlySelectedItemTag) {
detach(fragmentManager.findFragmentByTag(firstFragmentTag)!!)
fragmentManager.beginTransaction()
.setCustomAnimations(R.anim.tivi_enter_anim, R.anim.tivi_exit_anim,
R.anim.tivi_pop_enter_anim, R.anim.tivi_pop_exit_anim)
.attach(selectedFragment)
.setPrimaryNavigationFragment(selectedFragment)
.apply {
// Detach all other Fragments
graphIdToTagMap.forEach { _, fragmentTagIter ->
if (fragmentTagIter != newlySelectedItemTag) {
detach(fragmentManager.findFragmentByTag(firstFragmentTag)!!)
}
}
}
}

addToBackStack(firstFragmentTag)
setReorderingAllowed(true)
}
.addToBackStack(firstFragmentTag)
.setReorderingAllowed(true)
.commit()
}
selectedItemTag = newlySelectedItemTag
isOnFirstFragment = selectedItemTag == firstFragmentTag
@@ -138,7 +140,7 @@ fun BottomNavigationView.setupWithNavController(
// Finally, ensure that we update our BottomNavigationView when the back stack changes
fragmentManager.addOnBackStackChangedListener {
if (!isOnFirstFragment && !fragmentManager.isOnBackStack(firstFragmentTag)) {
selectedItemId = firstFragmentGraphId
this.selectedItemId = firstFragmentGraphId
}

// Reset the graph if the currentDestination is not valid (happens when the back
@@ -171,7 +173,7 @@ private fun BottomNavigationView.setupDeepLinks(
// Handle Intent
if (navHostFragment.navController.handleDeepLink(intent) &&
selectedItemId != navHostFragment.navController.graph.id) {
selectedItemId = navHostFragment.navController.graph.id
this.selectedItemId = navHostFragment.navController.graph.id
}
}
}
@@ -186,24 +188,34 @@ private fun BottomNavigationView.setupItemReselected(
as NavHostFragment
val navController = selectedFragment.navController
// Pop the back stack to the start destination of the current navController graph
navController.popBackStack(navController.graph.startDestination, false)
navController.popBackStack(
navController.graph.startDestination, false
)
}
}

private fun detachNavHostFragment(
fragmentManager: FragmentManager,
navHostFragment: NavHostFragment
) = fragmentManager.commitNow { detach(navHostFragment) }
) {
fragmentManager.beginTransaction()
.detach(navHostFragment)
.commitNow()
}

private fun attachNavHostFragment(
fragmentManager: FragmentManager,
navHostFragment: NavHostFragment,
isPrimaryNavFragment: Boolean
) = fragmentManager.commitNow {
attach(navHostFragment)
if (isPrimaryNavFragment) {
setPrimaryNavigationFragment(navHostFragment)
}
) {
fragmentManager.beginTransaction()
.attach(navHostFragment)
.apply {
if (isPrimaryNavFragment) {
setPrimaryNavigationFragment(navHostFragment)
}
}
.commitNow()
}

private fun obtainNavHostFragment(
@@ -214,13 +226,13 @@ private fun obtainNavHostFragment(
): NavHostFragment {
// If the Nav Host fragment exists, return it
val existingFragment = fragmentManager.findFragmentByTag(fragmentTag) as NavHostFragment?
if (existingFragment != null) {
return existingFragment
}
existingFragment?.let { return it }

// Otherwise, create it and return it.
val navHostFragment = NavHostFragment.create(navGraphId)
fragmentManager.commitNow { add(containerId, navHostFragment, fragmentTag) }
fragmentManager.beginTransaction()
.add(containerId, navHostFragment, fragmentTag)
.commitNow()
return navHostFragment
}

0 comments on commit e52f2fc

Please sign in to comment.
You can’t perform that action at this time.