Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { PureComponent } from 'react';
  2. import { StyleSheet, InteractionManager, Alert, View, FlatList } from 'react-native';
  3. import {
  4.   Container,
  5.   Header,
  6.   Title,
  7.   Content,
  8.   Footer,
  9.   FooterTab,
  10.   Left,
  11.   Body,
  12.   Right,
  13.   List,
  14.   ListItem,
  15.   Text,
  16.   Button,
  17.   Icon,
  18.   Spinner
  19. } from 'native-base';
  20. import BackgroundGeolocation from 'react-native-mauron85-background-geolocation';
  21.  
  22. const styles = StyleSheet.create({
  23.   iconStyle: {
  24.     color: '#0A69FE'
  25.   }
  26. });
  27.  
  28. const LogItem = ({
  29.   id: locationId,
  30.   latitude,
  31.   longitude,
  32.   time,
  33. }) => {
  34.   const date = new Date(time);
  35.   return (
  36.     <ListItem>
  37.       <Text>{`${locationId}`}</Text>
  38.       <Body>
  39.         <View>
  40.         <Text>{`lat: ${latitude}`}</Text>
  41.         <Text>{`lon: ${longitude}`}</Text>
  42.         <Text>{`time: ${date.toLocaleDateString()} ${date.toLocaleTimeString()}`}</Text>
  43.         </View>
  44.       </Body>
  45.     </ListItem>
  46.   );
  47. };
  48.  
  49. class AllLocationsScene extends PureComponent {
  50.   static navigationOptions = {
  51.     title: 'All Locations',
  52.     header: null,
  53.   };
  54.  
  55.   constructor(props) {
  56.     super(props);
  57.     this.state = { locations: null, selectedLocationId: -1, isReady: false };
  58.     this.refresh = this.refresh.bind(this);
  59.   }
  60.  
  61.   componentDidMount() {
  62.     InteractionManager.runAfterInteractions(() => {
  63.       this.refresh();
  64.     });
  65.   }
  66.  
  67.   refresh() {
  68.     this.setState({ selectedLocationId: -1, isReady: false });
  69.     BackgroundGeolocation.getLocations(locations => {
  70.       this.setState({ locations, isReady: true });
  71.     });
  72.   }
  73.  
  74.   _keyExtractor = (item, index) => item.id;
  75.  
  76.   render() {
  77.     const { locations, isReady } = this.state;
  78.     return (
  79.       <Container>
  80.         <Header>
  81.           <Body>
  82.             <Title>All Locations</Title>
  83.           </Body>
  84.         </Header>
  85.         <Content>
  86.           {(() => {
  87.             if (!isReady) {
  88.               return <Spinner />;
  89.             }
  90.             return (
  91.               <FlatList style={{ flex: 1, backgroundColor: '#fff' }}
  92.                 data={locations}
  93.                 keyExtractor={this._keyExtractor}
  94.                 renderItem={({ item }) => {
  95.                   const date = new Date(item.time);
  96.                   return (
  97.                     <LogItem
  98.                       {...item}
  99.                       onPress={this.onLocationSelected}
  100.                     />
  101.                   );
  102.                 }}
  103.               />
  104.             );
  105.           })()}
  106.         </Content>
  107.       </Container>
  108.     );
  109.   }
  110. }
  111.  
  112. export default AllLocationsScene;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement