Guest User

Untitled

a guest
Feb 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. import React, { Component } from "react";
  2. import {
  3. View,
  4. Animated,
  5. Image,
  6. Text,
  7. LayoutAnimation,
  8. TouchableOpacity
  9. } from "react-native";
  10. import MapboxGL from "@mapbox/react-native-mapbox-gl";
  11. import Interactable from "react-native-interactable";
  12. import { linear } from "everpolate";
  13. import { RenderIf, NOOP } from "mt-utils";
  14. import { DelayRenderer } from "mt-components";
  15. import { WIDTH_PHONE, HEIGHT_PHONE, smallHitSlop } from "mt-constants/style";
  16.  
  17. import boite from "mt-components/svg/map/pins/Pinboiteauxlettres";
  18. import poste from "mt-components/svg/map/pins/Pinposte";
  19. import pickup from "mt-components/svg/map/pins/Pinpickup";
  20. import Cross from "mt-components/svg/map/Cross";
  21. import SliderItem from "./slider-item";
  22. import Pager from "./pager";
  23. import Content from "./content";
  24. import Ovni from "./ovni";
  25. import style from "./style";
  26.  
  27. const ACCESS_TOKEN =
  28. "pk.eyJ1IjoibWFsaW52ZXJuaSIsImEiOiJjamJkejU0NmIwMHg2MzNrOTBzM3c3NnZjIn0.8sFiXIrekHwjEdK5ZGHHhQ";
  29.  
  30. const PINS = {
  31. boite,
  32. pickup,
  33. poste
  34. };
  35.  
  36. export default class Map extends Component {
  37. constructor(props) {
  38. super(props);
  39. this.mapIsMounted = false;
  40. this.mapOpacity = new Animated.Value(0);
  41. this.translateContent = new Animated.Value(400);
  42. this.translateMap = new Animated.Value(0);
  43. this.overlayOpacity = new Animated.Value(0);
  44. this.transform = { transform: [{ translateY: this.translateContent }] };
  45. this.transformMap = { transform: [{ translateY: this.translateMap }] };
  46. this.deltaY = new Animated.Value(0);
  47. this.offset = new Animated.Value(0);
  48. this.state = {
  49. index: 0,
  50. isReady: false
  51. };
  52. }
  53.  
  54. componentWillMount = () => {
  55. return MapboxGL.setAccessToken(ACCESS_TOKEN);
  56. };
  57.  
  58. componentDidMount = () => {
  59. const { disableDismissable } = this.props;
  60. return disableDismissable();
  61. };
  62.  
  63. componentWillReceiveProps = next => {
  64. if (!this.props.isOpen && next.isOpen) {
  65. return this.openingAnimation();
  66. }
  67. };
  68.  
  69. getIndicatorContent = () => {
  70. const { points } = this.props;
  71. const { moving, searching, index } = this.state;
  72. if (!moving && !searching)
  73. return `${Math.round(points[index] && points[index].distance)} m`;
  74. if (searching) return "Recherche en cours";
  75. if (moving) return "Rechercher ici";
  76. return "";
  77. };
  78.  
  79. onMapLayout = () => {
  80. const { points } = this.props;
  81. if (this.mapIsMounted) return;
  82. this.mapIsMounted = true;
  83. if (points[0]) {
  84. const coordinates = this.getLatLong(points[0]);
  85. //Animated.delay(800).start(() => this.map.moveTo(coordinates));
  86. }
  87. };
  88.  
  89. onSliderRelease = () => {
  90. const {points} = this.props;
  91. const {index} = this.state;
  92. const coordinate = this.getLatLong(points[index + 1]);
  93. return this.map.flyTo(coordinate, 800);
  94. }
  95.  
  96. onScrollEnd = index => {
  97. LayoutAnimation.easeInEaseOut();
  98. return this.setState({ index });
  99. };
  100.  
  101. getLatLong = data => {
  102. const { coordinates } = data.location;
  103. const lng = coordinates[1];
  104. const lat = coordinates[0];
  105. return [lng, lat];
  106. };
  107.  
  108. onWillChange = () => {
  109.  
  110. /*
  111.  
  112. return this.setState({ moving: true, searching: false }, () => {
  113. return Animated.spring(this.overlayOpacity, {
  114. toValue: 1,
  115. useNativeDriver: true
  116. }).start();
  117. }); */
  118.  
  119. };
  120.  
  121. onDidChange = () => {
  122.  
  123. /*
  124.  
  125. return this.setState({ moving: false, searching: true }, () => {
  126. return Animated.spring(this.overlayOpacity, {
  127. toValue: 0,
  128. useNativeDriver: true
  129. }).start();
  130. }); */
  131. };
  132.  
  133. openingAnimation = () => {
  134. Animated.parallel([
  135. Animated.spring(this.mapOpacity, {
  136. toValue: 1,
  137. useNativeDriver: true
  138. }),
  139. Animated.spring(this.translateContent, {
  140. friction: 6,
  141. toValue: 0,
  142. delay: 800,
  143. useNativeDriver: true
  144. }),
  145. Animated.spring(this.translateMap, {
  146. toValue: -150,
  147. delay: 800,
  148. useNativeDriver: true
  149. })
  150. ]).start(() => this.setState({isReady: true}));
  151. };
  152.  
  153.  
  154. closeView = () => {
  155. return this.props.closeModal();
  156. }
  157.  
  158. close = () => {
  159. return this.interactable.snapTo({ index: 0 });
  160. };
  161.  
  162. open = () => {
  163. return this.interactable.snapTo({ index: 1 });
  164. }
  165.  
  166. onPressAnnotation = (coordinate, index) => {
  167. this.scrollTo(index);
  168. return this.map.flyTo(coordinate, 800);
  169. }
  170.  
  171. annotation = (data, index) => {
  172. const Pin = PINS.boite;
  173. const coordinate = this.getLatLong(data);
  174.  
  175. return (
  176. <MapboxGL.PointAnnotation
  177. key={index.toString()}
  178. id={index.toString()}
  179. onSelected={() => this.onPressAnnotation(coordinate, index)}
  180. coordinate={coordinate}
  181. >
  182. <Pin />
  183. </MapboxGL.PointAnnotation>
  184. );
  185. };
  186.  
  187. render() {
  188. const { points, isOpen } = this.props;
  189. const { index, isReady, moving, searching } = this.state;
  190. const renderedPoints = points;
  191.  
  192. return (
  193. <View style={style.container}>
  194. <RenderIf condition={isOpen}>
  195. <Animated.View
  196. onLayout={this.onMapLayout}
  197. style={[{opacity: this.mapOpacity}, this.transformMap]}
  198. >
  199. <MapboxGL.MapView
  200. onRegionWillChange={this.onWillChange}
  201. onRegionDidChange={this.onDidChange}
  202. logoEnabled={false}
  203. styleURL="mapbox://styles/malinverni/cjbf41fkman7q2spxwv9isah3"
  204. centerCoordinate={[2.3469418, 48.8673289]}
  205. zoomLevel={14}
  206. ref={map => (this.map = map)}
  207. style={style.map}
  208. >
  209. {points.map(this.annotation)}
  210. </MapboxGL.MapView>
  211. <View pointerEvents="box-none" style={style.distanceOverlay}>
  212. <View style={style.dotContainer}>
  213. {moving && <View style={style.singleDot} />}
  214.  
  215. {searching && [
  216. <View style={style.threeDot}>
  217. <View style={style.singleSmallDot} />
  218. <View style={style.singleSmallDot} />
  219. <View style={style.singleSmallDot} />
  220. </View>
  221. ]}
  222. </View>
  223. <View style={style.distanceContainer}>
  224. <Text style={style.distanceText}>
  225. {this.getIndicatorContent()}
  226. </Text>
  227. </View>
  228. </View>
  229. <Animated.View
  230. pointerEvents="none"
  231. style={[style.mapOverlay, { opacity: this.overlayOpacity }]}
  232. />
  233. </Animated.View>
  234. <TouchableOpacity
  235. hitSlop={{ top: 20, left: 20, right: 20, bottom: 20 }}
  236. onPress={this.closeView}
  237. style={style.close}
  238. >
  239. <Cross ratio={0.6}/>
  240. </TouchableOpacity>
  241. </RenderIf>
  242. <DelayRenderer delay={1000}>
  243. <Animated.View
  244. pointerEvents="box-none"
  245. style={[style.over, this.transform]}
  246. >
  247. <Interactable.View
  248. verticalOnly={true}
  249. animatedNativeDriver={true}
  250. animatedValueY={this.deltaY}
  251. ref={r => (this.interactable = r)}
  252. snapPoints={[{ y: 0 }, { y: -HEIGHT_PHONE }]}
  253. boundaries={{ top: -HEIGHT_PHONE, bottom: 0, bounce: 0 }}
  254. style={style.interactable}
  255. >
  256. <View style={style.pager}>
  257. <Pager
  258. disabled={!isOpen}
  259. setOffset={o => (this.offset = o)}
  260. setScrollTo={s => (this.scrollTo = s)}
  261. setIsDragging={s => this.isSliderDragging = s}
  262. onResponderRelease={this.onSliderRelease}
  263. onScrollEnd={this.onScrollEnd}
  264.  
  265. indicatorPosition="none"
  266. >
  267. {renderedPoints.map((x, i) => (
  268. <SliderItem
  269. key={i}
  270. data={x}
  271. open={this.open}
  272. deltaY={this.deltaY}
  273. />
  274. ))}
  275. </Pager>
  276. </View>
  277. <Ovni />
  278. </Interactable.View>
  279.  
  280. <Animated.View
  281. style={[
  282. style.squares,
  283. { transform: [{ translateY: this.deltaY }] }
  284. ]}
  285. >
  286. <DelayRenderer delay={1000}>
  287. {points[index] && (
  288. <Content
  289. data={points[index]}
  290. deltaY={this.deltaY}
  291. close={this.close}
  292. />
  293. )}
  294. </DelayRenderer>
  295. </Animated.View>
  296. </Animated.View>
  297. </DelayRenderer>
  298. </View>
  299. );
  300. }
  301. }
Add Comment
Please, Sign In to add comment