-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
154 lines (135 loc) · 3.58 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
import formatTime from 'minutes-seconds-milliseconds';
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, TouchableHighlight} from 'react-native';
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
timeElpased: null,
running: false,
startTime: null,
laps:[]
}
}
render() {
return <View style={styles.container}>
<View style={[styles.header]}>
<View style={[styles.timerWrapper]}>
<Text style={styles.timer}>
{formatTime(this.state.timeElpased)}
</Text>
</View>
<View style={[styles.buttonWrapper]}>
{this.startStopButton()}
{this.lapButton()}
</View>
</View>
<View style={[styles.footer]}>
{this.laps()}
</View>
</View>
}
laps(){
return this.state.laps.map(function(time, index){
return <View style={styles.lap}>
<Text style={styles.lapText}>
Lap #{index + 1}
</Text>
<Text style={styles.lapText}>
{formatTime(time)}
</Text>
</View>
});
}
startStopButton(){
var style = this.state.running ? styles.stopButton : styles.startButton;
return <TouchableHighlight underlayColor="gray"
onPress={this.onStartStopButtonPressed}
style={[styles.button, style]}>
<Text>{this.state.running ? 'Stop' : 'Start'}</Text>
</TouchableHighlight>
}
lapButton(){
return <TouchableHighlight style={styles.button}
underlayColor="gray"
onPress={this.onLapButtonPressed}>
<Text>Lap</Text>
</TouchableHighlight>
}
onLapButtonPressed = () => {
var lap = this.state.timeElpased //value of the timer
this.setState({
startTime:new Date(),
laps: this.state.laps.concat([lap])
});
}
borderColor(color){
return {
borderColor: color,
borderWidth: 4
}
}
onStartStopButtonPressed = () => {
if(this.state.running){ //handling stop
clearInterval(this.interval);//clearing the state
this.setState({running: false}); //setting running to false by using setState method
return //return aka break the execution and return nothing
}
this.setState({startTime: new Date()}); //new date() method gets the current time in mili second
//new Date() will always start from 0
this.interval = setInterval(() => { //saving the state into interval
this.setState({
timeElpased: new Date() - this.state.startTime,
running: true
})
}, 30) // setInterval(function, time). This method sets the update interval to every 30 ms. where, function ()=>{} and time is 30 means 30 ms and 1000ms means 1 sec
//setState updates the state
}
}
const styles = StyleSheet.create({
container:{
flex:1,
alignItems:'stretch'
},
header:{
flex:1
},
footer:{
flex:1
},
timerWrapper:{
flex: 5,
justifyContent: 'center',
alignItems: 'center'
},
buttonWrapper:{
flex: 3,
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'
},
lap:{
justifyContent: 'space-around',
flexDirection: 'row'
},
lapText:{
fontSize: 30
}
});