Merge pull request #81 from TouchInstinct/feature/tabbar_navigation_add_possibility_to_reset_tab_state
Add possibility to reset tab state every time you navigate to it
This commit is contained in:
commit
da4d9c16ef
|
|
@ -19,7 +19,7 @@ import ru.touchin.roboswag.core.utils.ShouldNotHappenException
|
|||
class BottomNavigationController(
|
||||
private val context: Context,
|
||||
private val fragmentManager: FragmentManager,
|
||||
private val viewControllers: SparseArray<Pair<Class<out ViewController<*, *>>, Parcelable>>,
|
||||
private val viewControllers: SparseArray<BottomNavigationFragment.TabData>,
|
||||
@IdRes private val contentContainerViewId: Int,
|
||||
@LayoutRes private val contentContainerLayoutId: Int,
|
||||
private val wrapWithNavigationContainer: Boolean = false,
|
||||
|
|
@ -63,7 +63,7 @@ class BottomNavigationController(
|
|||
|
||||
fun navigateTo(@IdRes itemId: Int, state: Parcelable? = null) {
|
||||
// Find view controller class that needs to open
|
||||
val (viewControllerClass, defaultViewControllerState) = viewControllers[itemId] ?: return
|
||||
val (viewControllerClass, defaultViewControllerState, saveStateOnSwitching) = viewControllers[itemId] ?: return
|
||||
if (state != null && state::class != defaultViewControllerState::class) {
|
||||
throw ShouldNotHappenException(
|
||||
"Incorrect state type for navigation tab root ViewController. Should be ${defaultViewControllerState::class}"
|
||||
|
|
@ -76,7 +76,7 @@ class BottomNavigationController(
|
|||
val viewControllerName = viewControllerClass.canonicalName
|
||||
var fragment = fragmentManager.findFragmentByTag(viewControllerName)
|
||||
|
||||
if (state == null && fragment != null) {
|
||||
if (saveStateOnSwitching && state == null && fragment != null) {
|
||||
transaction.attach(fragment)
|
||||
} else {
|
||||
// If fragment already exist remove it first
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ abstract class BottomNavigationFragment : Fragment() {
|
|||
|
||||
protected abstract val wrapWithNavigationContainer: Boolean
|
||||
|
||||
protected abstract val navigationViewControllers: SparseArray<Pair<Class<out ViewController<*, *>>, Parcelable>>
|
||||
protected abstract val navigationViewControllers: SparseArray<TabData>
|
||||
|
||||
protected open val reselectListener: (() -> Unit) = { getNavigationActivity().innerNavigation.up(inclusive = true) }
|
||||
|
||||
|
|
@ -72,4 +72,14 @@ abstract class BottomNavigationFragment : Fragment() {
|
|||
|
||||
private fun getNavigationActivity() = requireActivity() as BottomNavigationActivity
|
||||
|
||||
data class TabData(
|
||||
val viewControllerClass: Class<out ViewController<*, *>>,
|
||||
val viewControllerState: Parcelable,
|
||||
/**
|
||||
* It can be useful in some cases when it is necessary to create ViewController
|
||||
* with initial state every time when tab opens.
|
||||
*/
|
||||
val saveStateOnSwitching: Boolean = true
|
||||
)
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue