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:
Даниил Борисовский 2019-10-03 13:03:21 +03:00 committed by GitHub
commit da4d9c16ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -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

View File

@ -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
)
}