Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. View,
  4. Animated,
  5. PanResponder,
  6. Dimensions,
  7. LayoutAnimation,
  8. UIManager
  9. } from 'react-native';
  10. import { Button } from 'react-native-elements'
  11. import Icon from 'react-native-vector-icons/FontAwesome';
  12.  
  13. //make the rotate to be work as the screen size in wach phone
  14. const SCREEN_WIDTH = Dimensions.get('window').width;
  15. //the limit of the card to move left/right and not return to the start position
  16. const SWIPE_THRESHOLD = 0.25 * SCREEN_WIDTH;
  17. //the value of ms that the card will move
  18. const SWIPE_OUT_DURATION = 250;
  19.  
  20. class Deck extends Component {
  21. //default function to make the app works whe card swip to a side when we make a real function with the same name the default will not work
  22. static defaultProps = {
  23. onSwipeRight: () => { },
  24. onSwipeLeft: () => { }
  25. }
  26. //constructor
  27. constructor(props) {
  28. super(props);
  29.  
  30. //default value to the position
  31. const position = new Animated.ValueXY();
  32.  
  33. const panResponder = PanResponder.create({
  34. //function that activate every time finger will be on the card
  35. onStartShouldSetPanResponder: () => true,
  36. //function that activate in every movemet with the finger and the card
  37. //envent - the card we put the finger
  38. //gesture - the details where the finger move like speed and position
  39. onPanResponderMove: (event, gesture) => {
  40. //update the position
  41. position.setValue({ x: gesture.dx, y: gesture.dy });
  42. },
  43. //function that activate when the finger is release from the screen and see where the card is
  44. onPanResponderRelease: (event, gesture) => {
  45. if (gesture.dx > SWIPE_THRESHOLD) {
  46. this.forceSwipe('right');
  47. } else if (gesture.dx < -SWIPE_THRESHOLD) {
  48. this.forceSwipe('left');
  49. } else {
  50. this.resetPosition();
  51. }
  52. }
  53. });
  54.  
  55. this.state = { panResponder, position, index: 0 };
  56. }
  57.  
  58. componentWillReceiveProps(nextProps) {
  59. if (nextProps.data !== this.props.data) {
  60. this.setState({ index: 0 });
  61. }
  62. }
  63.  
  64. componentWillUpdate() {
  65. UIManager.setLayoutAnimationEnabledExperimental && UIManager.setLayoutAnimationEnabledExperimental(true);
  66. LayoutAnimation.spring();
  67. }
  68.  
  69. //help function move the card right/left from the screen
  70. forceSwipe(direction) {
  71. const x = direction === 'right' ? SCREEN_WIDTH : -SCREEN_WIDTH;
  72. Animated.timing(this.state.position, { //timing move the card fast
  73. toValue: { x, y: 0 },//x will change to the x from the "if" SCREEN_WIDTH or -SCREEN_WIDTH
  74. duration: SWIPE_OUT_DURATION
  75. }).start(() => this.onSwipeComplete(direction));
  76. }
  77.  
  78. //help function make somethings when the card was swip left or right from the screen
  79. onSwipeComplete(direction) {
  80. const { onSwipeLeft, onSwipeRight, data } = this.props;
  81. const item = data[this.state.index];
  82.  
  83. direction === 'right' ? onSwipeRight(item) : onSwipeLeft(item);
  84. this.state.position.setValue({ x: 0, y: 0 }); //return the position to be (0, 0)
  85. this.setState({ index: this.state.index + 1 }); //move +1 in the index
  86. }
  87.  
  88. //help function that move the card to the start position
  89. resetPosition() {
  90. Animated.spring(this.state.position, {//spring the card will move nice
  91. toValue: { x: 0, y: 0 }
  92. }).start();
  93. }
  94.  
  95. //help function makeing all the style
  96. getCardStyle() {
  97. const { position } = this.state;
  98. //how the rotate will move
  99. const rotate = position.x.interpolate({
  100. inputRange: [-SCREEN_WIDTH * 1.5, 0, SCREEN_WIDTH * 1.5],//* 1.5 because the rotate move too fast
  101. outputRange: ['-120deg', '0deg', '120deg']
  102. });
  103.  
  104. return {
  105. ...position.getLayout(),
  106. //makes the rotate to the card
  107. transform: [{ rotate }]
  108. };
  109. }
  110.  
  111. //built and getting deck list
  112. renderCards() {
  113. //if the list of card empty now
  114. if (this.state.index >= this.props.data.length) {
  115. return this.props.renderNoMoreCards();
  116. }
  117.  
  118. return this.props.data.map((item, i) => {
  119. if (i < this.state.index) { return null; }//for the card we already moved
  120.  
  121. if (i === this.state.index) {
  122. return (
  123. <Animated.View
  124. key={item.id}
  125. style={[this.getCardStyle(), styles.cardStyle, { zIndex: 99 }]}
  126. {...this.state.panResponder.panHandlers} //the '...' is for separate the cards from each other
  127. >
  128. {this.props.renderCard(item)}
  129. <Button
  130. onPress={()=> this.forceSwipe('right')}
  131. large
  132. icon={{name: 'attach-money'}}
  133. title='Want it' />
  134. <Button
  135. onPress={()=> this.forceSwipe('left')}
  136. large
  137. icon={{name: 'money-off'}}
  138. title='Pass it' />
  139. </Animated.View>
  140. );
  141. }
  142.  
  143. return (
  144. <Animated.View
  145. key= {item.id}
  146. style={[styles.cardStyle, { top: 10 * (i - this.state.index), zIndex: 5 }]}
  147. >
  148. {this.props.renderCard(item)}
  149.  
  150.  
  151. </Animated.View>
  152. );
  153. }).reverse();
  154. }
  155.  
  156. render() {
  157. return (
  158. <View>
  159. {this.renderCards()}
  160.  
  161. </View>
  162. );
  163. }
  164.  
  165. }
  166. const styles = {
  167. cardStyle: {
  168. position: 'absolute',
  169. width: SCREEN_WIDTH
  170. }
  171. }
  172.  
  173. export default Deck;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement