Advertisement
Guest User

Android APP

a guest
Jan 23rd, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Sample React Native App
  3.  * https://github.com/facebook/react-native
  4.  * @flow
  5.  */
  6.  
  7. import React, {Component} from 'react';
  8. import {AppRegistry, StyleSheet, Alert, Text, View, TextInput, Button} from 'react-native';
  9. import _ from 'lodash';
  10. import CheckBox from 'react-native-checkbox';
  11.  
  12.  
  13. export default class Sensors extends Component {
  14.     constructor(props) {
  15.         super(props);
  16.  
  17.         this.state = {
  18.             list: [],
  19.             url: 'http://septen.pl/json.php',
  20.             emptyList: true,
  21.             changeUrl: false
  22.         };
  23.     }
  24.  
  25.     componentDidMount() {
  26.         this.interval = setInterval(() => {
  27.             if (this.state.changeUrl) {
  28.                 fetch(this.state.url)
  29.                     .then((response) => response.json())
  30.                     .then((responseJson) => {
  31.                         let keys = _.keys(responseJson.data);
  32.  
  33.                         this.setState({
  34.                             list: _.map(keys, (key, i) => ({
  35.                                 ...responseJson.data[key],
  36.                                 checked: typeof this.state.list[i] != 'undefined' ? this.state.list[i].checked : true
  37.                             })),
  38.                             emptyList: false
  39.                         });
  40.                     })
  41.                     .catch((error) => {
  42.                         this.setState({
  43.                             list: [],
  44.                             emptyList: true
  45.                         }, () => {
  46.                             clearInterval(this.interval);
  47.                             Alert.alert(
  48.                                 'Connection error',
  49.                                 'Connection error'
  50.                             )
  51.                         });
  52.                     });
  53.             }
  54.         }, 1000);
  55.     }
  56.  
  57.     onChange(i) {
  58.         this.state.list[i].checked = !this.state.list[i].checked;
  59.         this.setState({list: this.state.list});
  60.     }
  61.  
  62.     onChangeText(url) {
  63.         this.state.url = url;
  64.     }
  65.  
  66.     onClick() {
  67.         this.setState({changeUrl: true});
  68.     }
  69.  
  70.     render() {
  71.         const {emptyList, list} = this.state;
  72.  
  73.         let filteredList = _.filter(list, {checked: true});
  74.  
  75.  
  76.         return <View style={styles.container2}>
  77.             {emptyList && <TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}}
  78.                                      label="URL" value={this.state.url}
  79.                                      onChangeText={this.onChangeText.bind(this)}/>}
  80.             {emptyList && <Button onPress={this.onClick.bind(this)}
  81.                                   title="Ustaw url"/>}
  82.             {emptyList && <Text style={styles.empty}>
  83.               Brak sensorów
  84.             </Text>}
  85.             {!emptyList && <Text style={styles.sensors}>
  86.               Sensory
  87.             </Text>}
  88.           <View style={{flexDirection: 'row', flexWrap: 'wrap', marginBottom: 50}}>
  89.               {
  90.                   _.map(list, (elem, i) => <CheckBox label={elem.sensor_name}
  91.                                                      style={{flexGrow: 0}}
  92.                                                      key={i}
  93.                                                      checked={elem.checked}
  94.                                                      onChange={this.onChange.bind(this, i)}/>)
  95.               }
  96.           </View>
  97.             {
  98.                 _.map(filteredList, (elem, i) => {
  99.                     let length = elem.measurement.length;
  100.                     let value = elem.measurement[length - 1].measurement_value;
  101.  
  102.                     return <Text key={i}>{`${elem.sensor_name} wartość: ${value}`}</Text>
  103.                 })
  104.             }
  105.         </View>;
  106.     }
  107. }
  108.  
  109. const styles = StyleSheet.create({
  110.     container: {
  111.         flex: 1,
  112.         justifyContent: 'center',
  113.         alignItems: 'center',
  114.         backgroundColor: '#F5FCFF'
  115.     },
  116.     container2: {
  117.         flex: 1,
  118.         paddingTop: 30,
  119.         alignItems: 'center',
  120.         backgroundColor: '#F5FCFF'
  121.     },
  122.     sensors: {
  123.         fontSize: 30,
  124.         margin: 10
  125.     },
  126.     empty: {
  127.         fontSize: 30,
  128.         margin: 10
  129.     },
  130.     sensor: {
  131.         alignSelf: "flex-start",
  132.         fontSize: 18,
  133.         margin: 10
  134.     }
  135. });
  136.  
  137.  
  138. AppRegistry.registerComponent('Sensors', () => Sensors);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement