Merge pull request #86 from TouchInstinct/refactor/tabbar_navigation_modifying

TabData modifying
This commit is contained in:
Даниил Борисовский 2019-10-14 13:25:58 +03:00 committed by GitHub
commit 4846e7818c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 3 deletions

View File

@ -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
@ -72,14 +73,41 @@ abstract class BottomNavigationFragment : Fragment() {
private fun getNavigationActivity() = requireActivity() as BottomNavigationActivity
data class TabData(
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.
*/
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
}
}
}