Skip to content

Commit

Permalink
0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Badakhshan committed Dec 23, 2023
1 parent bc72468 commit e2a134e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
9 changes: 5 additions & 4 deletions app/src/main/java/com/alirezabdn/whygoogle/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import ir.ayantech.whygoogle.widget.SwipeBackContainer

class MainActivity : SwipableWhyGoogleActivity<ActivityMainBinding>() {


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
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/alirezabdn/whygoogle/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class MainFragment : WhyGoogleFragment<MainFragmentBinding>() {
go.setOnClickListener {
start(MainFragment())
}
val sec = SecondFragment()
val sec = SecondFragment().also {
it.name = "hi"
}
go2.setOnClickListener {
start(sec)
}
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/alirezabdn/whygoogle/SecondFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainFragmentBinding>() {

var name: String? by fragmentArgument()
override fun onCreate() {
super.onCreate()
binding.go2.makeGone()
Expand All @@ -18,6 +20,7 @@ class SecondFragment : WhyGoogleFragment<MainFragmentBinding>() {
binding.executeBtn.setOnClickListener {
preventFromPop = false
}
binding.executeBtn.text = name
}

override var lockedSwipe: Boolean = true
Expand Down
2 changes: 1 addition & 1 deletion whygoogle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "0.7.0"
versionName "0.8.0"
}
buildFeatures {
viewBinding true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,33 @@ abstract class SwipableWhyGoogleActivity<T : ViewBinding> : 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T : Any?>(
private val key: String? = null
) : ReadWriteProperty<Any?, T> {

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 <reified T : Any?> fragmentArgument(key: String? = null) = FragmentArgumentDelegate<T>(key)

0 comments on commit e2a134e

Please sign in to comment.