-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathApp-Step9-Navigation.js
140 lines (129 loc) · 3.19 KB
/
App-Step9-Navigation.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
import React from 'react';
import {
ScrollView,
StyleSheet,
Text,
View,
} from 'react-native';
import { ListItem } from 'react-native-elements';
import { StackNavigator } from 'react-navigation';
class CityWeather extends React.Component {
render() {
return (
<ListItem
title={this.props.name}
subtitle={
this.props.weather ? this.props.weather : 'TBD'
}
badge={{
value: this.props.temp
? this.props.temp + ' °F'
: '0 °F',
badgeContainerStyle: {
backgroundColor: 'lightblue',
},
}}
avatar={{ uri: this.props.icon }}
onPress={
this.props.onPress ? this.props.onPress() : null
}
/>
);
}
}
class MasterScreen extends React.Component {
static navigationOptions = {
title: 'Weather',
};
constructor() {
super();
this.state = {
cities: [
{ name: 'San Jose', id: 5392171 },
{ name: 'New York', id: 5128581 },
{ name: 'London', id: 2643744 },
{ name: 'Paris', id: 2968815 },
{ name: 'Hong Kong', id: 1819729 },
{ name: 'Singapore', id: 1880252 },
{ name: 'Beijing', id: 1816670 },
{ name: 'Sydney', id: 6619279 },
{ name: 'São Paulo', id: 3448439 },
{ name: 'San Juan', id: 4568138 },
{ name: 'Mumbai', id: 1275339 },
{ name: 'Reykjavík', id: 6692263 },
],
};
}
componentDidMount() {
const ids = this.state.cities
.map(city => city.id)
.toString();
fetch(
`http://api.openweathermap.org/data/2.5/group?units=imperial&APPID=b1b35bba8b434a28a0be2a3e1071ae5b&id=${ids}`
)
.then(res => res.json())
.then(body =>
body.list.map(city => {
return {
id: city.id,
name: city.name,
temp: city.main.temp,
icon: 'http://openweathermap.org/img/w/' +
city.weather[0].icon +
'.png',
weather: city.weather[0].main,
};
}))
.then(cities => {
this.setState({
cities: cities,
});
});
}
render() {
return (
<ScrollView style={{ backgroundColor: 'white' }}>
{this.state.cities.map(city => (
<CityWeather
key={city.id}
name={city.name}
temp={city.temp}
weather={city.weather}
icon={city.icon}
onPress={() =>
() =>
this.props.navigation.navigate('Detail', {
cityId: city.id,
})}
/>
))}
</ScrollView>
);
}
}
class DetailScreen extends React.Component {
static navigationOptions = {
title: 'Details',
};
render() {
return (
<View style={styles.container}>
<Text>
Detailed weather information coming soon.
</Text>
</View>
);
}
}
export default (MainNavigator = StackNavigator({
Master: { screen: MasterScreen },
Detail: { screen: DetailScreen },
}));
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});