-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.ios.js
160 lines (151 loc) · 5.05 KB
/
index.ios.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
'use strict';
var React = require('react-native');
var Dimensions = require('Dimensions');
var Common = require('./common/common');
var WeekView = require('./components/WeekView');
var Chart = require('./components/Chart');
var LoadingOverlay = require('./components/LoadingOverlay');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var WeatherApp = React.createClass({
statics: {
flex : [2,1,6,6]
},
getInitialState: function() {
var windowSize = Dimensions.get('window');
var flexItemHeight = windowSize.height / WeatherApp.flex.reduce(function(a, b){return a+b;});
return {
current: {
city: "",
weather: "",
temp: "",
date: "",
icon: ""
},
weekViewData: [],
chartWidth: windowSize.width,
chartHeight: flexItemHeight * 2,
chartTop: flexItemHeight * 11,
//chartData: [[],[]], TODO: can not fire binding,why?
chartData: null,
chartLineColors : ["#d35400","#a0dcdc"],
chartPointFillColors : ["#e67e22","#a0dcdc"],
chartPointStrokeColors : ["#d35400","#ffffff"],
isOverlayVisible: true,
}
},
getCurrent: function(data){
var now = Date.now();
var current = data.list.filter(function(item) {
return Math.abs(item.dt * 1000 - now) < (3 * 60 * 60 * 1000); // less than 3 hrs
})[0];
var _today = Common.today(now);
return {
city: data.city.name + ',' + data.city.country,
weather: current.weather[0].description,
temp: Common.f2c(current.main.temp),
date: _today[1] + '.' + _today[2] + ' ' + _today[0],
icon: Common.icon(current.weather[0].icon)
}
},
componentDidMount: function () {
var that = this;
fetch("http://api.openweathermap.org/data/2.5/forecast?q=Shanghai,cn&lang=zh_cn")
.then((response) => response.json())
.then(function(data) {
var current = that.getCurrent(data);
var chartData = [];
var temp_max_arr = [];
var temp_min_arr = [];
Common.week(data.list).map((item, index) => {
temp_max_arr.push(item.max);
temp_min_arr.push(item.min);
});
chartData.push(temp_max_arr);
chartData.push(temp_min_arr);
chartData.map((item, index) => {
item.push(item[0]);
item.unshift(item[0]);
});
that.setState({
current: current,
chartData: chartData,
weekViewData: data.list,
isOverlayVisible: false,
});
})
.catch((error) => {
//errorFunc && errorFunc(error);
})
.done();
},
render: function() {
return (
<View style = { styles.container }>
<View style = { styles.city_day }>
<Text style = { styles.city_day_text }>
{ this.state.current.city } / { this.state.current.date }
</Text>
</View>
<Text style = { styles.status }>
{ this.state.current.weather } { this.state.current.temp }°
</Text>
<Text style = { [styles.current, styles.weather_icon] }>
{ this.state.current.icon }
</Text>
<WeekView data={this.state.weekViewData} flex={WeatherApp.flex.slice(3)}/>
<Chart
data={this.state.chartData}
width={this.state.chartWidth}
height={this.state.chartHeight}
top={this.state.chartTop}
pointFillColors={this.state.chartPointFillColors}
pointStrokeColors={this.state.chartPointStrokeColors}
lineColors={this.state.chartLineColors}
/>
<LoadingOverlay isVisible={this.state.isOverlayVisible} />
</View>
);
}
});
var styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#3d566e',
flexDirection: 'column'
},
city_day: {
flex: WeatherApp.flex[0],
alignItems: 'center',
flexDirection: 'row',
},
city_day_text: {
justifyContent: 'center',
alignSelf: 'auto',
flex: 1,
fontSize: 20,
textAlign: 'center',
color: '#95a5a6',
},
status: {
flex: WeatherApp.flex[1],
fontSize: 20,
textAlign: 'center',
color: '#f1c40f',
fontWeight: 'bold',
},
current: {
textAlign: 'center',
fontSize: 130,
color: '#ffffff',
flex: WeatherApp.flex[2]
},
weather_icon: {
fontFamily: 'WeatherIcons-Regular',
},
});
AppRegistry.registerComponent('WeatherApp', () => WeatherApp);