-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortBreak.js
134 lines (109 loc) · 3.68 KB
/
ShortBreak.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
import React from 'react';
import {View,Text,StyleSheet,TouchableHighlight} from 'react-native';
import moment from 'moment';
export default class App extends React.Component{
state = {
TempMinutes:5,
TempSeconds:0,
countDown:moment.duration().add({minutes:5,seconds:0}),
running: false,
tempCount: false
};
Timer = null
startStopTimer = () => {
console.log(this.state.running)
if (this.state.tempCount == false){
this.setState({
tempCount:true})
}
//stop button is pressed; if running is true
if (this.state.running){
console.log("Stopped")
clearInterval(this.Timer)
this.setState({running:!this.state.running})
return;
} else{
console.log("Running....")
this.setState({running:!this.state.running})
console.log(this.state.countDown)
this.Timer = setInterval(() => {
if (this.state.countDown <= 0){ //timer runs out
clearInterval(this.Timer)
Alert.alert("Time is up")
} else {
this.state.countDown = this.state.countDown.subtract(1,'s')
const hours = this.state.countDown.hours()
const minutes = this.state.countDown.minutes()
const seconds = this.state.countDown.seconds()
this.setState({
TempHours: hours, TempMinutes: minutes, TempSeconds:seconds})}
},1000)}}
resetTimer = () =>{
if (!this.state.running){
console.log("Reset")
clearInterval(this.Timer)
this.setState({TempMinutes:5,TempSeconds:0,countDown:moment.duration().add({minutes:5,seconds:0}),tempCount:false})
}}
render(){
return(
<View style = {styles.containerStyle}>
<Text style = {styles.timerStyle}> {` ${this.state.TempMinutes} : ${this.state.TempSeconds}`} </Text>
<View style = {styles.buttonRowStyle}>
<TouchableHighlight
onPress = {this.resetTimer}
style = {styles.resetButtonStyle}
underlayColor='#fff'
>
<Text style = {styles.textStyle}> Reset </Text>
</TouchableHighlight>
<TouchableHighlight onPress = {this.startStopTimer} style = {styles.startButtonStyle}>
<Text style = {styles.textStyle}> {this.state.running? 'Stop':'Start'} </Text>
</TouchableHighlight>
</View>
</View>
)}}
const styles = StyleSheet.create({
containerStyle:{
flexDirection: 'column',
justifyContent:'space-around',
paddingBottom: 200,
alignItems: 'center',
backgroundColor: 'white',
flex: 1
},
timerStyle:{
padding: 30,
fontSize: 50,
justifyContent: 'center',
alignItems: 'center',
},
resetButtonStyle:{
height: 80,
width: 80,
borderRadius:40,
borderWidth: 1,
borderColor: '#f2a5a5',
alignItems:'center',
justifyContent: 'center',
marginRight:20
},
startButtonStyle:{
height: 80,
width: 80,
borderRadius:40,
borderWidth: 1,
borderColor: '#f2a5a5',
alignItems:'center',
justifyContent: 'center',
marginLeft:20
},
buttonRowStyle: {
paddingTop: 50,
flexDirection: 'row',
alignContent: 'flex-end',
justifyContent: 'center'
},
textStyle: {
fontWeight: 'bold'
}
})