diff --git a/app/src/main/java/com/github/fragivity/example/swipeback/SwipeBackFragment.kt b/app/src/main/java/com/github/fragivity/example/swipeback/SwipeBackFragment.kt index 8e98c37..44c3b59 100644 --- a/app/src/main/java/com/github/fragivity/example/swipeback/SwipeBackFragment.kt +++ b/app/src/main/java/com/github/fragivity/example/swipeback/SwipeBackFragment.kt @@ -4,10 +4,10 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.fragment.app.setEnableGesture import com.github.fragivity.example.AbsBaseFragment import com.github.fragivity.example.R import com.github.fragivity.example.databinding.FragmentSwipeBackBinding +import com.github.fragivity.swipeback.setEnableGesture class SwipeBackFragment : AbsBaseFragment() { diff --git a/gradle.properties b/gradle.properties index 27baa63..6da7361 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,5 +23,5 @@ kotlin.code.style=official # bintray upload USER_ORG=vitaviva ARTIFACT_GROUP=com.github.fragivity -ARTIFACT_VERSION=0.2.7 +ARTIFACT_VERSION=0.2.8 WEB_SITE=https://github.com/vitaviva/fragivity diff --git a/library/CHANGE_LOG.md b/library/CHANGE_LOG.md index 991f9cb..e4eae95 100644 --- a/library/CHANGE_LOG.md +++ b/library/CHANGE_LOG.md @@ -1,4 +1,4 @@ -## 0.2.7 +## 0.2.8 fix https://github.com/vitaviva/fragivity/issues/37 ## 0.2.5 diff --git a/library/src/main/java/com/github/fragivity/NavOptionsBuilder.kt b/library/src/main/java/com/github/fragivity/NavOptionsBuilder.kt index b165b24..4473eb8 100644 --- a/library/src/main/java/com/github/fragivity/NavOptionsBuilder.kt +++ b/library/src/main/java/com/github/fragivity/NavOptionsBuilder.kt @@ -7,13 +7,13 @@ import androidx.annotation.AnimatorRes class NavOptionsBuilder internal constructor() { - private val navOptions = `$NavOptionsDefault`() + private val navOptions = navOptions() fun setLaunchMode(mode: LaunchMode) = also { navOptions.launchMode = mode } fun popSelf(enable: Boolean) = - also { navOptions.popSelf = enable} + also { navOptions.popSelf = enable } fun setArguments(bundle: Bundle) = also { navOptions.arguments = bundle } @@ -31,13 +31,7 @@ class NavOptionsBuilder internal constructor() { also { navOptions.popExitAnim = popExitAnim } fun setSharedElements(sharedElements: Map) = - also { - val list = mutableListOf>() - for ((view, name) in sharedElements) { - list.add(Pair(view, name)) - } - navOptions.sharedElements = list - } + also { navOptions.sharedElements = sharedElements } fun build(): NavOptions { return navOptions diff --git a/library/src/main/kotlin/androidx/fragment/app/Ext.kt b/library/src/main/kotlin/androidx/fragment/app/Ext.kt new file mode 100644 index 0000000..f3111e7 --- /dev/null +++ b/library/src/main/kotlin/androidx/fragment/app/Ext.kt @@ -0,0 +1,16 @@ +package androidx.fragment.app + +import android.view.View +import androidx.fragment.R +import androidx.lifecycle.ViewTreeLifecycleOwner +import androidx.lifecycle.ViewTreeViewModelStoreOwner +import androidx.savedstate.SavedStateRegistryOwner +import androidx.savedstate.ViewTreeSavedStateRegistryOwner + +internal fun Fragment.setView(view: View) { + view.setTag(R.id.fragment_container_view_tag, this) + ViewTreeLifecycleOwner.set(view, viewLifecycleOwner) + ViewTreeViewModelStoreOwner.set(view, this) + ViewTreeSavedStateRegistryOwner.set(view, viewLifecycleOwner as SavedStateRegistryOwner) + mView = view +} \ No newline at end of file diff --git a/library/src/main/kotlin/androidx/fragment/app/FragivityFragmentNavigator.kt b/library/src/main/kotlin/androidx/fragment/app/FragivityFragmentNavigator.kt index 3b00aff..9f515d4 100644 --- a/library/src/main/kotlin/androidx/fragment/app/FragivityFragmentNavigator.kt +++ b/library/src/main/kotlin/androidx/fragment/app/FragivityFragmentNavigator.kt @@ -10,7 +10,6 @@ import androidx.navigation.NavDestination import androidx.navigation.NavOptions import androidx.navigation.Navigator import androidx.navigation.fragment.FragmentNavigator -import com.github.fragivity.KEY_POP_SELF import com.github.fragivity.plusAssign import com.github.fragivity.replaceAll @@ -22,6 +21,8 @@ class FragivityFragmentNavigator( ) : Navigator() { private val backStack = ArrayDeque() + private val descendingBackStack = backStack.asReversed() + private var mIsPendingAddToBackStackOperation = false private var mIsPendingPopBackStackOperation = false @@ -195,7 +196,7 @@ class FragivityFragmentNavigator( } var backStackIndex = fragmentBackStackCount - 1 - val backStackIterator = backStack.asReversed().iterator() + val backStackIterator = descendingBackStack.iterator() while (backStackIterator.hasNext() && backStackIndex >= 0) { val destId = backStackIterator.next() val fragmentDestId = getDestinationId( @@ -250,7 +251,7 @@ class FragivityFragmentNavigator( if (backStack.isEmpty()) return null var index = backStack.size - 1 - backStack.asReversed().forEach { destId -> + descendingBackStack.forEach { destId -> if (destinationId == destId) { return fragmentManager.findFragment(index, destId) } @@ -270,5 +271,6 @@ class FragivityFragmentNavigator( companion object { private const val TAG = "FragivityNavigator" private const val KEY_BACK_STACK_IDS = "myFragmentNavigator:backStackIds" + internal const val KEY_POP_SELF = "Fragivity:PopSelf" } } diff --git a/library/src/main/kotlin/com/github/fragivity/NavOptions.kt b/library/src/main/kotlin/com/github/fragivity/NavOptions.kt index 8a4cc15..547b68a 100644 --- a/library/src/main/kotlin/com/github/fragivity/NavOptions.kt +++ b/library/src/main/kotlin/com/github/fragivity/NavOptions.kt @@ -5,8 +5,11 @@ import android.view.View import androidx.annotation.AnimRes import androidx.annotation.AnimatorRes import androidx.core.os.bundleOf +import androidx.fragment.app.FragivityFragmentNavigator.Companion.KEY_POP_SELF import androidx.navigation.fragment.FragmentNavigator +private typealias NavigationOptionsBuilder = androidx.navigation.NavOptions.Builder + enum class LaunchMode { STANDARD, SINGLE_TOP, SINGLE_TASK } @@ -22,44 +25,49 @@ interface NavOptions { @set:JvmSynthetic var arguments: Bundle - @set:AnimRes - @set:AnimatorRes - @set:JvmSynthetic + @set:[JvmSynthetic AnimRes AnimatorRes] var enterAnim: Int - @set:AnimRes - @set:AnimatorRes - @set:JvmSynthetic + @set:[JvmSynthetic AnimRes AnimatorRes] var exitAnim: Int - @set:AnimRes - @set:AnimatorRes - @set:JvmSynthetic + @set:[JvmSynthetic AnimRes AnimatorRes] var popEnterAnim: Int - @set:AnimRes - @set:AnimatorRes - @set:JvmSynthetic + @set:[JvmSynthetic AnimRes AnimatorRes] var popExitAnim: Int @set:JvmSynthetic - var sharedElements: List> - + var sharedElements: Map } -internal class `$NavOptionsDefault` : NavOptions { - override var launchMode: LaunchMode = LaunchMode.STANDARD - override var popSelf: Boolean = false - override var arguments: Bundle = Bundle.EMPTY - override var enterAnim = -1 - override var exitAnim = -1 - override var popEnterAnim = -1 - override var popExitAnim = -1 - override var sharedElements = emptyList>() -} +@JvmSynthetic +internal fun NavOptions.toBundle() = + if (popSelf) { + Bundle(arguments).apply { putBoolean(KEY_POP_SELF, true) } + } else arguments + +@JvmSynthetic +internal fun NavOptions.totOptions(destinationId: Int? = null) = + NavigationOptionsBuilder().apply { + setEnterAnim(enterAnim) + setExitAnim(exitAnim) + setPopEnterAnim(popEnterAnim) + setPopExitAnim(popExitAnim) + setLaunchSingleTop(launchMode == LaunchMode.SINGLE_TOP) + if (launchMode == LaunchMode.SINGLE_TASK && destinationId != null) { + setPopUpTo(destinationId, true) + } + }.build() + +@JvmSynthetic +internal fun NavOptions.totExtras() = + FragmentNavigator.Extras.Builder() + .addSharedElements(sharedElements) + .build() fun NavOptions.applyLaunchMode(mode: LaunchMode) { - launchMode = mode + this.launchMode = mode } fun NavOptions.applyArguments(vararg args: Pair) { @@ -67,7 +75,7 @@ fun NavOptions.applyArguments(vararg args: Pair) { } fun NavOptions.applySharedElements(vararg sharedElements: Pair) { - this.sharedElements = sharedElementsOf(*sharedElements) + this.sharedElements = mapOf(*sharedElements) } fun NavOptions.applySlideInOut() { @@ -98,45 +106,17 @@ fun NavOptions.applyHorizontalInOut() { popExitAnim = R.anim.h_fragment_pop_exit } -internal const val KEY_POP_SELF = "Fragivity:PopSelf" - -@JvmSynthetic -internal fun NavOptions.toBundle(): Bundle = - (arguments.takeIf { it != Bundle.EMPTY } ?: Bundle()).apply { - putBoolean(KEY_POP_SELF, this@toBundle.popSelf) - } - -@JvmSynthetic -internal fun NavOptions.totOptions( - destinationId: Int? = null -): androidx.navigation.NavOptions = - androidx.navigation.NavOptions.Builder().apply { - setEnterAnim(enterAnim) - setExitAnim(exitAnim) - setPopEnterAnim(popEnterAnim) - setPopExitAnim(popExitAnim) - setLaunchSingleTop(launchMode == LaunchMode.SINGLE_TOP) - if (launchMode == LaunchMode.SINGLE_TASK && destinationId != null) { - setPopUpTo(destinationId, true) - } - }.build() - - -@JvmSynthetic -internal fun NavOptions.totExtras(): FragmentNavigator.Extras = - FragmentNavigator.Extras.Builder().apply { - sharedElements.forEach { (view, name) -> - addSharedElement(view, name) - } - }.build() - -fun sharedElementsOf(vararg sharedElements: Pair) = - mutableListOf>().apply { - sharedElements.forEach { (view, name) -> - add(view to name) - } - } - fun navOptions(optionsBuilder: NavOptions.() -> Unit = {}): NavOptions { - return `$NavOptionsDefault`().apply(optionsBuilder) + return NavOptionsDefault().apply(optionsBuilder) +} + +private class NavOptionsDefault : NavOptions { + override var launchMode: LaunchMode = LaunchMode.STANDARD + override var popSelf: Boolean = false + override var arguments: Bundle = Bundle.EMPTY + override var enterAnim = -1 + override var exitAnim = -1 + override var popEnterAnim = -1 + override var popExitAnim = -1 + override var sharedElements: Map = emptyMap() } diff --git a/library/src/main/kotlin/com/github/fragivity/swipeback/Ext.kt b/library/src/main/kotlin/com/github/fragivity/swipeback/Ext.kt index f5fd944..4f95f6f 100644 --- a/library/src/main/kotlin/com/github/fragivity/swipeback/Ext.kt +++ b/library/src/main/kotlin/com/github/fragivity/swipeback/Ext.kt @@ -1,25 +1,19 @@ @file:JvmName("SwipeBackUtil") -package androidx.fragment.app +package com.github.fragivity.swipeback import android.graphics.Color import android.view.View import android.view.ViewGroup -import androidx.fragment.R import androidx.fragment.app.Fragment +import androidx.fragment.app.setView import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleEventObserver -import androidx.lifecycle.ViewTreeLifecycleOwner -import androidx.lifecycle.ViewTreeViewModelStoreOwner -import androidx.savedstate.SavedStateRegistryOwner -import androidx.savedstate.ViewTreeSavedStateRegistryOwner import com.github.fragivity.appendBackground import com.github.fragivity.navigator import com.github.fragivity.pop -import com.github.fragivity.swipeback.SwipeBackLayout - -private fun Fragment.attachToSwipeBack(view: View): View { +private fun Fragment.attachToSwipeBack(view: View): SwipeBackLayout { val swipeBackLayout = SwipeBackLayout(requireContext()).apply { layoutParams = ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, @@ -52,19 +46,11 @@ fun Fragment.setEnableGesture(enable: Boolean) { val parent = rootView.parent as ViewGroup parent.removeView(rootView) - rootView = attachToSwipeBack(rootView).also { - it.setTag(R.id.fragment_container_view_tag, this) - ViewTreeLifecycleOwner.set(it, viewLifecycleOwner) - ViewTreeViewModelStoreOwner.set(it, this) - ViewTreeSavedStateRegistryOwner.set(it, viewLifecycleOwner as SavedStateRegistryOwner) - } - + rootView = attachToSwipeBack(rootView) + setView(rootView) parent.addView(rootView) - this.mView = rootView } - - (rootView as SwipeBackLayout).setEnableGesture(enable) - + rootView.setEnableGesture(enable) } /** diff --git a/library/src/main/kotlin/com/github/fragivity/swipeback/SwipeBackLayout.java b/library/src/main/kotlin/com/github/fragivity/swipeback/SwipeBackLayout.java index 8c130b6..6eaadb6 100644 --- a/library/src/main/kotlin/com/github/fragivity/swipeback/SwipeBackLayout.java +++ b/library/src/main/kotlin/com/github/fragivity/swipeback/SwipeBackLayout.java @@ -23,7 +23,6 @@ import androidx.customview.widget.ViewDragHelper; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.SwipeBackUtil; import com.github.fragivity.R;