From 7caf2d121be52e4de25ec9ea486139f1ec4291e8 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Mon, 30 Sep 2019 18:54:06 +0300 Subject: [PATCH 1/4] Add possibility to reset tab state every time you navigate to it --- .../tabbarnavigation/BottomNavigationController.kt | 6 +++--- .../tabbarnavigation/BottomNavigationFragment.kt | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt index 427a58b..e80c0fd 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt @@ -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>, Parcelable>>, + private val viewControllers: SparseArray, @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, needToSaveState) = 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 (needToSaveState && state == null && fragment != null) { transaction.attach(fragment) } else { // If fragment already exist remove it first diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt index fc659a9..8fe1dde 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt @@ -29,7 +29,7 @@ abstract class BottomNavigationFragment : Fragment() { protected abstract val wrapWithNavigationContainer: Boolean - protected abstract val navigationViewControllers: SparseArray>, Parcelable>> + protected abstract val navigationViewControllers: SparseArray protected open val reselectListener: (() -> Unit) = { getNavigationActivity().innerNavigation.up(inclusive = true) } @@ -72,4 +72,10 @@ abstract class BottomNavigationFragment : Fragment() { private fun getNavigationActivity() = requireActivity() as BottomNavigationActivity + data class TabData( + val viewControllerClass: Class>, + val viewControllerState: Parcelable, + val needToSaveState: Boolean = false + ) + } From dd52179e2975f86a56d138d3374303344d01cee5 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Mon, 30 Sep 2019 20:49:31 +0300 Subject: [PATCH 2/4] Fixed comment https://github.com/TouchInstinct/RoboSwag/pull/81/files#r329668219 Renamed `needToSaveState` to `saveStateOnSwitching` --- .../components/tabbarnavigation/BottomNavigationController.kt | 4 ++-- .../components/tabbarnavigation/BottomNavigationFragment.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt index e80c0fd..b8a1bc6 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationController.kt @@ -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, needToSaveState) = 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 (needToSaveState && state == null && fragment != null) { + if (saveStateOnSwitching && state == null && fragment != null) { transaction.attach(fragment) } else { // If fragment already exist remove it first diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt index 8fe1dde..848619e 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt @@ -75,7 +75,7 @@ abstract class BottomNavigationFragment : Fragment() { data class TabData( val viewControllerClass: Class>, val viewControllerState: Parcelable, - val needToSaveState: Boolean = false + val saveStateOnSwitching: Boolean = false ) } From c173e7ad64e3708a2cc157dd04419f1db838d578 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Mon, 30 Sep 2019 21:16:05 +0300 Subject: [PATCH 3/4] Change default value of `saveStateOnSwitching` to `true` --- .../components/tabbarnavigation/BottomNavigationFragment.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt index 848619e..bc1acf2 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt @@ -75,7 +75,9 @@ abstract class BottomNavigationFragment : Fragment() { data class TabData( val viewControllerClass: Class>, val viewControllerState: Parcelable, - val saveStateOnSwitching: Boolean = false + //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 ) } From 2b37d4c6622181a7a0eacd3a2402bf40bc0279a0 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Mon, 30 Sep 2019 21:25:13 +0300 Subject: [PATCH 4/4] FIxed comment codestyle --- .../components/tabbarnavigation/BottomNavigationFragment.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt index bc1acf2..d514716 100644 --- a/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt +++ b/tabbar-navigation/src/main/java/ru/touchin/roboswag/components/tabbarnavigation/BottomNavigationFragment.kt @@ -75,8 +75,10 @@ abstract class BottomNavigationFragment : Fragment() { data class TabData( val viewControllerClass: Class>, 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. + /** + * 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 )