Skip to content

feat: expose transition progress - #890

Merged
WoLewicki merged 58 commits into
masterfrom
@wolewicki/expose-transition-progress
Aug 24, 2021
Merged

WoLewicki merged 58 commits into
masterfrom
@wolewicki/expose-transition-progress

Conversation

@WoLewicki

@WoLewicki WoLewicki commented Apr 15, 2021 •

Copy link
Copy Markdown
Member

Description

PR adding event with transition progress sent while animating between screens of native-stack. The progress is sent to both transitioning screens.

Changes

  • Added onTransitionProgress event
  • Added logic for calculating, and sending to JS, transition progress for all kinds (hopefully) of transitions for iOS and Android

Test code and steps to reproduce

Test887.tsx

Checklist

@hirbod

hirbod commented Apr 15, 2021

Copy link
Copy Markdown
Contributor

CC @IjzerenHein
Is this something that works for you to implement react-native-shared-element with native-stack?

@mrousavy

mrousavy commented Apr 15, 2021 •

Copy link
Copy Markdown
Contributor

Awesome work @WoLewicki! 🔥

Although I wonder what would be needed in order to pipe the RCTDirectEventBlock (onTransitionProgress event) through reanimated v2 to handle it completely synchronous on the UI thread (in a REA worklet)?

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.

Comment thread ios/RNSScreenStack.m Resolved
Comment thread ios/RNSScreenStack.m Resolved
Comment thread src/native-stack/types.tsx Outdated
/**
* A callback called every frame during the transition of screens of `native-stack`.
*/
onTransitionProgress?: (e: TransitionProgressEvent) => void;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread ios/RNSScreen.m Outdated
}
}

- (void)notifyTransitionProgress:(double)progress closing:(BOOL)closing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ensure that this works with Animated.event withuseNativeDriver: true.


public class ScreenTransitionProgressEvent extends Event<ScreenAppearEvent> {

public static final String EVENT_NAME = "topTransitionProgress";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to call it topTransitionProgress instead of onTransitionProgress?

}

protected void dispatchTransitionProgress(float alpha, boolean closing, boolean shouldIncrementCoalescingKey) {
if (shouldIncrementCoalescingKey) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of incrementing the key?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread createNativeStackNavigator/README.md Resolved
Comment thread ios/RNSScreen.m Outdated
// 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this. Why would this method send events to the view that is not transitioning at the moment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation is the same as in #890 (comment).

Comment thread ios/RNSScreen.m Outdated
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self->_animationTimer setPaused:YES];
[self->_animationTimer invalidate];
self->_fakeView = nil;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need fakeView to be retained under this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could make mAboveScreen null at the end of the transition so it is only alive for the lifetime of it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ done

@kmagiera kmagiera left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks much cleaner after recent changes. Don't have any more comments and I think it is fine to merge it. 👏👏👏👏👏

@WoLewicki
WoLewicki merged commit 8cc51c4 into master Aug 24, 2021
@WoLewicki
WoLewicki deleted the @wolewicki/expose-transition-progress branch August 24, 2021 14:24
WoLewicki added a commit that referenced this pull request Aug 24, 2021
PR fixing deleted by mistake files from #890 and updating Podfile.lock of TestsExample.
WoLewicki added a commit that referenced this pull request Aug 25, 2021
Fix a bug introduced in #890 where when container is already null, the app crashes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider exposing Animated.Value with transition progress (for shared element transitions)

6 participants