From 37f2cf01a0ec8fd2cbe8a753b460d598201da375 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Fri, 25 Oct 2019 17:12:35 +0300 Subject: [PATCH 1/2] Fixed https://github.com/TouchInstinct/RoboSwag/issues/93 Using approach `parcel.writeParcelable(this, 0)` instead of `this.writeToParcel(parcel, 0)` resolves the problem. --- .../BottomNavigationFragment.kt | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 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 9e008be..34cc2bc 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 @@ -10,7 +10,6 @@ import android.view.ViewGroup import androidx.annotation.IdRes import androidx.fragment.app.Fragment import ru.touchin.roboswag.components.navigation.activities.OnBackPressedListener -import ru.touchin.roboswag.components.navigation.viewcontrollers.EmptyState import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewController abstract class BottomNavigationFragment : Fragment() { @@ -76,7 +75,7 @@ abstract class BottomNavigationFragment : Fragment() { class TabData( val viewControllerClass: Class>, - val viewControllerState: Parcelable, + viewControllerState: Parcelable, /** * It can be useful in some cases when it is necessary to create ViewController * with initial state every time when tab opens. @@ -84,18 +83,28 @@ abstract class BottomNavigationFragment : Fragment() { val saveStateOnSwitching: Boolean = true ) { + /** + * It is value as class body property instead of value as constructor parameter to specify + * custom getter of this field which returns copy of Parcelable every time it be called. + * This is necessary to avoid modifying this value if it would be a value as constructor parameter + * and every getting of this value would return the same instance. + */ + val viewControllerState = viewControllerState + get() = field.copy() + operator fun component1() = viewControllerClass operator fun component2() = viewControllerState operator fun component3() = saveStateOnSwitching - private fun Parcelable.copy(): Parcelable { + private fun Parcelable.copy(): T { val parcel = Parcel.obtain() - this.writeToParcel(parcel, 0) + parcel.writeParcelable(this, 0) parcel.setDataPosition(0) - val result = parcel.readParcelable(Thread.currentThread().contextClassLoader) - ?: throw IllegalStateException("It must not be null") + val result = parcel.readParcelable( + javaClass.classLoader ?: Thread.currentThread().contextClassLoader + ) ?: throw IllegalStateException("It must not be null") parcel.recycle() return result } From 9ca661f60e2f7976cc70a28d80f23e85799641d2 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Mon, 28 Oct 2019 12:15:27 +0300 Subject: [PATCH 2/2] Fixed comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * https://github.com/TouchInstinct/RoboSwag/pull/96#discussion_r339450162 – changed exceptions desription * https://github.com/TouchInstinct/RoboSwag/pull/96#discussion_r339450491 – provided Empty state checking to avoid useless calculations --- .../BottomNavigationFragment.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 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 34cc2bc..c1364fc 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 @@ -10,6 +10,7 @@ import android.view.ViewGroup import androidx.annotation.IdRes import androidx.fragment.app.Fragment import ru.touchin.roboswag.components.navigation.activities.OnBackPressedListener +import ru.touchin.roboswag.components.navigation.viewcontrollers.EmptyState import ru.touchin.roboswag.components.navigation.viewcontrollers.ViewController abstract class BottomNavigationFragment : Fragment() { @@ -98,16 +99,19 @@ abstract class BottomNavigationFragment : Fragment() { operator fun component3() = saveStateOnSwitching - private fun Parcelable.copy(): T { - val parcel = Parcel.obtain() - parcel.writeParcelable(this, 0) - parcel.setDataPosition(0) - val result = parcel.readParcelable( - javaClass.classLoader ?: Thread.currentThread().contextClassLoader - ) ?: throw IllegalStateException("It must not be null") - parcel.recycle() - return result - } + private fun Parcelable.copy(): Parcelable = + if (this is EmptyState) { + EmptyState + } else { + val parcel = Parcel.obtain() + parcel.writeParcelable(this, 0) + parcel.setDataPosition(0) + val result = parcel.readParcelable( + javaClass.classLoader ?: Thread.currentThread().contextClassLoader + ) ?: throw IllegalStateException("Failed to copy tab state") + parcel.recycle() + result + } }