This repository was archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.js
155 lines (140 loc) · 4.2 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
import React from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity,
Animated,
Dimensions,
} from 'react-native';
import Image from 'react-native-remote-svg';
import checkIcon from './assets/checked.svg';
import cancelIcon from './assets/cancel.svg';
import Card from "./Card";
import img1 from './assets/image1.jpeg';
import img2 from './assets/image2.jpeg';
import img3 from './assets/image3.jpeg';
import img4 from './assets/image4.jpeg';
import img5 from './assets/image5.jpeg';
import EmptyState from './EmptyState';
const SCREEN_HEIGHT = Dimensions.get('window').height;
const SCREEN_WIDTH = Dimensions.get('window').width;
const getCards = () => {
const cards = [
{ id: '1', image: img1, isActive: true },
{ id: '2', image: img2, isActive: false },
{ id: '3', image: img3, isActive: false },
{ id: '4', image: img4, isActive: false },
{ id: '5', image: img5, isActive: false },
];
let lastItemPosition = false;
cards.forEach((card, i) => {
const position = new Animated.ValueXY();
card.position = position;
card.parentPosition = lastItemPosition;
lastItemPosition = position;
});
return cards;
}
export default class App extends React.Component {
constructor() {
super();
const cards = getCards();
this.state = {cards};
}
onCardSwiped = (id) => {
this.setState(prevState => {
const swipedIndex = prevState.cards.findIndex(card => card.id === id);
const isLastIndex = swipedIndex === (prevState.cards.length - 1);
const nextIndex = swipedIndex + 1;
const newState = {...prevState};
newState.cards[swipedIndex]['isActive'] = false;
if (isLastIndex) return prevState;
newState.cards[nextIndex]['isActive'] = true;
return newState;
});
}
handleNopeSelect = (dy=0, position=false) => {
const activeIndex = this.state.cards.findIndex(card => card.isActive);
if (activeIndex < 0) return;
if (!position) {
position = this.state.cards[activeIndex].position;
}
Animated.spring(position, {
toValue: { x: SCREEN_WIDTH + 100, y: dy }
}).start(this.onCardSwiped(this.state.cards[activeIndex].id));
}
handleLikeSelect = (dy=0, position=false) => {
const activeIndex = this.state.cards.findIndex(card => card.isActive);
if (activeIndex < 0) return;
if (!position) {
position = this.state.cards[activeIndex].position;
}
Animated.spring(position, {
toValue: { x: -SCREEN_WIDTH - 100, y: dy }
}).start(this.onCardSwiped(this.state.cards[activeIndex].id));
}
renderCards = (cards) => {
if (this.isEmptyState()) return <EmptyState reloadCards={this.reloadCards} />
return cards.map((card, index) => {
return <Card key={card.id} {...card} handleNopeSelect={this.handleNopeSelect} handleLikeSelect={this.handleLikeSelect} />;
}).reverse();
}
reloadCards = () => {
const cards = getCards();
this.setState({cards});
}
isEmptyState = () => {
return this.state.cards.findIndex(card => card.isActive) < 0;
}
render() {
return (
<View style={styles.container} >
<View style={styles.cardArea} >
{this.renderCards(this.state.cards)}
</View>
<View style={styles.btnContainer}>
<TouchableOpacity style={styles.btn} onPress={() => this.handleLikeSelect()} >
<Image source={checkIcon} style={styles.btnIcon} />
</TouchableOpacity>
<TouchableOpacity style={styles.btn} onPress={() => this.handleNopeSelect()} >
<Image source={cancelIcon} style={styles.btnIcon} />
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
justifyContent: 'center',
backgroundColor: '#fff',
alignItems: 'stretch',
},
cardArea: {
flex: 10,
marginTop: 30
},
btnContainer: {
flex: 2,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
zIndex: -1,
},
btn: {
height: 70,
width: 70,
borderRadius: 35,
alignItems: 'center',
justifyContent: 'center',
marginHorizontal: 15,
backgroundColor: '#efefef',
},
btnIcon: {
height: 25,
width: 25,
},
});