forked from michaelwoon/R2S2-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableView.js
162 lines (143 loc) · 3.44 KB
/
TableView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
import React, { Component } from 'react';
import {
FlatList,
StyleSheet,
Text,
TextInput,
View,
Button,
ActivityIndicator,
Image,
TouchableHighlight,
} from 'react-native';
import base64 from "react-native-base64";
class ListItem extends React.PureComponent {
_onPress = () => {
this.props.onPressItem(this.props.item,this.props.index);
}
fixCase(str){
str = str.toLowerCase();
return str.charAt(0).toUpperCase() + str.slice(1);
};
floodingColor(waterLevel){
if (waterLevel > 0) {
return '#ff8787';
}
else if (waterLevel < 0 && waterLevel > -0.3) {
return '#fdffa0';
}
else {
return '';
}
};
render() {
const item = this.props.item;
return (
<TouchableHighlight
onPress={this._onPress}
underlayColor='#dddddd'>
<View>
<View style={styles.rowContainer}>
<View style={styles.textContainer}
backgroundColor = {this.floodingColor(item.floodedby)}
>
<Text style={styles.road}>{this.fixCase(item.roadname)}</Text>
<Text style={styles.title}
numberOfLines={1}>{this.fixCase(item.stream)}</Text>
</View>
</View>
<View style={styles.separator}/>
</View>
</TouchableHighlight>
);
}
}
type Props = {};
export default class TableView extends Component<Props> {
constructor(props){
super(props);
this.state = { isLoading: true};
this.bridges = [];
};
static navigationOptions = {
title: 'Table',
};
loadBridges(){
let headers = new Headers();
headers.append(
"Authorization",
"Basic " + base64.encode(global.token + ":x")
);
const { params } = this.props.navigation.state;
console.log("date: " + params.date);
fetch("http://35.194.88.251/api/bridges/" + params.date, {
method: "GET",
headers: headers
})
.then(function(response) {
return response.json();
})
.then(json => {
console.log("data received");
this.setState({
isLoading: false,
bridges: json,
});
})
.catch(function(ex) {
console.log("parsing failed or no token", ex);
});
};
fixCase(str){
str = str.toLowerCase();
return str.charAt(0).toUpperCase() + str.slice(1);
};
componentDidMount(){
this.loadBridges();
};
_keyExtractor = (item, index) => index.toString();
_renderItem = ({item, index}) => (
<ListItem
item={item}
index={index}
onPressItem={this._onPressItem}
/>
);
_onPressItem = (item,index) => {
console.log("Pressed row: "+index);
this.props.navigation.navigate('Info', {bridge: item});
};
render() {
const { params } = this.props.navigation.state;
return (
<FlatList
data={this.state.bridges}
keyExtractor={this._keyExtractor}
renderItem={this._renderItem}
/>
);
}
}
const styles = StyleSheet.create({
textContainer: {
flex: 1
},
separator: {
height: 1,
backgroundColor: '#dddddd'
},
road: {
fontSize: 25,
fontWeight: 'bold',
color: '#3d3d3d'
},
title: {
fontSize: 20,
color: '#3d3d3d'
},
rowContainer: {
flexDirection: 'row',
padding: 10,
},
});