-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
166 lines (162 loc) · 3.74 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TouchableHighlight,
View
} from 'react-native';
import formatTime from 'minutes-seconds-milliseconds';
var StopWatch = React.createClass({
getInitialState:function () {
return {
timeElapsed: null,
running: false,
startTime: null,
laps: []
}
},
render: function () {
return (
<View style={styles.container}>
<View style={[styles.header]}>
<View style={[styles.timmerWrapper]}>
<Text style={styles.timer}>
{formatTime(this.state.timeElapsed)}
</Text>
</View>
<View style={[styles.buttonWrapper]}>
{this.startStopButton()}
{this.lapButton()}
</View>
</View>
<View style={[styles.footer]}>
{this.laps()}
</View>
</View>
);
},
laps: function () {
return this.state.laps.map(function (time,index) {
// Each child in an array or iterator should have a unique "key"
//key={index}
return(
<View key={index} style={styles.lap}>
<Text style={styles.lapText}>
Lap #{index+1}
</Text>
<Text style={styles.lapText}>
{formatTime(time)}
</Text>
</View>
);
});
},
startStopButton: function () {
var style = this.state.running ? styles.stopButton : styles.startButton;
return (
<TouchableHighlight
underlayColor="gray"
style={[styles.button,style]}
onPress={() => this.handleStartPress()}>
<Text>
{this.state.running ? 'Stop' : 'Start'}
</Text>
</TouchableHighlight>
);
},
lapButton: function () {
return (
<TouchableHighlight
style={styles.button}
underlayColor="gray"
onPress={() => this.handleLapPress()}
>
<Text>
Lap
</Text>
</TouchableHighlight>
);
},
handleLapPress:function () {
var lap = this.state.timeElapsed;
this.setState({
startTime: new Date(),
laps: this.state.laps.concat([lap])
});
},
handleStartPress: function () {
console.log('Start was pressed!');
if(this.state.running){
clearInterval(this.interval);
this.setState({running:false});
return
}
var startTime = new Date();
this.setState({startTime:new Date()});
// Never do!!! in react
// this.state.timeElapsed = new Date()
// update our state with some new value and this
// Cause our view to re-render
this.interval = setInterval(
() => {
this.setState({
timeElapsed: new Date() - this.state.startTime,
running:true
});
} , 30);
}
});
var styles = StyleSheet.create({
container:{
flex:1 , // fill the entire the screen
alignItems: 'stretch'
},
header:{ //yellow
flex:1
},
footer:{ //blue
flex:1
},
timmerWrapper:{ //Red
flex:5, //takes up 5/8ths (total = 5+3)of the available space
justifyContent: 'center',
alignItems:'center'
},
buttonWrapper:{ //Green
flex:3, //takes up 3/8ths of the available space
flexDirection:'row',
justifyContent: 'space-around',
alignItems:'center'
},
timer:{
fontSize: 60
},
button:{
borderWidth:2,
height:90,
width:90,
borderRadius:50,
justifyContent:'center',
alignItems:'center'
},
startButton:{
borderColor: '#00CC00'
},
stopButton:{
borderColor: '#CC0000'
},
lap:{
justifyContent:'space-around',
flexDirection:'row'
},
lapText:{
fontSize: 30
}
})
AppRegistry.registerComponent('stopwatch', () => StopWatch);