Skip to content
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

Prevent Lottie from running in the background after navigating to other pages #48444

Merged
merged 8 commits into from
Sep 10, 2024
40 changes: 36 additions & 4 deletions src/components/Lottie/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {NavigationContainerRefContext, NavigationContext} from '@react-navigation/native';
import type {AnimationObject, LottieViewProps} from 'lottie-react-native';
import LottieView from 'lottie-react-native';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useEffect, useState} from 'react';
import React, {forwardRef, useContext, useEffect, useState} from 'react';
import {View} from 'react-native';
import type DotLottieAnimation from '@components/LottieAnimations/types';
import useAppState from '@hooks/useAppState';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import CONST from '@src/CONST';
import NAVIGATORS from '@src/NAVIGATORS';
import {useSplashScreenStateContext} from '@src/SplashScreenStateContext';

type Props = {
Expand All @@ -30,11 +33,40 @@ function Lottie({source, webStyle, ...props}: Props, ref: ForwardedRef<LottieVie

const aspectRatioStyle = styles.aspectRatioLottie(source);

// If the image fails to load, app is in background state, animation file isn't ready, or the splash screen isn't hidden yet,
const browser = Browser.getBrowser();
const [hasNavigatedAway, setHasNavigatedAway] = React.useState(false);
const navigationContainerRef = useContext(NavigationContainerRefContext);
const navigator = useContext(NavigationContext);

useEffect(() => {
if (!browser || !navigationContainerRef || !navigator) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to check browser?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the issue only occurs in browsers, it seems unnecessary to apply this fix to native platforms.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, let change the PR status

return;
}
const unsubscribeNavigationFocus = navigator.addListener('focus', () => {
setHasNavigatedAway(false);
});
return unsubscribeNavigationFocus;
}, [browser, navigationContainerRef, navigator]);

useEffect(() => {
if (!browser || !navigationContainerRef || !navigator) {
return;
}
const unsubscribeNavigationBlur = navigator.addListener('blur', () => {
const state = navigationContainerRef.getRootState();
const targetRouteName = state?.routes?.[state?.index ?? 0]?.name;
if (targetRouteName !== NAVIGATORS.RIGHT_MODAL_NAVIGATOR) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (targetRouteName !== NAVIGATORS.RIGHT_MODAL_NAVIGATOR) {
if (![NAVIGATORS.RIGHT_MODAL_NAVIGATOR, NAVIGATORS.LEFT_MODAL_NAVIGATOR].includes(targetRouteName)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That will be better. Thanks for pointing it out!

setHasNavigatedAway(true);
}
});
return unsubscribeNavigationBlur;
}, [browser, navigationContainerRef, navigator]);

// If the page navigates to another screen, the image fails to load, app is in background state, animation file isn't ready, or the splash screen isn't hidden yet,
// we'll just render an empty view as the fallback to prevent
// 1. memory leak, see issue: https://github.com/Expensify/App/issues/36645
// 2. heavy rendering, see issue: https://github.com/Expensify/App/issues/34696
if (isError || appState.isBackground || !animationFile || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN) {
// 2. heavy rendering, see issues: https://github.com/Expensify/App/issues/34696 and https://github.com/Expensify/App/issues/47273
if (hasNavigatedAway || isError || appState.isBackground || !animationFile || splashScreenState !== CONST.BOOT_SPLASH_STATE.HIDDEN) {
return <View style={[aspectRatioStyle, props.style]} />;
}

Expand Down
Loading