Skip to content

Commit

Permalink
Fixed rare case of infinitive animation of animated image (#3332)
Browse files Browse the repository at this point in the history
* Fixed rare case of infinitive animation of animated image

* Fixed tests
  • Loading branch information
gosha212 authored Oct 30, 2024
1 parent 59b71f6 commit ed8d3cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
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

0 comments on commit ed8d3cf

Please sign in to comment.