-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathApp.js
200 lines (189 loc) · 5.27 KB
/
App.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Inspiration: https://dribbble.com/shots/8257559-Movie-2-0
*
*/
import * as React from 'react';
import {
StatusBar,
Text,
View,
StyleSheet,
FlatList,
Image,
Dimensions,
Animated,
TouchableOpacity,
Platform,
} from 'react-native';
const { width, height } = Dimensions.get('window');
import { getMovies } from './api';
import Genres from './Genres';
import Rating from './Rating';
import { LinearGradient } from 'expo-linear-gradient';
const SPACING = 10;
const ITEM_SIZE = Platform.OS === 'ios' ? width * 0.72 : width * 0.74;
const EMPTY_ITEM_SIZE = (width - ITEM_SIZE) / 2;
const BACKDROP_HEIGHT = height * 0.65;
const Loading = () => (
<View style={styles.loadingContainer}>
<Text style={styles.paragraph}>Loading...</Text>
</View>
);
const Backdrop = ({ movies, scrollX }) => {
return (
<View style={{ height: BACKDROP_HEIGHT, width, position: 'absolute' }}>
<FlatList
data={movies.reverse()}
keyExtractor={(item) => item.key + '-backdrop'}
removeClippedSubviews={false}
contentContainerStyle={{ width, height: BACKDROP_HEIGHT }}
renderItem={({ item, index }) => {
if (!item.backdrop) {
return null;
}
const translateX = scrollX.interpolate({
inputRange: [(index - 2) * ITEM_SIZE, (index - 1) * ITEM_SIZE],
outputRange: [0, width],
// extrapolate:'clamp'
});
return (
<Animated.View
removeClippedSubviews={false}
style={{
position: 'absolute',
width: translateX,
height,
overflow: 'hidden',
}}
>
<Image
source={{ uri: item.backdrop }}
style={{
width,
height: BACKDROP_HEIGHT,
position: 'absolute',
}}
/>
</Animated.View>
);
}}
/>
<LinearGradient
colors={['rgba(0, 0, 0, 0)', 'white']}
style={{
height: BACKDROP_HEIGHT,
width,
position: 'absolute',
bottom: 0,
}}
/>
</View>
);
};
export default function App() {
const [movies, setMovies] = React.useState([]);
const scrollX = React.useRef(new Animated.Value(0)).current;
React.useEffect(() => {
const fetchData = async () => {
const movies = await getMovies();
// Add empty items to create fake space
// [empty_item, ...movies, empty_item]
setMovies([{ key: 'empty-left' }, ...movies, { key: 'empty-right' }]);
};
if (movies.length === 0) {
fetchData(movies);
}
}, [movies]);
if (movies.length === 0) {
return <Loading />;
}
return (
<View style={styles.container}>
<Backdrop movies={movies} scrollX={scrollX} />
<StatusBar hidden />
<Animated.FlatList
showsHorizontalScrollIndicator={false}
data={movies}
keyExtractor={(item) => item.key}
horizontal
bounces={false}
decelerationRate={Platform.OS === 'ios' ? 0 : 0.98}
renderToHardwareTextureAndroid
contentContainerStyle={{ alignItems: 'center' }}
snapToInterval={ITEM_SIZE}
snapToAlignment='start'
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { x: scrollX } } }],
{ useNativeDriver: false }
)}
scrollEventThrottle={16}
renderItem={({ item, index }) => {
if (!item.poster) {
return <View style={{ width: EMPTY_ITEM_SIZE }} />;
}
const inputRange = [
(index - 2) * ITEM_SIZE,
(index - 1) * ITEM_SIZE,
index * ITEM_SIZE,
];
const translateY = scrollX.interpolate({
inputRange,
outputRange: [100, 50, 100],
extrapolate: 'clamp',
});
return (
<View style={{ width: ITEM_SIZE }}>
<Animated.View
style={{
marginHorizontal: SPACING,
padding: SPACING * 2,
alignItems: 'center',
transform: [{ translateY }],
backgroundColor: 'white',
borderRadius: 34,
}}
>
<Image
source={{ uri: item.poster }}
style={styles.posterImage}
/>
<Text style={{ fontSize: 24 }} numberOfLines={1}>
{item.title}
</Text>
<Rating rating={item.rating} />
<Genres genres={item.genres} />
<Text style={{ fontSize: 12 }} numberOfLines={3}>
{item.description}
</Text>
</Animated.View>
</View>
);
}}
/>
</View>
);
}
const styles = StyleSheet.create({
loadingContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
container: {
flex: 1,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
posterImage: {
width: '100%',
height: ITEM_SIZE * 1.2,
resizeMode: 'cover',
borderRadius: 24,
margin: 0,
marginBottom: 10,
},
});