-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ios.js
386 lines (368 loc) · 13.1 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
'use strict';
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
TouchableWithoutFeedback,
TouchableOpacity,
AsyncStorage,
Dimensions,
Alert,
} from 'react-native';
import Button from 'apsl-react-native-button';
import Svg, {Text as SVGText, Circle, Polyline} from 'react-native-svg';
import Graph from 'react-native-line-plot';
import {styles, colorControl} from './styles.js';
var startTime;
// TODO: Get rid of this nasty use of state.
var testState = 0;
// Main class, contains navigator.
var ReactionTraining = React.createClass({
renderScene (route, navigator) {
return <route.component navigator={navigator} {...route.passProps} />
},
render () {
return(
<Navigator
renderScene={(route, navigator) =>
this.renderScene(route, navigator)}
initialRoute={{component: Menu}}
configureScene={this.configureScene}
/>
);
}
});
// Menu screen.
var Menu = React.createClass({
_navigate: function () {
this.props.navigator.push({component: GameInit})
},
_goToSettings: function () {
this.props.navigator.push({component: Settings})
},
_goToDataScreen: function () {
this.props.navigator.push({component: DataScreen})
},
render () {
return (
<View style={styles.bg}>
<View style={styles.container}>
<Svg height="300" width="350">
<Circle
fill="none"
stroke={colorControl.buttonColor}
strokeWidth="18"
cx="175"
cy="125"
r="110"
/>
<SVGText
fill="none"
stroke={colorControl.buttonBorderColor}
strokeWidth="1.3"
fontSize="64"
fontWeight="900"
x="175"
y="85"
textAnchor="middle"
>SACCADE</SVGText>
</Svg>
<Button
style={styles.button}
textStyle={styles.buttonTextStyle}
onPress={this._navigate}>
TRAIN
</Button>
<Button
style={styles.button}
textStyle={styles.buttonTextStyle}
onPress={this._goToDataScreen}>
DATA
</Button>
<Button
style={styles.button}
textStyle={styles.buttonTextStyle}
onPress={this._goToSettings}>
SETTINGS
</Button>
</View>
</View>
)
}
});
// Display graph of user reaction speed over time.
var DataScreen = React.createClass({
getInitialState () {
return { isLoading: true }
},
componentWillMount () {
var reactionData = [];
var keyList = [];
AsyncStorage.getAllKeys((error, keys) => {
if (error) { console.warn(error) } else {
for (var i in keys) {
keyList.push(keys[i]);
}
}
}).then(()=>{
keyList.forEach((listItem, index) => {
AsyncStorage.getItem(listItem, (error, result) => {
if (error) { console.warn(error) }
else { reactionData.push([listItem, result]); }
});
});
}).then(() => {
setTimeout(() => {
console.warn("reactionData:",reactionData);
this.setState({
reactionData: reactionData,
isLoading: false
});
}, 300);
});
},
_back: function () {
this.props.navigator.pop();
},
render () {
var xAxisDensity = 5;
if (this.state.isLoading){
return(<View style={styles.bg}/>)
}
if (this.state.reactionData.length < 2){
return(
<View style={styles.bg}>
<View style={styles.container}>
<Text style={styles.warningTextStyle}>
Graphing requires at least two days of data,
come back tomorrow!
</Text>
<Button
style={styles.thinButton}
textStyle={styles.buttonTextStyle}
onPress={this._back}>
BACK
</Button>
</View>
</View>
)
}
else if (this.state.reactionData.length < 6) {
xAxisDensity = 2;
}
return(
<View style={styles.bg}>
<View style={styles.container}>
<Graph data={this.state.reactionData}
graphColorPrimary={colorControl.buttonColor}
graphColorSecondary={colorControl.buttonBorderColor}
xUnit="date"
yUnit="ms"
xAxisDensity={xAxisDensity}
/>
<Button
style={styles.thinButton}
textStyle={styles.buttonTextStyle}
onPress={this._back}>
BACK
</Button>
</View>
</View>
)
},
});
// Settings menu, only clears data, no other functionality.
var Settings = React.createClass({
deleteData: function () {
AsyncStorage.clear((error) => {if (error) {console.warn(error)}});
},
_back: function () {
this.props.navigator.pop();
},
render () {
return(
<View style={styles.bg}>
<View style={styles.container}>
<Button
textStyle={styles.buttonTextStyle}
style={styles.button}
onPress={this.deleteData}>
CLEAR DATA
</Button>
<Button
textStyle={styles.buttonTextStyle}
style={styles.button}
onPress={this._back}>
BACK
</Button>
</View>
</View>
)
}
});
// Start reaction test from this screen.
var GameInit = React.createClass({
getInitialState() {
return { activeState: 0 }
},
_navigate: function () {
this.setState({ activeState: 1 });
testState += 1;
/* Each click increments testState by 1 so if a user clicks in between
* triggering the test and GameLive being pushed onto the view stack
* they should be taken to the cheating screen. */
if (testState == 1) {
let delay = 2500 + 1000 * Math.random()
window.setTimeout(() => {
if (testState == 1){
this.props.navigator.replace({component: GameLive})
}
}, delay)
}
else {
testState = 0;
this.props.navigator.replace({component: GameResult,
passProps: {timeElapsed: 0}});
}
},
_back: function () {
testState = 0;
this.props.navigator.popToTop({component: Menu});
},
render () {
var gameInitStyle = styles.container;
var gameBGStyle = styles.bg;
var bigButtonStyle = styles.bigButton;
var thinButtonStyle = styles.thinButton;
if (this.state.activeState) {
gameInitStyle = styles.containerActive;
gameBGStyle = styles.bgActive;
bigButtonStyle = styles.bigButtonActive;
thinButtonStyle = styles.thinButtonActive;
}
return(
<View style={gameBGStyle}>
<View style={gameInitStyle}>
<Button
style={bigButtonStyle}
textStyle={styles.buttonTextStyle}
onPress={this._navigate}>
START TEST
</Button>
<Button
style={thinButtonStyle}
textStyle={styles.buttonTextStyle}
onPress={this._back}>
BACK
</Button>
</View>
</View>
)
}
});
// Reaction test is live here.
var GameLive = React.createClass({
getInitialState(){
startTime = (new Date()).getTime();
return {
backgroundColor: colorControl.liveColor
}
},
_reactionHandler(){
var timeElapsed = (new Date()).getTime() - startTime;
this.props.navigator.replace({component: GameResult,
passProps: {timeElapsed: timeElapsed}});
},
render () {
return(
<View style={{flex: 1,
backgroundColor: this.state.backgroundColor,
justifyContent: 'center',
alignItems: 'center'}}>
<TouchableWithoutFeedback
onPress={() => this._reactionHandler()}>
<View style={{flex: 1,
justifyContent: 'center',
alignItems: 'center'}}>
<Text style={{height: 1000, width: 1000}}>Click!</Text>
</View>
</TouchableWithoutFeedback>
</View>
)
}
});
var GameResult = React.createClass({
save (time) {
// Passing arg to new Date() spoofs a date. For testing purposes only.
var dateOfResult = new Date(1468814400000);
/* Normalize dates; collapse same-day entries into single entry
* representing the 'high-score' for that day. */
const months = ['January, ', 'February, ', 'March, ', 'April, ', 'May, ',
'June, ', 'July, ', 'August, ', 'September, ', 'October, ', 'November, ',
'December, '];
var normalizedDOR = String(Date.parse(months[dateOfResult.getMonth()]
+ String(dateOfResult.getDate()) + " "
+ String(dateOfResult.getFullYear())));
AsyncStorage.getItem(normalizedDOR, (error, result) => {
if (error) { console.warn(error) }
else {
if (!result) {
console.warn("No result for this date found, logging time of "
+ String(time) + ", " + normalizedDOR + " ms since that"
+ " arbitrary time in 1970.");
AsyncStorage.setItem(normalizedDOR, String(time), (error) => {
if (error) { console.warn(error) }
});
} else {
var newResult = (Number(result) < time) ? result : String(time);
if (newResult == time) {
console.warn("Overwriting previous result with "
+ String(newResult) + ", " + normalizedDOR + " ms "
+ "since that arbitrary time in 1970.");
} else { console.warn("Did not beat record"); }
AsyncStorage.setItem(normalizedDOR, newResult, (error) => {
if (error) { console.warn(error) }
});
}
}
});
},
_done() {
// Reset testState and go back to GameInit
testState = 0;
if (this.props.timeElapsed){
this.save(this.props.timeElapsed);
}
this.props.navigator.push({component: GameInit});
},
render(){
if (this.props.timeElapsed){
return(
<TouchableWithoutFeedback
onPress={() => this._done()}>
<View style={styles.bg}>
<View style={styles.container}>
<Text>{this.props.timeElapsed} ms</Text>
</View>
</View>
</TouchableWithoutFeedback>
)
} else {
// timeElapsed will be 0 if user makes an input too early.
return(
<TouchableWithoutFeedback
onPress={() => this._done()}>
<View style={styles.bg}>
<View style={styles.container}>
<Text>You cheater!</Text>
</View>
</View>
</TouchableWithoutFeedback>
)
}
}
});
AppRegistry.registerComponent('ReactionTraining', () => ReactionTraining);