From e2a134eea627d2b06a4e59c192bc9a655e8e6700 Mon Sep 17 00:00:00 2001 From: "A.Badakhshan" Date: Sat, 23 Dec 2023 16:37:06 +0330 Subject: [PATCH] 0.8.0 --- .../com/alirezabdn/whygoogle/MainActivity.kt | 9 ++-- .../com/alirezabdn/whygoogle/MainFragment.kt | 4 +- .../alirezabdn/whygoogle/SecondFragment.kt | 5 +- whygoogle/build.gradle | 2 +- .../activity/SwipableWhyGoogleActivity.kt | 54 +++++++++---------- .../helper/FragmentArgumentDelegate.kt | 42 +++++++++++++++ 6 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 whygoogle/src/main/java/ir/ayantech/whygoogle/helper/FragmentArgumentDelegate.kt diff --git a/app/src/main/java/com/alirezabdn/whygoogle/MainActivity.kt b/app/src/main/java/com/alirezabdn/whygoogle/MainActivity.kt index c52d624..23da6c4 100644 --- a/app/src/main/java/com/alirezabdn/whygoogle/MainActivity.kt +++ b/app/src/main/java/com/alirezabdn/whygoogle/MainActivity.kt @@ -13,14 +13,15 @@ import ir.ayantech.whygoogle.widget.SwipeBackContainer class MainActivity : SwipableWhyGoogleActivity() { - override val directionCareRtl: Boolean get() = true + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - start(MainFragment(), onFragmentCreationEndedCallback = { - Log.d("WhG", "onFragmentCreationEndedCallback") - }) + if (savedInstanceState == null) + start(MainFragment(), onFragmentCreationEndedCallback = { + Log.d("WhG", "onFragmentCreationEndedCallback") + }) } override val binder: (LayoutInflater) -> ActivityMainBinding diff --git a/app/src/main/java/com/alirezabdn/whygoogle/MainFragment.kt b/app/src/main/java/com/alirezabdn/whygoogle/MainFragment.kt index 8bc5ec2..5f72f4d 100644 --- a/app/src/main/java/com/alirezabdn/whygoogle/MainFragment.kt +++ b/app/src/main/java/com/alirezabdn/whygoogle/MainFragment.kt @@ -23,7 +23,9 @@ class MainFragment : WhyGoogleFragment() { go.setOnClickListener { start(MainFragment()) } - val sec = SecondFragment() + val sec = SecondFragment().also { + it.name = "hi" + } go2.setOnClickListener { start(sec) } diff --git a/app/src/main/java/com/alirezabdn/whygoogle/SecondFragment.kt b/app/src/main/java/com/alirezabdn/whygoogle/SecondFragment.kt index f47f2ed..7d76468 100644 --- a/app/src/main/java/com/alirezabdn/whygoogle/SecondFragment.kt +++ b/app/src/main/java/com/alirezabdn/whygoogle/SecondFragment.kt @@ -5,10 +5,12 @@ import android.view.ViewGroup import android.widget.Toast import com.alirezabdn.whygoogle.databinding.MainFragmentBinding import ir.ayantech.whygoogle.fragment.WhyGoogleFragment +import ir.ayantech.whygoogle.helper.fragmentArgument import ir.ayantech.whygoogle.helper.makeGone -import ir.ayantech.whygoogle.standard.LaunchMode class SecondFragment : WhyGoogleFragment() { + + var name: String? by fragmentArgument() override fun onCreate() { super.onCreate() binding.go2.makeGone() @@ -18,6 +20,7 @@ class SecondFragment : WhyGoogleFragment() { binding.executeBtn.setOnClickListener { preventFromPop = false } + binding.executeBtn.text = name } override var lockedSwipe: Boolean = true diff --git a/whygoogle/build.gradle b/whygoogle/build.gradle index 2f7e263..3e165bc 100644 --- a/whygoogle/build.gradle +++ b/whygoogle/build.gradle @@ -11,7 +11,7 @@ android { minSdkVersion 17 targetSdkVersion 30 versionCode 1 - versionName "0.7.0" + versionName "0.8.0" } buildFeatures { viewBinding true diff --git a/whygoogle/src/main/java/ir/ayantech/whygoogle/activity/SwipableWhyGoogleActivity.kt b/whygoogle/src/main/java/ir/ayantech/whygoogle/activity/SwipableWhyGoogleActivity.kt index b9d1d8e..a40ef02 100644 --- a/whygoogle/src/main/java/ir/ayantech/whygoogle/activity/SwipableWhyGoogleActivity.kt +++ b/whygoogle/src/main/java/ir/ayantech/whygoogle/activity/SwipableWhyGoogleActivity.kt @@ -66,33 +66,33 @@ abstract class SwipableWhyGoogleActivity : AppCompatActivity(), if (directionCareRtl) { window.decorView.layoutDirection = View.LAYOUT_DIRECTION_RTL } - fragmentHost.listener( - onPageSettled = { - val previousCount = getFragmentCount() - if (fragmentHost.currentItem <= getFragmentCount() - 2 && getTopFragment()?.preventFromPop == true) { - fragmentHost.setCurrentItem(getFragmentCount() - 1, true) - getTopFragment()?.onBackPressed() - return@listener - } - while (fragmentHost.currentItem <= getFragmentCount() - 2) { - fragmentStack.removeLast() - } - if (previousCount >= fragmentHost.currentItem + 2) { - whyGoogleFragmentAdapter.notifyItemRangeRemoved( - fragmentHost.currentItem + 1, - previousCount - fragmentHost.currentItem - 1 - ) - fragmentStack.lastOrNull()?.onFragmentVisible() - fragmentStack.lastOrNull()?.onBackToFragment() - onTopFragmentChanged(fragmentStack.last()) - } - if (lastKnownFragment != fragmentStack.lastOrNull()) { - fragmentStack.lastOrNull()?.onEnterAnimationEnded() - lastKnownFragment = fragmentStack.lastOrNull() - } - }, - onPageScrolled = {} - ) + fragmentHost.listener( + onPageSettled = { + val previousCount = getFragmentCount() + if (fragmentHost.currentItem <= getFragmentCount() - 2 && getTopFragment()?.preventFromPop == true) { + fragmentHost.setCurrentItem(getFragmentCount() - 1, true) + getTopFragment()?.onBackPressed() + return@listener + } + while (fragmentHost.currentItem <= getFragmentCount() - 2) { + fragmentStack.removeLast() + } + if (previousCount >= fragmentHost.currentItem + 2) { + whyGoogleFragmentAdapter.notifyItemRangeRemoved( + fragmentHost.currentItem + 1, + previousCount - fragmentHost.currentItem - 1 + ) + fragmentStack.lastOrNull()?.onFragmentVisible() + fragmentStack.lastOrNull()?.onBackToFragment() + onTopFragmentChanged(fragmentStack.last()) + } + if (lastKnownFragment != fragmentStack.lastOrNull()) { + fragmentStack.lastOrNull()?.onEnterAnimationEnded() + lastKnownFragment = fragmentStack.lastOrNull() + } + }, + onPageScrolled = {} + ) } fun accessViews(block: T.() -> Unit) { diff --git a/whygoogle/src/main/java/ir/ayantech/whygoogle/helper/FragmentArgumentDelegate.kt b/whygoogle/src/main/java/ir/ayantech/whygoogle/helper/FragmentArgumentDelegate.kt new file mode 100644 index 0000000..a238fb8 --- /dev/null +++ b/whygoogle/src/main/java/ir/ayantech/whygoogle/helper/FragmentArgumentDelegate.kt @@ -0,0 +1,42 @@ +package ir.ayantech.whygoogle.helper + +import android.os.Bundle +import android.os.Parcelable +import ir.ayantech.whygoogle.fragment.WhyGoogleFragment +import java.io.Serializable +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +class FragmentArgumentDelegate( + private val key: String? = null +) : ReadWriteProperty { + + override fun getValue(thisRef: Any?, property: KProperty<*>): T { + val arguments = (thisRef as? WhyGoogleFragment<*>)?.arguments + return arguments?.get(key ?: property.name) as? T + ?: throw IllegalStateException("Property ${property.name} not initialized") + } + + override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { + val arguments = (thisRef as? WhyGoogleFragment<*>)?.arguments ?: Bundle() + val finalKey = this.key ?: property.name + + when (value) { + is String -> arguments.putString(finalKey, value) + is Int -> arguments.putInt(finalKey, value) + is Long -> arguments.putLong(finalKey, value) + is Double -> arguments.putDouble(finalKey, value) + is Boolean -> arguments.putBoolean(finalKey, value) + is Float -> arguments.putFloat(finalKey, value) + is Char -> arguments.putChar(finalKey, value) + is Short -> arguments.putShort(finalKey, value) + is Byte -> arguments.putByte(finalKey, value) + is Serializable -> arguments.putSerializable(finalKey, value) + is Parcelable -> arguments.putParcelable(finalKey, value) + } + + (thisRef as? WhyGoogleFragment<*>)?.arguments = arguments + } +} + +inline fun fragmentArgument(key: String? = null) = FragmentArgumentDelegate(key) \ No newline at end of file