Skip to content

Commit f1b160c

Browse files
authored
Merge pull request #4 from khaiql/example
Example
2 parents 69d47e8 + 39c5b0c commit f1b160c

File tree

11 files changed

+223
-28
lines changed

11 files changed

+223
-28
lines changed

.expo/packager-info.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"expoServerPort": null,
3+
"expoServerNgrokUrl": null,
4+
"packagerNgrokUrl": null,
5+
"ngrokPid": null,
6+
"packagerPort": null,
7+
"packagerPid": null
8+
}

.expo/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"hostType": "tunnel",
3+
"lanType": "ip",
4+
"dev": true,
5+
"strict": false,
6+
"minify": false,
7+
"urlType": "exp",
8+
"urlRandomness": null
9+
}

example/.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": ["babel-preset-expo"],
3+
"env": {
4+
"development": {
5+
"plugins": ["transform-react-jsx-source"]
6+
}
7+
}
8+
}

example/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/**/*
2+
.expo/*
3+
npm-debug.*

example/.watchmanconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

example/assets/icons/app.png

7 KB
Loading

example/assets/icons/loading.png

7 KB
Loading

example/exp.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "example",
3+
"description": "An empty new project",
4+
"slug": "example",
5+
"privacy": "public",
6+
"sdkVersion": "16.0.0",
7+
"version": "1.0.0",
8+
"orientation": "portrait",
9+
"primaryColor": "#cccccc",
10+
"icon": "./assets/icons/app.png",
11+
"loading": {
12+
"icon": "./assets/icons/loading.png",
13+
"hideExponentText": false
14+
},
15+
"packagerOpts": {
16+
"assetExts": ["ttf", "mp4"]
17+
},
18+
"ios": {
19+
"supportsTablet": true
20+
}
21+
}

example/main.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import Expo, { Components } from 'expo';
2+
import React from 'react';
3+
import ProgressiveInput from 'react-native-progressive-input';
4+
import { StyleSheet, Text, View, ListView, TouchableOpacity } from 'react-native';
5+
6+
const GOOGLE_API_KEY = 'AIzaSyB7-8qph-zszuxivIm7cwT5b37D22bm1A4';
7+
const ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1.id !== r2.id });
8+
const latitudeDelta = 0.0922;
9+
const longitudeDelta = 0.0421;
10+
11+
class App extends React.Component {
12+
constructor(props) {
13+
super(props);
14+
this.state = {
15+
isLoading: false,
16+
dataSource: ds.cloneWithRows([]),
17+
value: '',
18+
};
19+
this.searchLocation = this.searchLocation.bind(this);
20+
this.renderRow = this.renderRow.bind(this);
21+
this.renderSeparator = this.renderSeparator.bind(this);
22+
this.onInputCleared = this.onInputCleared.bind(this);
23+
}
24+
25+
async searchLocation(query) {
26+
const url = `https://maps.googleapis.com/maps/api/place/autocomplete/json?key=${GOOGLE_API_KEY}&input=${query}`;
27+
this.setState({ isLoading: true, value: query });
28+
const response = await fetch(url);
29+
const jsonResponse = await response.json();
30+
this.setState({ isLoading: false, dataSource: ds.cloneWithRows(jsonResponse.predictions) });
31+
}
32+
33+
renderRow(prediction) {
34+
return (
35+
<TouchableOpacity onPress={() => this.onListItemClicked(prediction)} style={styles.listItem}>
36+
<Text>{prediction.description}</Text>
37+
</TouchableOpacity>
38+
);
39+
}
40+
41+
renderSeparator() {
42+
return <View style={styles.listItemSeparator} />;
43+
}
44+
45+
onInputCleared() {
46+
this.setState({ value: '', isLoading: false, dataSource: ds.cloneWithRows([]) });
47+
}
48+
49+
async onListItemClicked(prediction) {
50+
this.setState({ value: prediction.description, dataSource: ds.cloneWithRows([]), isLoading: true });
51+
const url = `https://maps.googleapis.com/maps/api/place/details/json?placeid=${prediction.place_id}&key=${GOOGLE_API_KEY}`;
52+
const response = await fetch(url);
53+
const jsonResponse = await response.json();
54+
const { lat, lng } = jsonResponse.result.geometry.location;
55+
this.mapView.animateToRegion({
56+
longitude: lng,
57+
latitude: lat,
58+
latitudeDelta,
59+
longitudeDelta,
60+
});
61+
this.setState({ isLoading: false });
62+
}
63+
64+
render() {
65+
return (
66+
<View style={styles.container}>
67+
<Expo.MapView
68+
style={styles.map}
69+
ref={m => this.mapView = m}
70+
initialRegion={{
71+
latitude: 37.78825,
72+
longitude: -122.4324,
73+
latitudeDelta,
74+
longitudeDelta,
75+
}}
76+
/>
77+
<ProgressiveInput
78+
value={this.state.value}
79+
style={styles.progressiveInput}
80+
isLoading={this.state.isLoading}
81+
onChangeText={this.searchLocation}
82+
onInputCleared={this.onInputCleared}
83+
/>
84+
<View style={styles.listViewContainer}>
85+
<ListView
86+
enableEmptySections
87+
style={styles.listView}
88+
dataSource={this.state.dataSource}
89+
renderRow={this.renderRow}
90+
renderSeparator={this.renderSeparator}
91+
/>
92+
</View>
93+
</View>
94+
);
95+
}
96+
}
97+
98+
const styles = StyleSheet.create({
99+
container: {
100+
flex: 1,
101+
backgroundColor: 'grey',
102+
flexDirection: 'column',
103+
justifyContent: 'flex-start',
104+
},
105+
map: {
106+
position: 'absolute',
107+
top: 0,
108+
right: 0,
109+
left: 0,
110+
bottom: 0,
111+
},
112+
progressiveInput: {
113+
backgroundColor: 'white',
114+
marginTop: 20,
115+
marginLeft: 10,
116+
marginRight: 10,
117+
},
118+
listViewContainer: {
119+
flex: 0,
120+
},
121+
listView: {
122+
backgroundColor: 'white',
123+
margin: 10,
124+
},
125+
listItem: {
126+
padding: 10,
127+
},
128+
listItemSeparator: {
129+
borderWidth: 0.5,
130+
borderColor: 'lightgrey',
131+
},
132+
});
133+
134+
Expo.registerRootComponent(App);

example/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "example",
3+
"version": "0.0.0",
4+
"description": "Hello Expo!",
5+
"author": null,
6+
"private": true,
7+
"main": "main.js",
8+
"dependencies": {
9+
"expo": "16.0.0",
10+
"react": "16.0.0-alpha.6",
11+
"react-native": "https://github.com/expo/react-native/archive/sdk-16.0.0.tar.gz",
12+
"react-native-progressive-input": "^1.0.1",
13+
"react-native-vector-icons": "^4.1.0"
14+
}
15+
}

0 commit comments

Comments
 (0)