-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.js
More file actions
140 lines (129 loc) · 3.57 KB
/
Timer.js
File metadata and controls
140 lines (129 loc) · 3.57 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
import React from 'react';
import { View, Text, StyleSheet, TouchableOpacity, Dimensions } from 'react-native';
export default class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { total: 90, seconds: 90, running: false };
this.styles = this.createCss();
}
tick() {
if (this.state.seconds && this.state.running) {
this.setState(state => ({
seconds: state.seconds - 1
}));
}
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
createCss() {
let h = Dimensions.get('window').height
return StyleSheet.create({
timerbar: {
// borderWidth: 2,
// color: "#fff",
// alignContent: "center",
// justifyContent: "flex-end",
flex: 0.3,
backgroundColor: "grey",
// borderRadius: 1,
flexDirection: "row",
},
timerexpire: {
borderWidth: 2,
// color: "#fff",
alignContent: "center",
justifyContent: "center",
flex: 0.3,
backgroundColor: "gold",
borderRadius: 1,
},
detail: {
fontSize: h * 0.15,
color: "white",
fontWeight: "bold",
textAlign: "center",
justifyContent: "center",
},
timecontainer: {
flex: 5,
// borderWidth: 1,
// borderColor: "yellow",
},
buttoncontainer: {
flex: 1,
// width: "12%",
// height:"100%",
// padding: "30",
flexDirection: "column",
justifyContent: "center",
// alignItems:"center",
alignContent: "space-around",
// borderWidth: 1,
// borderColor:"red",
// borderStyle:"dashed"
},
button: {
fontSize: h * 0.048,
fontWeight: "bold",
// flex: 1,
// margin: "25",
color: "black",
// width: "50%",
// height: "30%",
// borderStyle:"solid",
// borderWidth:1,
// borderColor:"orange",
textAlign: "center",
}
})
};
toggleClock = () => {
if (this.state.running) {
this.setState({ running: false })
} else { this.setState({ running: true }) }
}
doReset = () => {
if (this.state.running == false) this.setState({ seconds: this.state.total })
}
timeFormat = () => {
let minutes, seconds = 0;
minutes = Math.floor(this.state.seconds / 60)
seconds = this.state.seconds % 60
let minstr = "0" + minutes
let secstr = seconds > 9 ? "" + seconds : "0" + seconds
return minstr + ":" + secstr
}
render() {
if (this.state.seconds > 0) {
return (
<View style={this.styles.timerbar}>
<View style={this.styles.buttoncontainer}>
<TouchableOpacity><Text style={this.styles.button}>Adjust</Text></TouchableOpacity>
</View>
<View style={this.styles.timecontainer}>
<TouchableOpacity onPress={this.toggleClock}>
<Text style={this.styles.detail}>
{this.timeFormat()}
</Text>
</TouchableOpacity>
</View>
<View style={this.styles.buttoncontainer}>
<TouchableOpacity onPress={this.doReset}><Text style={this.styles.button}>Reset</Text></TouchableOpacity>
</View>
</View>
);
} else {
return (
<View style={this.styles.timerexpire}>
<Text style={this.styles.detail}>
Seconds: {this.state.seconds}
</Text>
</View>
);
}
}
}