-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ios.js
451 lines (411 loc) · 10.8 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
'use strict';
var PARSE_APPLICATION_ID = 'Y31uHUHsSOcmfEjLPt2uvT2Qmc53EuX2JCB0iHsK';
var PARSE_JAVASCRIPT_KEY = 'PUA1mxzlF8dA6DgQQlK5UhO3zHDOH3tr7mp2usNu';
var GOOGLE_MAPS_API_KEY = 'AIzaSyBDZOWrvmGMgmhimndfQa9TnFn21M_rTAQ';
var React = require('react-native');
var Parse = require('parse/react-native');
var ParseReact = require('parse-react/react-native');
var RefreshableListView = require('react-native-refreshable-listview');
var FBSDKLogin = require('react-native-fbsdklogin');
var {
AppRegistry,
Button,
DatePickerIOS,
Heading,
Image,
ListView,
NavigatorIOS,
ScrollView,
StyleSheet,
Text,
TextInput,
TouchableHighlight,
StatusBarIOS,
View,
} = React;
var {
FBSDKLoginButton,
} = FBSDKLogin;
Parse.initialize(
PARSE_APPLICATION_ID,
PARSE_JAVASCRIPT_KEY,
);
var Divebook = React.createClass({
render: function() {
StatusBarIOS.setStyle('light-content', true);
return (
<NavigatorIOS
style={styles.navigator}
initialRoute={{
title: 'Divebook',
component: DivebookSplash,
}}
/>
);
}
});
var DivebookSplash = React.createClass({
render: function() {
return (
<View style={styles.splashContainer}>
<Text style={styles.splashText} onPress={this.continue}>Divebook</Text>
<Image source={require('image!diver')} style={styles.splashImage} />
<Login />
</View>
);
},
continue: function() {
console.log("clicked splash text");
StatusBarIOS.setStyle('default', true);
this.props.navigator.push({
title: 'Divebook',
component: DivesitesList,
});
},
});
var Login = React.createClass({
render: function() {
return (
<View>
<FBSDKLoginButton
onLoginFinished={(error, result) => {
if (error) {
alert('Error logging in.');
} else {
if (result.isCanceled) {
alert('Login cancelled.');
} else {
alert('Logged in.');
}
}
}}
onLogoutFinished={() => alert('Logged out.')}
readPermissions={[]}
publishPermissions={['publish_actions']}/>
</View>
);
}
});
var EmptyView = React.createClass({
render: function() {
return (
<View style={styles.container}>
<Text>Empty View</Text>
</View>
);
},
});
var DivesitesList = React.createClass({
mixins: [ParseReact.Mixin], // Enable query subscriptions
watchID: (null: ?number), // to store geolocation watcher
observe: function() {
// The results will be available at this.data.divesites
return {
divesites: (new Parse.Query('divesite')).near('location', this.state.position)
};
},
getInitialState: function () {
return {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loading: true,
position: undefined,
};
},
componentDidUpdate: function(prevProps, prevState) {
if (prevState.loading && (this.pendingQueries().length == 0)) {
this.setState({ loading: false });
}
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(position) => this.setState({position: positionToGeoPoint(position)}),
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.watchID = navigator.geolocation.watchPosition((position) => {
positionToGeoPoint(position)
this.setState({position: positionToGeoPoint(position)});
});
},
componentWillUnmount: function() {
navigator.geolocation.clearWatch(this.watchID);
},
reloadListView: function() {
this.refreshQueries();
},
render: function() {
if (this.state.loading) {
return this.renderLoadingView();
}
return (
<RefreshableListView
dataSource={this.state.dataSource.cloneWithRows(this.data.divesites)}
renderRow={this.renderDivesiteView}
style={styles.listView}
loadData={this.reloadListView}
refreshDescription="Finding divesites nearby…"
renderHeaderWrapper={this.renderHeaderWrapper}
/>
);
},
renderDivesiteView: function(divesite) {
var src = mapImageSrc(divesite.location)
return (
<TouchableHighlight onPress={this.showDivesite.bind(this, divesite)}>
<View style={styles.rowContainer}>
<View style={styles.rightContainer}>
<Text style={styles.title}>{divesite.name}</Text>
<Text style={styles.subtitle}>{divesite.address}</Text>
</View>
<Image style={styles.thumb} source={{uri: src}} />
</View>
</TouchableHighlight>
);
},
renderLoadingView: function() {
return (
<View style={styles.container}>
<Text>
Loading…
</Text>
</View>
);
},
renderHeaderWrapper: function(refreshingIndicator) {
return (
<View>
{refreshingIndicator}
</View>
)
},
showDivesite: function(divesite) {
this.props.navigator.push({
component: DivesiteShow,
passProps: { divesite },
});
},
});
var DivesiteShow = React.createClass({
render: function() {
var divesite = this.props.divesite;
var src = mapImageSrc(divesite.location, {width: 800, height: 400});
return (
<View style={styles.container}>
<Text style={styles.title}>{divesite.name}</Text>
<Text style={styles.subtitle}>{divesite.address}</Text>
<Image style={styles.map} source={{uri: src}} />
<TouchableHighlight
style={styles.button}
onPress={this.logDive.bind(this, divesite)}>
<Text style={styles.buttonText}>Log a dive here…</Text>
</TouchableHighlight>
</View>
);
},
logDive: function(divesite) {
this.props.navigator.push({
component: DivesiteLog,
passProps: { divesite },
});
},
});
var DivesiteLog = React.createClass({
mixins: [ParseReact.Mixin],
observe: function() {
// blank on purpose
},
getDefaultProps: function () {
return {
date: new Date(),
timeZoneOffsetInHours: (-1) * (new Date()).getTimezoneOffset() / 60,
};
},
getInitialState: function() {
return {
date: this.props.date,
timeZoneOffsetInHours: this.props.timeZoneOffsetInHours,
};
},
render: function() {
var divesite = this.props.divesite;
return (
<ScrollView
ref='scrollView'
contentContainerStyle={styles.container}
keyboardDismissMode='on-drag'
>
<Text style={styles.subtitle}>Log dive in</Text>
<Text style={styles.title}>{divesite.name}</Text>
<Text style={styles.subtitle}>{divesite.address}</Text>
<Text style={styles.label}>Start Time</Text>
<DatePickerIOS
date={this.state.date}
mode="datetime"
timeZoneOffsetInMinutes={this.state.timeZoneOffsetInHours * 60}
onDateChange={this.onDateChange}
/>
<Text style={styles.label}>Max. Depth (meters)</Text>
<TextInput
ref='maxDepth'
style={styles.input}
onFocus={inputFocused.bind(this, 'maxDepth')}
keyboardType='numeric'
onChange={this.onInputMaxDepth}
/>
<Text style={styles.label}>Water Temperature (celsius)</Text>
<TextInput
ref='temperature'
style={styles.input}
onFocus={inputFocused.bind(this, 'temperature')}
keyboardType='numeric'
onChange={this.onInputTemp.bind(this)}
/>
<TouchableHighlight
style={styles.button}
onPress={this.log}>
<Text style={styles.buttonText}>Log it</Text>
</TouchableHighlight>
</ScrollView>
);
},
onDateChange: function(date) {
this.setState({
date: date
});
},
onInputMaxDepth: function(event) {
this.setState({
maxDepth: parseFloat(event.nativeEvent.text)
});
},
onInputTemp: function(event) {
this.setState({
temperature: parseFloat(event.nativeEvent.text)
});
},
log: function() {
var divesite = this.props.divesite;
var data = {
start: this.state.date,
maxDepth: this.state.maxDepth,
temperature: this.state.temperature,
};
ParseReact.Mutation.Create('dive', data)
.dispatch()
.then(function(dive) {
ParseReact.Mutation.AddRelation(dive, 'divesite', divesite).dispatch();
})
.then(this.handleSuccess)
.fail(handleError);
},
handleSuccess: function(dive) {
alert("Dive logged!");
this.props.navigator.pop();
},
});
var styles = StyleSheet.create({
splashContainer: {
alignItems: 'center',
backgroundColor: '#212342',
flex: 1,
justifyContent: 'center',
},
splashImage: {
height: 120,
width: 260,
marginBottom: 50,
marginTop: 50,
},
splashText : {
color: 'white',
fontWeight: 'bold',
fontSize: 22,
},
navigator: {
flex: 1,
},
rowContainer: {
flex: 1,
flexDirection: 'row',
backgroundColor: '#F5FCFF',
borderBottomColor: '#CCCCCC',
borderBottomWidth: 1,
},
rightContainer: {
flex: 1,
padding: 10,
},
container: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
paddingTop: 65,
},
title: {
fontSize: 20,
},
subtitle: {
color: '#333333',
},
listView: {
paddingTop: 65,
backgroundColor: '#F5FCFF',
},
thumb: {
height: 100,
width: 100,
},
map: {
height: 200,
width: 400,
margin: 20,
},
button: {
borderRadius: 5,
borderWidth: 2,
marginTop: 20,
padding: 20,
},
buttonText: {
fontSize: 20,
},
label: {
fontSize: 20,
marginBottom: 10,
marginTop: 20,
},
input: {
borderWidth: 1,
height: 40,
marginLeft: 10,
marginRight: 10,
},
});
// Auxiliary functions (TODO: modularize)
var mapImageSrc = function(geopoint, options = { width: 200, height: 200 }) {
return `https://maps.googleapis.com/maps/api/staticmap?markers=size:tiny|${geopoint.latitude},${geopoint.longitude}&zoom=8&size=${options.width}x${options.height}&key=${GOOGLE_MAPS_API_KEY}`
};
var positionToGeoPoint = function(position) {
return {
latitude: position.coords.latitude,
longitude: position.coords.longitude
}
};
// Scroll a component into view. Just pass the component ref string.
// http://stackoverflow.com/questions/29313244/how-to-auto-slide-the-window-out-from-behind-keyboard-when-textinput-has-focus
var inputFocused = function(refName) {
setTimeout(() => {
let scrollResponder = this.refs.scrollView.getScrollResponder();
scrollResponder.scrollResponderScrollNativeHandleToKeyboard(
React.findNodeHandle(this.refs[refName]),
110, //additionalOffset
true
);
}, 50);
};
var handleError = function(error) {
alert(error.message);
};
AppRegistry.registerComponent('Divebook', () => Divebook);