Skip to content

Commit a374f65

Browse files
committed
refactor(chat): animate AttachmentThumbnail progress with Reanimated
1 parent ea6f980 commit a374f65

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

components/chat-screen/AttachmentThumbnail.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import React, { useEffect, useMemo, useRef } from 'react';
1+
import React, { useEffect, useMemo } from 'react';
22
import {
33
View,
44
Image,
55
Text,
66
TouchableOpacity,
77
ActivityIndicator,
8-
Animated,
9-
Easing,
108
StyleSheet,
119
} from 'react-native';
10+
import Animated, {
11+
Easing,
12+
useAnimatedStyle,
13+
useSharedValue,
14+
withTiming,
15+
} from 'react-native-reanimated';
1216
import { useTheme } from '../../context/ThemeContext';
1317
import { Theme } from '../../styles/colors';
1418
import { fontFamily, fontSizes } from '../../styles/fontStyles';
@@ -26,17 +30,19 @@ const AttachmentThumbnail = ({ attachment, onRemove }: Props) => {
2630
const { theme } = useTheme();
2731
const styles = useMemo(() => createStyles(theme), [theme]);
2832

29-
const fill = useRef(new Animated.Value(0)).current;
33+
const fill = useSharedValue(0);
3034
useEffect(() => {
3135
if (attachment.progress == null) return;
32-
Animated.timing(fill, {
33-
toValue: attachment.progress * ATTACHMENT_PROGRESS_TRACK_WIDTH,
34-
duration: 250,
35-
easing: Easing.out(Easing.cubic),
36-
useNativeDriver: false,
37-
}).start();
36+
fill.set(
37+
withTiming(attachment.progress * ATTACHMENT_PROGRESS_TRACK_WIDTH, {
38+
duration: 250,
39+
easing: Easing.out(Easing.cubic),
40+
})
41+
);
3842
}, [attachment.progress, fill]);
3943

44+
const fillStyle = useAnimatedStyle(() => ({ width: fill.get() }));
45+
4046
const renderContent = () => {
4147
if (attachment.status === 'loading') {
4248
if (attachment.progress == null) {
@@ -58,7 +64,7 @@ const AttachmentThumbnail = ({ attachment, onRemove }: Props) => {
5864
{Math.round(attachment.progress * 100)}%
5965
</Text>
6066
<View style={styles.progressTrack}>
61-
<Animated.View style={[styles.progressFill, { width: fill }]} />
67+
<Animated.View style={[styles.progressFill, fillStyle]} />
6268
</View>
6369
</View>
6470
);

0 commit comments

Comments
 (0)