-
-
Notifications
You must be signed in to change notification settings - Fork 575
fix(Android): missing transition events with formSheet presentation #2682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
android/src/main/java/com/swmansion/rnscreens/events/ScreenAnimationDelegate.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.swmansion.rnscreens.events | ||
|
||
import android.animation.Animator | ||
import android.util.Log | ||
import com.swmansion.rnscreens.ScreenStackFragmentWrapper | ||
|
||
// The goal is to make this universal delegate for handling animation progress related logic. | ||
// At this moment this class works only with form sheet presentation. | ||
class ScreenAnimationDelegate( | ||
private val wrapper: ScreenStackFragmentWrapper, | ||
private val eventEmitter: ScreenEventEmitter?, | ||
private val animationType: AnimationType, | ||
) : Animator.AnimatorListener { | ||
enum class AnimationType { | ||
ENTER, | ||
EXIT | ||
} | ||
|
||
private var currentState: LifecycleState = LifecycleState.INITIALIZED | ||
|
||
private fun progressState() { | ||
currentState = | ||
when (currentState) { | ||
LifecycleState.INITIALIZED -> LifecycleState.START_DISPATCHED | ||
LifecycleState.START_DISPATCHED -> LifecycleState.END_DISPATCHED | ||
LifecycleState.END_DISPATCHED -> LifecycleState.END_DISPATCHED | ||
} | ||
} | ||
|
||
override fun onAnimationStart(animation: Animator) { | ||
if (currentState === LifecycleState.INITIALIZED) { | ||
progressState() | ||
|
||
// These callbacks do not work as expected from this call site, TODO: investigate it. | ||
// To fix it quickly we emit required events manually | ||
// wrapper.onViewAnimationStart() | ||
|
||
when (animationType) { | ||
AnimationType.ENTER -> eventEmitter?.dispatchOnWillAppear() | ||
AnimationType.EXIT -> eventEmitter?.dispatchOnWillDisappear() | ||
} | ||
|
||
val isExitAnimation = animationType === AnimationType.EXIT | ||
eventEmitter?.dispatchTransitionProgress( | ||
0.0f, | ||
isExitAnimation, | ||
isExitAnimation, | ||
) | ||
} | ||
} | ||
|
||
override fun onAnimationEnd(animation: Animator) { | ||
if (currentState === LifecycleState.START_DISPATCHED) { | ||
progressState() | ||
animation.removeListener(this) | ||
|
||
// wrapper.onViewAnimationEnd() | ||
|
||
when (animationType) { | ||
AnimationType.ENTER -> eventEmitter?.dispatchOnAppear() | ||
AnimationType.EXIT -> eventEmitter?.dispatchOnDisappear() | ||
} | ||
|
||
val isExitAnimation = animationType === AnimationType.EXIT | ||
eventEmitter?.dispatchTransitionProgress( | ||
1.0f, | ||
isExitAnimation, | ||
isExitAnimation, | ||
) | ||
|
||
wrapper.screen.endRemovalTransition() | ||
} | ||
} | ||
|
||
override fun onAnimationCancel(animation: Animator) = Unit | ||
|
||
override fun onAnimationRepeat(animation: Animator) = Unit | ||
|
||
private enum class LifecycleState { | ||
INITIALIZED, | ||
START_DISPATCHED, | ||
END_DISPATCHED, | ||
} | ||
|
||
companion object { | ||
const val TAG = "ScreenEventDelegate" | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
android/src/main/java/com/swmansion/rnscreens/events/ScreenEventDelegate.kt
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
android/src/main/java/com/swmansion/rnscreens/events/ScreenEventEmitter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.swmansion.rnscreens.events | ||
|
||
import com.facebook.react.uimanager.UIManagerHelper | ||
import com.swmansion.rnscreens.Screen | ||
import com.swmansion.rnscreens.ScreenFragment | ||
|
||
// TODO: Consider taking weak ref here or accepting screen as argument in every method | ||
// to avoid reference cycle. | ||
class ScreenEventEmitter(val screen: Screen) { | ||
val reactEventDispatcher | ||
get() = screen.reactEventDispatcher | ||
|
||
val reactSurfaceId | ||
get() = UIManagerHelper.getSurfaceId(screen) | ||
|
||
fun dispatchOnWillAppear() = | ||
reactEventDispatcher?.dispatchEvent(ScreenWillAppearEvent(reactSurfaceId, screen.id)) | ||
|
||
fun dispatchOnAppear() = | ||
reactEventDispatcher?.dispatchEvent(ScreenAppearEvent(reactSurfaceId, screen.id)) | ||
|
||
fun dispatchOnWillDisappear() = | ||
reactEventDispatcher?.dispatchEvent(ScreenWillDisappearEvent(reactSurfaceId, screen.id)) | ||
|
||
fun dispatchOnDisappear() = | ||
reactEventDispatcher?.dispatchEvent(ScreenDisappearEvent(reactSurfaceId, screen.id)) | ||
|
||
fun dispatchOnDismissed() = | ||
reactEventDispatcher?.dispatchEvent(ScreenDismissedEvent(reactSurfaceId, screen.id)) | ||
|
||
fun dispatchTransitionProgress(progress: Float, isExitAnimation: Boolean, isGoingForward: Boolean) { | ||
val sanitizedProgress = progress.coerceIn(0.0f, 1.0f) | ||
val coalescingKey = ScreenFragment.getCoalescingKey(sanitizedProgress) | ||
reactEventDispatcher?.dispatchEvent(ScreenTransitionProgressEvent(reactSurfaceId, screen.id, sanitizedProgress, isExitAnimation, isGoingForward, coalescingKey)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not entirely sure of
isGoingForward
semantics here. This is something to retrospect later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created ticket on board for this