-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ios.js
More file actions
172 lines (155 loc) · 3.46 KB
/
index.ios.js
File metadata and controls
172 lines (155 loc) · 3.46 KB
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
import formatTime from 'minutes-seconds-milliseconds';
import React, { Component } from 'react';
import {
AppRegistry,
Text,
View,
TouchableHighlight,
StyleSheet
} from 'react-native';
class StopWatch extends Component {
constructor(props) {
super(props);
this.state = {
timeElapsed: null,
running: false,
startTime: null,
laps: []
};
}
startStopButton() {
let style = this.state.running ? styles.stopButton : styles.startButton;
return (
<TouchableHighlight
underlayColor="gray"
onPress={this.handleStartPress}
style={[styles.button, style]}
>
<Text>
{this.state.running ? 'Stop' : 'Start'}
</Text>
</TouchableHighlight>
)
}
lapButton() {
let style = this.state.running ? styles.button : [styles.button, styles.lapButtonRestrict];
return (
<TouchableHighlight
style={style}
underlayColor="gray"
onPress={this.handleLapPress}
>
<Text>
Lap
</Text>
</TouchableHighlight>
)
}
handleLapPress = () => {
let lap = this.state.timeElapsed;
this.setState({
startTime: new Date(),
laps: [...this.state.laps, lap]
})
}
handleStartPress = () => {
if (this.state.running) {
clearInterval(this.interval);
this.setState({ running: false });
return
}
let startTime = new Date();
this.setState({ startTime: new Date() })
this.interval = setInterval(() => {
this.setState({
timeElapsed: new Date() - this.state.startTime,
running: true
});
}, 30);
}
laps() {
return this.state.laps.map((time, index) => {
return (
<View style={styles.lap}>
<Text style={styles.lapText}>
Lap # { index + 1 }
</Text>
<Text style={styles.lapText}>
{formatTime(time)}
</Text>
</View>
)
})
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<View style={styles.timerWrapper}>
<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>
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1, // Fill the entire screen
alignItems: 'stretch'
},
header: { // Yellow
flex: 1
},
footer: { // Blue
flex: 1
},
timerWrapper: { // Red
flex: 5,// Takes up 5/8ths 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: 100,
width: 100,
borderRadius: 50,
justifyContent: 'center',
alignItems: 'center'
},
startButton: {
borderColor: '#00CC00'
},
stopButton: {
borderColor: '#CC0000'
},
lapButtonRestrict: {
backgroundColor: 'gray'
},
lap: {
justifyContent: 'space-around',
flexDirection: 'row'
},
lapText: {
fontSize: 30
}
});
AppRegistry.registerComponent('stopwatch', () => StopWatch);