Merge pull request #96 from TouchInstinct/fix/issue-93_copying_state_does_not_works
Fix of https://github.com/TouchInstinct/RoboSwag/issues/93
This commit is contained in:
commit
991f1626f2
|
|
@ -76,7 +76,7 @@ abstract class BottomNavigationFragment : Fragment() {
|
|||
|
||||
class TabData(
|
||||
val viewControllerClass: Class<out ViewController<*, *>>,
|
||||
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,21 +84,34 @@ 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 {
|
||||
val parcel = Parcel.obtain()
|
||||
this.writeToParcel(parcel, 0)
|
||||
parcel.setDataPosition(0)
|
||||
val result = parcel.readParcelable<Parcelable>(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<Parcelable>(
|
||||
javaClass.classLoader ?: Thread.currentThread().contextClassLoader
|
||||
) ?: throw IllegalStateException("Failed to copy tab state")
|
||||
parcel.recycle()
|
||||
result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue