-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskCard.tsx
More file actions
178 lines (165 loc) · 4.63 KB
/
TaskCard.tsx
File metadata and controls
178 lines (165 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import FontAwesome5 from '@expo/vector-icons/FontAwesome5';
import { memo } from 'react';
import { Dimensions, StyleSheet, Text, View } from 'react-native';
import type { SharedValue } from 'react-native-reanimated';
import Animated, {
interpolateColor,
LinearTransition,
useAnimatedReaction,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withTiming
} from 'react-native-reanimated';
import Sortable, { useItemContext } from 'react-native-sortables';
import AnimatedText from '@/components/AnimatedText';
import { colors, flex, iconSizes, radius, spacing, text } from '@/theme';
import { SEPARATOR } from './constants';
import type { Task } from './types';
import { minutesToTime } from './utils';
function formatDuration(duration: number) {
'worklet';
return `Duration: ${minutesToTime(duration)}`;
}
type TaskCardProps = Task & {
startTimeMinutes: number;
totalDurations: SharedValue<Record<string, number>>;
};
function TaskCard({
duration,
icon,
startTimeMinutes,
title,
totalDurations
}: TaskCardProps) {
const { itemKey, keyToIndex } = useItemContext();
const selectedAnimationProgress = useDerivedValue(() => {
const itemIndex = keyToIndex.value[itemKey] ?? 0;
const separatorIndex = keyToIndex.value[SEPARATOR] ?? 0;
return withTiming(itemIndex < separatorIndex ? 1 : 0);
});
const isSelected = useDerivedValue(() =>
Math.round(selectedAnimationProgress.value)
);
const animatedText = useSharedValue(formatDuration(duration));
useAnimatedReaction(
() => ({
progress: selectedAnimationProgress.value,
totalDuration: totalDurations.value[itemKey]
}),
({ progress, totalDuration }) => {
if (progress === 1 && totalDuration) {
const startTime = startTimeMinutes + totalDuration - duration;
animatedText.value = minutesToTime(startTime);
} else if (progress === 0) {
animatedText.value = formatDuration(duration);
}
},
[totalDurations, itemKey, duration, startTimeMinutes]
);
const animatedCardStyle = useAnimatedStyle(() => ({
backgroundColor: interpolateColor(
selectedAnimationProgress.value,
[0, 1],
[colors.white, colors.primary]
),
...(isSelected.value
? {
justifyContent: 'flex-start',
marginLeft: 60,
minHeight: 1.5 * duration
}
: {
justifyContent: 'center',
marginLeft: 0,
minHeight: 'auto'
})
}));
const animatedTitleStyle = useAnimatedStyle(() => ({
color: interpolateColor(
selectedAnimationProgress.value,
[0, 1],
[colors.foreground2, colors.white]
)
}));
const animatedSubtitleStyle = useAnimatedStyle(() =>
isSelected.value
? {
left: -110,
position: 'absolute',
top: -10
}
: {
left: 0,
position: 'relative',
top: 0
}
);
return (
<Animated.View
layout={LinearTransition}
style={[styles.card, animatedCardStyle]}>
<View style={styles.contentContainer}>
<View style={styles.content}>
<Text>{icon}</Text>
<View style={flex.shrink}>
<Animated.Text
layout={LinearTransition}
numberOfLines={1}
style={[styles.title, animatedTitleStyle]}>
{title}
</Animated.Text>
<AnimatedText
layout={LinearTransition}
style={[styles.subtitle, animatedSubtitleStyle]}
text={animatedText}
/>
</View>
</View>
<Animated.View layout={LinearTransition}>
<Sortable.Handle>
<FontAwesome5
color='#d1d1d1'
name='grip-horizontal'
size={iconSizes.md}
/>
</Sortable.Handle>
</Animated.View>
</View>
</Animated.View>
);
}
export default memo(TaskCard);
const styles = StyleSheet.create({
card: {
backgroundColor: colors.background2,
borderRadius: radius.md,
paddingHorizontal: spacing.lg,
paddingVertical: spacing.md
},
content: {
alignItems: 'center',
flexDirection: 'row',
flexShrink: 1,
gap: spacing.md
},
contentContainer: {
alignItems: 'center',
flexDirection: 'row',
gap: spacing.md,
justifyContent: 'space-between'
},
subtitle: {
...text.subHeading3,
color: colors.foreground3,
// Set to something large to prevent text clipping when text in the
// animated text input changes
width: Dimensions.get('window').width
},
title: {
...text.heading4,
color: colors.foreground2,
flexShrink: 1,
textOverflow: 'ellipsis'
}
});