Skip to content

Commit

Permalink
Refactor ScreenEventDelegate
Browse files Browse the repository at this point in the history
I simplified the implementation. It now relies on `onViewAnimationStart`
/ `onViewAnimationEnd` callbacks of the fragment wrappers.
  • Loading branch information
kkafar committed Jan 31, 2025
1 parent 224bfdd commit cdd0d56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.widget.LinearLayout
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.animation.addListener
import androidx.core.view.WindowInsetsCompat
import com.facebook.react.uimanager.PixelUtil
import com.facebook.react.uimanager.PointerEvents
Expand All @@ -46,6 +47,7 @@ import com.swmansion.rnscreens.bottomsheet.useThreeDetents
import com.swmansion.rnscreens.bottomsheet.useTwoDetents
import com.swmansion.rnscreens.bottomsheet.usesFormSheetPresentation
import com.swmansion.rnscreens.events.ScreenDismissedEvent
import com.swmansion.rnscreens.events.ScreenEventDelegate
import com.swmansion.rnscreens.ext.recycle
import com.swmansion.rnscreens.transition.ExternalBoundaryValuesEvaluator
import com.swmansion.rnscreens.utils.DeviceUtils
Expand Down Expand Up @@ -145,7 +147,12 @@ class ScreenStackFragment :

override fun onViewAnimationEnd() {
super.onViewAnimationEnd()

// Rely on guards inside the callee to detect whether this was indeed appear transition.
notifyViewAppearTransitionEnd()

// Rely on guards inside the callee to detect whether this was indeed removal transition.
screen.endRemovalTransition()
}

private fun notifyViewAppearTransitionEnd() {
Expand Down Expand Up @@ -359,6 +366,7 @@ class ScreenStackFragment :
}
animatorSet.play(alphaAnimator).with(slideAnimator)
}
animatorSet.addListener(ScreenEventDelegate(this))
return animatorSet
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,11 @@
package com.swmansion.rnscreens.events

import android.util.Log
import androidx.transition.Transition
import com.swmansion.rnscreens.ScreenEventDispatcher
import com.swmansion.rnscreens.ScreenFragment
import android.animation.Animator
import com.swmansion.rnscreens.ScreenFragmentWrapper

class ScreenEventDelegate(private val eventDispatcher: ScreenEventDispatcher, private val wrapper: ScreenFragmentWrapper, private val direction: TransitionDirection): Transition.TransitionListener {
class ScreenEventDelegate(private val wrapper: ScreenFragmentWrapper) : Animator.AnimatorListener {
private var currentState: LifecycleState = LifecycleState.INITIALIZED

override fun onTransitionStart(transition: Transition) {
if (currentState < LifecycleState.START_DISPATCHED) {
Log.i(TAG, "onTransitionStart")
val eventType = if (direction === TransitionDirection.FORWARD) {
ScreenFragment.ScreenLifecycleEvent.WILL_APPEAR
} else {
ScreenFragment.ScreenLifecycleEvent.WILL_DISAPPEAR
}
eventDispatcher.dispatchLifecycleEvent(eventType, wrapper)
progressState()
}
}

override fun onTransitionEnd(transition: Transition) {
if (currentState < LifecycleState.END_DISPATCHED) {
Log.i(TAG, "onTransitionEnd")
val eventType = if (direction === TransitionDirection.FORWARD) {
ScreenFragment.ScreenLifecycleEvent.DID_APPEAR
} else {
ScreenFragment.ScreenLifecycleEvent.DID_DISAPPEAR
}
eventDispatcher.dispatchLifecycleEvent(eventType, wrapper)
transition.removeListener(this)
progressState()
wrapper.screen.endRemovalTransition()
}
}

override fun onTransitionCancel(transition: Transition) = Unit

override fun onTransitionPause(transition: Transition) = Unit

override fun onTransitionResume(transition: Transition) = Unit

private fun progressState() {
currentState = when (currentState) {
LifecycleState.INITIALIZED -> LifecycleState.START_DISPATCHED
Expand All @@ -51,21 +14,31 @@ class ScreenEventDelegate(private val eventDispatcher: ScreenEventDispatcher, pr
}
}

private fun resetState() {
currentState = LifecycleState.INITIALIZED
override fun onAnimationStart(animation: Animator) {
if (currentState === LifecycleState.INITIALIZED) {
progressState()
wrapper.onViewAnimationStart()
}
}

override fun onAnimationEnd(animation: Animator) {
if (currentState === LifecycleState.START_DISPATCHED) {
progressState()
animation.removeListener(this)
wrapper.onViewAnimationEnd()
}
}

override fun onAnimationCancel(animation: Animator) = Unit

override fun onAnimationRepeat(animation: Animator) = Unit

private enum class LifecycleState {
INITIALIZED,
START_DISPATCHED,
END_DISPATCHED,
}

enum class TransitionDirection {
FORWARD,
BACKWARD,
}

companion object {
const val TAG = "ScreenEventDelegate"
}
Expand Down

0 comments on commit cdd0d56

Please sign in to comment.