From 6d7ebc5de8f24935991ffdf3e3e7fe4e45d66c51 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Sat, 12 Oct 2019 01:48:18 +0300 Subject: [PATCH 1/2] =?UTF-8?q?Changed=20TabData=20=E2=80=93=20now=20gette?= =?UTF-8?q?r=20of=20field=20`viewControllerState`=20returns=20new=20instan?= =?UTF-8?q?ce=20every=20time=20it=20be=20called=20instead=20of=20returning?= =?UTF-8?q?=20the=20same=20instance.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BottomNavigationFragment.kt | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 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 d514716..cfe88c8 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 @@ -72,14 +72,32 @@ abstract class BottomNavigationFragment : Fragment() { private fun getNavigationActivity() = requireActivity() as BottomNavigationActivity - data class TabData( + 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. */ val saveStateOnSwitching: Boolean = true - ) + ) { + + /** + * It is value as class body field with lazy delegate instead of value as constructor parameter + * to specify custom getter of this field which returns new instance of Parcelable every time it be invoked. + * 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: Parcelable by lazy { + viewControllerState + } + + operator fun component1() = viewControllerClass + + operator fun component2() = viewControllerState + + operator fun component3() = saveStateOnSwitching + + } } From 705f4e9e39be931178796de5de0bcfad95066460 Mon Sep 17 00:00:00 2001 From: Daniil Borisovskii Date: Sat, 12 Oct 2019 19:25:29 +0300 Subject: [PATCH 2/2] Correct copying of `viewControllerState` property has provided --- .../BottomNavigationFragment.kt | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 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 cfe88c8..f17f7e8 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 @@ -1,6 +1,7 @@ package ru.touchin.roboswag.components.tabbarnavigation import android.os.Bundle +import android.os.Parcel import android.os.Parcelable import android.util.SparseArray import android.view.LayoutInflater @@ -83,14 +84,13 @@ abstract class BottomNavigationFragment : Fragment() { ) { /** - * It is value as class body field with lazy delegate instead of value as constructor parameter - * to specify custom getter of this field which returns new instance of Parcelable every time it be invoked. - * 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: Parcelable by lazy { - viewControllerState - } + * 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 @@ -98,6 +98,16 @@ abstract class BottomNavigationFragment : Fragment() { operator fun component3() = saveStateOnSwitching + private fun Parcelable.copy(): Parcelable { + val parcel = Parcel.obtain() + this.writeToParcel(parcel, 0) + parcel.setDataPosition(0) + val result = parcel.readParcelable(Thread.currentThread().contextClassLoader) + ?: throw IllegalStateException("It must not be null") + parcel.recycle() + return result + } + } }