From 45dddff10aeec7e9536b5ab4db056521e31e88d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=91=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=D0=BD=D1=81=D0=BA=D0=B8=D0=B8=CC=86?= Date: Wed, 27 May 2020 20:41:46 +0300 Subject: [PATCH] split basefragment and state saving --- .../activities/BaseActivity.kt | 2 +- .../navigation_base/fragments/BaseFragment.kt | 42 +------------- .../fragments/FragmentWithState.kt | 55 +++++++++++++++++++ .../KeyboardBehaviorDetector.kt | 2 +- 4 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/FragmentWithState.kt diff --git a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/activities/BaseActivity.kt b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/activities/BaseActivity.kt index eda7d0c..be2244e 100644 --- a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/activities/BaseActivity.kt +++ b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/activities/BaseActivity.kt @@ -29,7 +29,7 @@ import androidx.appcompat.app.AppCompatActivity import ru.touchin.roboswag.core.log.Lc import ru.touchin.roboswag.core.log.LcGroup import ru.touchin.roboswag.navigation_base.fragments.LifecycleLoggingObserver -import ru.touchin.roboswag.navigation_viewcontroller.keyboard_resizeable.KeyboardBehaviorDetector +import ru.touchin.roboswag.navigation_base.keyboard_resizeable.KeyboardBehaviorDetector /** * Created by Gavriil Sitnikov on 08/03/2016. diff --git a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/BaseFragment.kt b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/BaseFragment.kt index c8a85df..b38027f 100644 --- a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/BaseFragment.kt +++ b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/BaseFragment.kt @@ -4,8 +4,6 @@ import android.content.Context import android.content.res.ColorStateList import android.graphics.drawable.Drawable import android.os.Bundle -import android.os.Parcel -import android.os.Parcelable import android.view.View import androidx.annotation.ColorInt import androidx.annotation.ColorRes @@ -15,30 +13,12 @@ import androidx.annotation.LayoutRes import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentActivity -import ru.touchin.roboswag.navigation_base.BuildConfig -open class BaseFragment(@LayoutRes layoutRes: Int) : Fragment(layoutRes) { +open class BaseFragment : Fragment { - companion object { - private const val BASE_FRAGMENT_STATE_EXTRA = "BASE_FRAGMENT_STATE_EXTRA" + constructor() : super() - fun args(state: Parcelable?): Bundle = Bundle().also { it.putParcelable(BASE_FRAGMENT_STATE_EXTRA, state) } - - // This method used to check unique state of each fragment. - // If two fragments share same class for state, you should not pass state instance of current fragment to the one you transition to - private fun reserialize(parcelable: T): T { - var parcel = Parcel.obtain() - parcel.writeParcelable(parcelable, 0) - val serializableBytes = parcel.marshall() - parcel.recycle() - parcel = Parcel.obtain() - parcel.unmarshall(serializableBytes, 0, serializableBytes.size) - parcel.setDataPosition(0) - val result = parcel.readParcelable(Thread.currentThread().contextClassLoader) ?: throw IllegalStateException("It must not be null") - parcel.recycle() - return result - } - } + constructor(@LayoutRes layoutRes: Int) : super(layoutRes) protected val view: View @JvmName("requireViewKtx") get() = requireView() @@ -49,21 +29,10 @@ open class BaseFragment(@Layo protected val context: Context @JvmName("requireContextKtx") get() = requireContext() - protected lateinit var state: TState - private set - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) - - state = savedInstanceState?.getParcelable(BASE_FRAGMENT_STATE_EXTRA) - ?: arguments?.getParcelable(BASE_FRAGMENT_STATE_EXTRA) - ?: throw IllegalStateException("Fragment state can't be null") - - if (BuildConfig.DEBUG) { - state = reserialize(state) - } } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -72,11 +41,6 @@ open class BaseFragment(@Layo lifecycle.addObserver(LifecycleLoggingObserver(this)) } - override fun onSaveInstanceState(outState: Bundle) { - super.onSaveInstanceState(outState) - outState.putParcelable(BASE_FRAGMENT_STATE_EXTRA, state) - } - fun findViewById(@IdRes id: Int): T = view.findViewById(id) @ColorInt diff --git a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/FragmentWithState.kt b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/FragmentWithState.kt new file mode 100644 index 0000000..dedf118 --- /dev/null +++ b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/fragments/FragmentWithState.kt @@ -0,0 +1,55 @@ +package ru.touchin.roboswag.navigation_base.fragments + +import android.os.Bundle +import android.os.Parcel +import android.os.Parcelable +import androidx.annotation.LayoutRes +import androidx.fragment.app.FragmentActivity +import ru.touchin.roboswag.navigation_base.BuildConfig + +open class FragmentWithState( + @LayoutRes layoutRes: Int +) : BaseFragment(layoutRes) { + + companion object { + private const val BASE_FRAGMENT_STATE_EXTRA = "BASE_FRAGMENT_STATE_EXTRA" + + fun args(state: Parcelable?): Bundle = Bundle().also { it.putParcelable(BASE_FRAGMENT_STATE_EXTRA, state) } + + // This method used to check unique state of each fragment. + // If two fragments share same class for state, you should not pass state instance of current fragment to the one you transition to + private fun reserialize(parcelable: T): T { + var parcel = Parcel.obtain() + parcel.writeParcelable(parcelable, 0) + val serializableBytes = parcel.marshall() + parcel.recycle() + parcel = Parcel.obtain() + parcel.unmarshall(serializableBytes, 0, serializableBytes.size) + parcel.setDataPosition(0) + val result = parcel.readParcelable(Thread.currentThread().contextClassLoader) ?: throw IllegalStateException("It must not be null") + parcel.recycle() + return result + } + } + + protected lateinit var state: TState + private set + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + state = savedInstanceState?.getParcelable(BASE_FRAGMENT_STATE_EXTRA) + ?: arguments?.getParcelable(BASE_FRAGMENT_STATE_EXTRA) + ?: throw IllegalStateException("Fragment state can't be null") + + if (BuildConfig.DEBUG) { + state = reserialize(state) + } + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putParcelable(BASE_FRAGMENT_STATE_EXTRA, state) + } + +} diff --git a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/keyboard_resizeable/KeyboardBehaviorDetector.kt b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/keyboard_resizeable/KeyboardBehaviorDetector.kt index 7f93232..30774a2 100644 --- a/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/keyboard_resizeable/KeyboardBehaviorDetector.kt +++ b/navigation-base/src/main/java/ru/touchin/roboswag/navigation_base/keyboard_resizeable/KeyboardBehaviorDetector.kt @@ -1,4 +1,4 @@ -package ru.touchin.roboswag.navigation_viewcontroller.keyboard_resizeable +package ru.touchin.roboswag.navigation_base.keyboard_resizeable import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat