Repository navigation
feat: expose transition progress - #890
Conversation
|
CC @IjzerenHein |
|
Awesome work @WoLewicki! 🔥 Although I wonder what would be needed in order to pipe the I think this would be beneficial since pushing a screen could potentially be a timeframe where the JS thread is really busy (rendering the newly pushed screen, effects, etc), so running the animations directly on the UI thread would yield much greater performance. |
| /** | ||
| * A callback called every frame during the transition of screens of `native-stack`. | ||
| */ | ||
| onTransitionProgress?: (e: TransitionProgressEvent) => void; |
There was a problem hiding this comment.
IMO there shouldn't be such an option. We don't expose events in options, we have a separate event system for it. But it also probably shouldn't be a regular event coz then we would need to do animations in JS only. Instead we should expose an Animated.Value like JS stack does. JS stack has a useCardAnimation() hook that we can use in screens in stack. It has many properties in JS stack, but for native stack, this should be enough:
{
/**
* Values for the current screen.
*/
current: {
/**
* Animated node representing the progress value of the current screen.
*/
progress: Animated.AnimatedInterpolation;
};
/**
* Values for the screen after this one in the stack.
* This can be `undefined` in case the screen animating is the last one.
*/
next?: {
/**
* Animated node representing the progress value of the next screen.
*/
progress: Animated.AnimatedInterpolation;
};
};We'd basically need to create animated values for each screen that we can update with Animated.event and useNativeDriver: true.
| } | ||
| } | ||
|
|
||
| - (void)notifyTransitionProgress:(double)progress closing:(BOOL)closing |
There was a problem hiding this comment.
Let's ensure that this works with Animated.event withuseNativeDriver: true.
|
|
||
| public class ScreenTransitionProgressEvent extends Event<ScreenAppearEvent> { | ||
|
|
||
| public static final String EVENT_NAME = "topTransitionProgress"; |
There was a problem hiding this comment.
Is there a reason to call it topTransitionProgress instead of onTransitionProgress?
| } | ||
|
|
||
| protected void dispatchTransitionProgress(float alpha, boolean closing, boolean shouldIncrementCoalescingKey) { | ||
| if (shouldIncrementCoalescingKey) { |
There was a problem hiding this comment.
What's the purpose of incrementing the key?
There was a problem hiding this comment.
I think we want the progress of 0.0 and 1.0 to be always sent to the JS, and such values are dispatched in lifecycle events, so we increment the key in those methods.
There was a problem hiding this comment.
if we want 0 and 1 to be dispatched then coalescing key could be based on the progress value e.g.:
- progress is 0 -> key 1
- progress is 1 -> key 2
- progress is between 0 and 1 -> key 3
| } | ||
| sendTransitionProgressEvent(alpha, closing); | ||
|
|
||
| if (mAboveScreen != null && mAboveScreen.getFragment() != null && !mAboveScreen.getFragment().isSendingProgress()) { |
There was a problem hiding this comment.
Can you add a comment explaining this here. Why would more than one view send progress at a given time? Is there a problem when they do? I understand that progress updates will be delivered to separate views
There was a problem hiding this comment.
We send the transition progress to both of the transitioning screens. We do it by subscribing to the ongoing transition of fragments: https://github.com/software-mansion/react-native-screens/pull/890/files#diff-2c98f4cb51ad28895af60cd79d14b67a66256480a554d81a9dfa6f51c84061c2R41. But if we are pushing a transparentModal, the fragment below does not participate in the transition, so it does not call the onAnimationStart method. In order to achieve the state when always 2 views send progress during the transition, we check if above screen is sending progress, and, if not, we do it from the screen being pushed. Same applies to modals on iOS.
| @Override | ||
| public short getCoalescingKey() { | ||
| // All events for a given view can be coalesced. | ||
| return mCoalescingKey; |
There was a problem hiding this comment.
if you want all events for single view to be coalesced you should just return the same key here. I think that what would be bettere here actually would be to make the key depend on mClosing such that we don't coalesce closing and opening events
There was a problem hiding this comment.
This comment should not be there, it slipped in when I copied the code regarding this. We already increment the key on closing and opening events, as mentioned in the #890 (comment).
| // if we are in a modal, we want to send transition to the above screen too, which might not trigger appear/disappear events | ||
| // e.g. when we are in iOS >= 13 default modal | ||
| // we also check if we are not already sending the progress of transition and if it does not present another modal, | ||
| // because otherwise we would send progress to the screen 2 levels higher then the current one |
There was a problem hiding this comment.
I don't understand this. Why would this method send events to the view that is not transitioning at the moment?
There was a problem hiding this comment.
The explanation is the same as in #890 (comment).
| } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) { | ||
| [self->_animationTimer setPaused:YES]; | ||
| [self->_animationTimer invalidate]; | ||
| self->_fakeView = nil; |
There was a problem hiding this comment.
do we need fakeView to be retained under this?
There was a problem hiding this comment.
We use it in every tick of _animationTimer and I am not sure how else can it be used in the handleAnimation of it (see https://github.com/software-mansion/react-native-screens/pull/890/files#diff-e988f8ab84e7063d6cc9ad4e634806389882ce6727b5c95f3cfbc9f9d3921a67R665)
|
|
||
| protected Screen mScreenView; | ||
| private boolean mIsSendingProgress; | ||
| private Screen mAboveScreen; |
There was a problem hiding this comment.
I'd be hesitant of storing reference to other screens here especially if we want to get reference to a screen that's directly before this one in the container. Note that this order can bemodified and that other screen can be removed (e.g. as a result of JS update) from the container without any notice. In this case we'd keep a reference to an orphaned view here
There was a problem hiding this comment.
We could make mAboveScreen null at the end of the transition so it is only alive for the lifetime of it.
kmagiera
left a comment
There was a problem hiding this comment.
This looks much cleaner after recent changes. Don't have any more comments and I think it is fine to merge it. 👏👏👏👏👏
PR fixing deleted by mistake files from #890 and updating Podfile.lock of TestsExample.
Fix a bug introduced in #890 where when container is already null, the app crashes.
Description
PR adding event with transition progress sent while animating between screens of
native-stack. The progress is sent to both transitioning screens.Changes
onTransitionProgresseventTest code and steps to reproduce
Test887.tsxChecklist