-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
342 lines (305 loc) · 8.59 KB
/
App.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import React, { Component } from 'react';
import { Text, View, StyleSheet, Button, Alert, Image, ListView, ScrollView, Animated, TouchableOpacity} from 'react-native';
import { Constants, Location, Permissions } from 'expo';
import * as firebase from 'firebase'; // 4.2.0
const firebaseConfig = {
apiKey: "AIzaSyA78htuLAZ-1aBxQNIj9-ZDawnx1Jrgk0I",
authDomain: "hype-635d3.firebaseapp.com",
databaseURL: "https://hype-635d3.firebaseio.com",
storageBucket: "hype-635d3.appspot.com"
};
try{
firebase.initializeApp(firebaseConfig);
}
catch(err)
{
//fadsfa
}
const firebaseApp = firebase;
const itemsRef = firebaseApp.database().ref();
//var rootRef = firebase.database().ref();
/* example of how to set value to the database
itemsRef.child('/tester').set({
beef: "boos",
chicken: "yoos"
});*/
//const firebaseApp = firebase;
export class HypeButton extends Component {
constructor (props) {
super(props)
this.springValue = new Animated.Value(1)
this.state = {cooldown : false}
}
spring() {
this.springValue.setValue(0.4)
Animated.spring(
this.springValue,
{
toValue: 1,
friction: 1,
}
).start()
}
distance(lat, lon) {
const loc = JSON.parse(this.props.location);
return Math.sqrt(Math.pow(lat - loc.coords.latitude, 2) + Math.pow(lon - loc.coords.longitude, 2));
}
_handleHypePress = () => {
if (this.props.location == null){
return;
}
var d = 0;
var next = 0;
var ret = 1;
d = this.distance(-122.494806, 37.769745);
if (d > (next = this.distance(-122.492956, 37.769745))) {
d = next;
ret = 2;
}
if (d > (next = this.distance(-122.486318, 37.769447))) {
d = next;
ret = 3;
}
if (d > this.distance(-122.483202, 37.769656)) {
ret = 4;
}
this.spring()
console.log(this.props.zones);
const zone = this.props.zones[ret-1];
const value = zone["score"] + 1;
const name = zone["title"];
const lat = zone["latitude"];
const long = zone["longitude"];
itemsRef.child('zones').child(this.props.zones[ret-1]["_key"]).set({
score : value,
title : name,
latitude: lat,
longitude: long,
})
this.setState({cooldown : true})
setTimeout(() => {this.setState({cooldown: false})}, 10000)
}
render() {
return (
<View style = {styles.hype_button}>
<TouchableOpacity onPress = {this._handleHypePress} disabled={this.state.cooldown}>
<Animated.Image
source={{ uri: 'http://i.imgur.com/P5LVcrr.png' }}
style={{ height: 140, width: 200, transform: [{scale: this.springValue}] }}
/>
</TouchableOpacity>
</View>
);
}
}
export class BottomBar extends Component {
_handleButtonPress = () => {
Alert.alert(
'Button pressed!',
'You did it!',
);
itemsRef.child('/tester').push({
stew: "yes",
cool: "poop"
});
};
render() {
return (
<View style={styles.bottom_bar}>
<Button
title="Top"
onPress={this._handleButtonPress}
/>
<Button
title="Map"
onPress={this._handleButtonPress}
/>
<Button
title="Rising"
onPress={this._handleButtonPress}
/>
</View>
);
}
}
export class HelpButton extends Component {
_handleHelpPress = () => {
Alert.alert(
'HYPE when you are feeling a venue! Do 5 HYPEs today for a chance to win free tickets',
);
};
render() {
return (
<View style={styles.help_button}>
<TouchableOpacity onPress = {this._handleHelpPress}>
<Image
source={{ uri: 'http://www.clker.com/cliparts/i/X/D/N/j/p/icon-with-question-mark-hi.png' }}
style={{ height: 40, width: 40, }}
/>
</TouchableOpacity>
</View>
);
}
}
export class ScrollingMapView extends Component {
_handleButtonPress = () => {
Alert.alert(
'Button pressed!',
'You did it!',
);
};
styleZone(zone){
if (zone["user_inside"]){
return {transform : [{scale : 2}]};
}
else{
return {};
}
}
render(){
return (
<ScrollView horizontal={true}
contentContainerStyle = {styles.scrollMapViewContent}
showsHorizontalScrollIndicator = {false}
showsVerticalScrollIndicator = {false}
overScrollMode = 'never'
backgroundColor = '#719263'>
<Image
style={{flex:1, height: undefined, width: undefined}}
source={{ uri: 'https://image.ibb.co/dXScZk/outsidelands.png' }}
/>
<View style={[styles.hype_score, styles.zone1, this.styleZone(this.props.zones[0])]}>
<Text style={{ fontSize: 15, fontWeight: 'bold'}}>
{this.props.zones[0]["score"]}
</Text>
</View>
<View style={[styles.hype_score, styles.zone2, this.styleZone(this.props.zones[1])]}>
<Text style={{ fontSize: 15, fontWeight: 'bold'}}>
{this.props.zones[1]["score"]}
</Text>
</View>
<View style={[styles.hype_score, styles.zone3, this.styleZone(this.props.zones[2])]}>
<Text style={{ fontSize: 15, fontWeight: 'bold'}}>
{this.props.zones[2]["score"]}
</Text>
</View>
<View style={[styles.hype_score, styles.zone4, this.styleZone(this.props.zones[3])]}>
<Text style={{ fontSize: 15, fontWeight: 'bold'}}>
{this.props.zones[3]["score"]}
</Text>
</View>
</ScrollView>
);
}
}
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
zones: Array(4).fill({}),
locationResult: null
};
this.itemsRef = this.getRef().child('zones');
}
getRef() {
return firebaseApp.database().ref();
}
listenForItems(itemsRef) {
itemsRef.on('value', (snap) => {
// get children as an array
var items = [];
snap.forEach((zone) => {
items.push({
title: zone.child("title").val(),
score: zone.child("score").val(),
latitude: zone.child("latitude").val(),
longitude: zone.child("longitude").val(),
_key: zone.key
});
});
this.markNearestZone(items, JSON.parse(this.state.locationResult));
this.setState({
zones: items
});
});
}
componentDidMount() {
this.listenForItems(this.itemsRef);
this._getLocationAsync();
}
dist(zone, location) {
return Math.sqrt(Math.pow(zone['latitude'] - location.coords.latitude, 2) + Math.pow(zone['longitude'] - location.coords.longitude, 2));
}
markNearestZone(zones, location) {
var mindist = 1000000;
var minind = 0;
zones.forEach((zone, index) => {
if (this.dist(zone, location) < mindist){
mindist = this.dist(zone, location);
minind = index;
}
})
zones.forEach((zone, index) =>{
if (index == minind){
zone["user_inside"] = true;
}
else{
zone["user_inside"] = false;
}
})
}
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
locationResult: 'Permission to access location was denied',
});
}
let location = await Location.getCurrentPositionAsync({});
this.markNearestZone(this.state.zones, location);
this.setState({ locationResult: JSON.stringify(location),
zones : this.state.zones
});
};
render() {
return (
<View style={styles.container}>
<ScrollingMapView zones = {this.state.zones}
/>
<HypeButton location = {this.state.locationResult}
zones = {this.state.zones}
/>
<HelpButton />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
paddingTop: Constants.statusBarHeight,
backgroundColor: '#719263',
},
bottom_bar: {
flex: 1,
flexDirection: 'row',
justifyContent: 'space-between',
bottom: 80
},
hype_button : {
alignItems: 'center',
bottom: 80,
},
help_button : {
alignItems: 'right',
bottom: 90,
},
scrollMapViewContent: {
width : 1185, // <--- set the max width of the scrolled content
height : 459, // <--- set the max height of the scrolled content
},
hype_score: {position: 'absolute', right: 0, bottom: 0, width: 65, height: 30, borderRadius: 10, borderColor: '#f00', borderWidth: 5, backgroundColor: '#ff0', alignItems: 'center', fontSize: 100},
zone1: {top: 280, left: 90},
zone2: {top: 70, left: 220},
zone3: {top: 80, left: 770},
zone4: {top: 80, left: 970},
});