Skip to content

Commit

Permalink
Merge pull request #40 from vitaviva/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vitaviva committed May 26, 2021
2 parents 5de6285 + 3df6768 commit fd9c23d
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion library/CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.2.7
## 0.2.8
fix https://github.com/vitaviva/fragivity/issues/37

## 0.2.5
Expand Down
12 changes: 3 additions & 9 deletions library/src/main/java/com/github/fragivity/NavOptionsBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -31,13 +31,7 @@ class NavOptionsBuilder internal constructor() {
also { navOptions.popExitAnim = popExitAnim }

fun setSharedElements(sharedElements: Map<View, String>) =
also {
val list = mutableListOf<Pair<View, String>>()
for ((view, name) in sharedElements) {
list.add(Pair(view, name))
}
navOptions.sharedElements = list
}
also { navOptions.sharedElements = sharedElements }

fun build(): NavOptions {
return navOptions
Expand Down
16 changes: 16 additions & 0 deletions library/src/main/kotlin/androidx/fragment/app/Ext.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,6 +21,8 @@ class FragivityFragmentNavigator(
) : Navigator<FragmentNavigator.Destination>() {

private val backStack = ArrayDeque<Int>()
private val descendingBackStack = backStack.asReversed()

private var mIsPendingAddToBackStackOperation = false
private var mIsPendingPopBackStackOperation = false

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
}
Expand All @@ -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"
}
}
112 changes: 46 additions & 66 deletions library/src/main/kotlin/com/github/fragivity/NavOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -22,52 +25,57 @@ 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<Pair<View, String>>

var sharedElements: Map<View, String>
}

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<Pair<View, String>>()
}
@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<String, Any?>) {
this.arguments = bundleOf(*args)
}

fun NavOptions.applySharedElements(vararg sharedElements: Pair<View, String>) {
this.sharedElements = sharedElementsOf(*sharedElements)
this.sharedElements = mapOf(*sharedElements)
}

fun NavOptions.applySlideInOut() {
Expand Down Expand Up @@ -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<View, String>) =
mutableListOf<Pair<View, String>>().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<View, String> = emptyMap()
}
26 changes: 6 additions & 20 deletions library/src/main/kotlin/com/github/fragivity/swipeback/Ext.kt
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit fd9c23d

Please sign in to comment.