Skip to content

Fixed rare case of infinitive animation of animated image #3332

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 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,7 @@ exports[`AvatarScreen renders screen 1`] = `
assetGroup="icons"
onError={[Function]}
onLoad={[Function]}
onLoadStart={[Function]}
source={
{
"uri": "https://static.pexels.com/photos/60628/flower-garden-blue-sky-hokkaido-japan-60628.jpeg",
Expand Down
31 changes: 20 additions & 11 deletions src/components/animatedImage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ const AnimatedImage = (props: AnimatedImageProps) => {
loader,
style,
onLoad: propsOnLoad,
onLoadStart: propsOnLoadStart,
animationDuration = 300,
testID,
...others
} = props;

const [isLoading, setIsLoading] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const opacity = useSharedValue(0);

useDidUpdate(() => {
Expand All @@ -50,16 +51,23 @@ const AnimatedImage = (props: AnimatedImageProps) => {
}
}, [loader]);

const onLoad = useCallback((event: NativeSyntheticEvent<ImageLoadEventData>) => {
setIsLoading(false);
propsOnLoad?.(event);
// did not start the animation already
if (opacity.value === 0) {
opacity.value = withTiming(1, {duration: animationDuration});
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[setIsLoading, propsOnLoad, animationDuration]);
const onLoad = useCallback(
(event: NativeSyntheticEvent<ImageLoadEventData>) => {
setIsLoading(false);
propsOnLoad?.(event);
// did not start the animation already
if (opacity.value === 0) {
opacity.value = withTiming(1, {duration: animationDuration});
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[setIsLoading, propsOnLoad, animationDuration]
);

const onLoadStart = useCallback(() => {
setIsLoading(true);
propsOnLoadStart?.();
}, [setIsLoading, propsOnLoadStart, animationDuration]);

const fadeInStyle = useAnimatedStyle(() => {
return {opacity: opacity.value};
Expand All @@ -75,6 +83,7 @@ const AnimatedImage = (props: AnimatedImageProps) => {
style={_style}
source={source}
onLoad={onLoad}
onLoadStart={onLoadStart}
testID={testID}
imageStyle={undefined}
/>
Expand Down