|
| 1 | +import React from 'react'; |
| 2 | +import { View, Image, TouchableOpacity, Animated, Easing } from 'react-native'; |
| 3 | +import Pulse from './Pulse'; |
| 4 | + |
| 5 | + |
| 6 | +export default class LocationPulseLoader extends React.Component { |
| 7 | + constructor(props) { |
| 8 | + super(props); |
| 9 | + |
| 10 | + this.state = { |
| 11 | + circles: [] |
| 12 | + }; |
| 13 | + |
| 14 | + this.counter = 1; |
| 15 | + this.setInterval = null; |
| 16 | + this.anim = new Animated.Value(1); |
| 17 | + } |
| 18 | + |
| 19 | + componentDidMount() { |
| 20 | + this.setCircleInterval(); |
| 21 | + } |
| 22 | + |
| 23 | + setCircleInterval() { |
| 24 | + this.setInterval = setInterval(this.addCircle.bind(this), this.props.interval); |
| 25 | + this.addCircle(); |
| 26 | + } |
| 27 | + |
| 28 | + addCircle() { |
| 29 | + this.setState({ circles: [...this.state.circles, this.counter] }); |
| 30 | + this.counter++; |
| 31 | + } |
| 32 | + |
| 33 | + onPressIn() { |
| 34 | + Animated.timing(this.anim, { |
| 35 | + toValue: this.props.pressInValue, |
| 36 | + duration: this.props.pressDuration, |
| 37 | + easing: this.props.pressInEasing, |
| 38 | + }).start(() => clearInterval(this.setInterval)); |
| 39 | + } |
| 40 | + |
| 41 | + onPressOut() { |
| 42 | + Animated.timing(this.anim, { |
| 43 | + toValue: 1, |
| 44 | + duration: this.props.pressDuration, |
| 45 | + easing: this.props.pressOutEasing, |
| 46 | + }).start(this.setCircleInterval.bind(this)); |
| 47 | + } |
| 48 | + |
| 49 | + render() { |
| 50 | + const { size, avatar, avatarBackgroundColor, interval } = this.props; |
| 51 | + |
| 52 | + return ( |
| 53 | + <View style={{ |
| 54 | + flex: 1, |
| 55 | + backgroundColor: 'transparent', |
| 56 | + justifyContent: 'center', |
| 57 | + alignItems: 'center', |
| 58 | + }}> |
| 59 | + {this.state.circles.map((circle) => ( |
| 60 | + <Pulse |
| 61 | + key={circle} |
| 62 | + {...this.props} |
| 63 | + /> |
| 64 | + ))} |
| 65 | + |
| 66 | + <TouchableOpacity |
| 67 | + activeOpacity={1} |
| 68 | + onPressIn={this.onPressIn.bind(this)} |
| 69 | + onPressOut={this.onPressOut.bind(this)} |
| 70 | + style={{ |
| 71 | + transform: [{ |
| 72 | + scale: this.anim |
| 73 | + }] |
| 74 | + }} |
| 75 | + > |
| 76 | + <Image |
| 77 | + source={{ uri: avatar }} |
| 78 | + style={{ |
| 79 | + width: size, |
| 80 | + height: size, |
| 81 | + borderRadius: size/2, |
| 82 | + backgroundColor: avatarBackgroundColor |
| 83 | + }} |
| 84 | + /> |
| 85 | + </TouchableOpacity> |
| 86 | + </View> |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +LocationPulseLoader.propTypes = { |
| 92 | + interval: React.PropTypes.number, |
| 93 | + size: React.PropTypes.number, |
| 94 | + pulseMaxSize: React.PropTypes.number, |
| 95 | + avatar: React.PropTypes.string.isRequired, |
| 96 | + avatarBackgroundColor: React.PropTypes.string, |
| 97 | + pressInValue: React.PropTypes.number, |
| 98 | + pressDuration: React.PropTypes.number, |
| 99 | + borderColor: React.PropTypes.string, |
| 100 | + backgroundColor: React.PropTypes.string, |
| 101 | + getStyle: React.PropTypes.func, |
| 102 | +}; |
| 103 | + |
| 104 | +LocationPulseLoader.defaultProps = { |
| 105 | + interval: 2000, |
| 106 | + size: 100, |
| 107 | + pulseMaxSize: 250, |
| 108 | + avatar: undefined, |
| 109 | + avatarBackgroundColor: 'white', |
| 110 | + pressInValue: 0.8, |
| 111 | + pressDuration: 150, |
| 112 | + pressInEasing: Easing.in, |
| 113 | + pressOutEasing: Easing.in, |
| 114 | + borderColor: '#D8335B', |
| 115 | + backgroundColor: '#ED225B55', |
| 116 | + getStyle: undefined, |
| 117 | +}; |
| 118 | + |
0 commit comments