-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
147 lines (137 loc) · 3.95 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
import React, { Component } from 'react';
import { StyleSheet, Text, View, Picker, Button } from 'react-native';
import BackgroundTimer from 'react-native-background-timer';
export default class App extends Component {
intervalId = 0;
timeoutId = 0;
state = { timer: 0, timerStarted: false, timerFunction: "0" };
startTimeout = () => {
// Start a timer that runs once after X milliseconds
timeoutId = BackgroundTimer.setTimeout(() => {
this.updateTimer();
// this will be executed once after 5 seconds
// even when app is the the background
console.log('tac');
}, 5000);
}
stopTimeout = () => {
BackgroundTimer.clearTimeout(timeoutId);
this.setState({ timer: 0 });
}
startInterval = () => {
intervalId = BackgroundTimer.setInterval(() => {
this.updateTimer();
// this will be executed each second
// even when app is the the background
console.log('tic');
}, 1000);
}
stopInterval = () => {
BackgroundTimer.clearInterval(intervalId);
this.setState({ timer: 0 });
}
updateTimer = () => {
const timer = this.state.timer;
this.setState({ timer: timer + 1 });
}
onStart = () => {
const { timerStarted, timerFunction } = this.state;
if(!timerStarted){
this.setState({ timerStarted: true });
if(timerFunction === "1"){
this.startTimeout();
} else if(timerFunction === "2"){
this.startInterval();
}
}
}
onCancel = () => {
const { timerStarted, timerFunction } = this.state;
if(timerStarted){
this.setState({ timerStarted: false });
if(timerFunction === "1"){
this.stopTimeout();
} else if(timerFunction === "2"){
this.stopInterval();
}
}
}
render() {
let description = "select a timer."
if(this.state.timerFunction === "1"){
description = "In Timeout timer value will be updated after 5 seconds.";
} else if (this.state.timerFunction === "2"){
description = "In Interval timer value will be updated each seconds.";
}
return (
<View style={styles.container}>
<View style={styles.timerView}>
<Text style={styles.timerText}>
{this.state.timer}
</Text>
</View>
<View style={styles.timerFunctionView}>
<Picker
style={styles.timerFunctionPicker}
enabled={this.state.timerStarted ? false : true}
selectedValue={this.state.timerFunction}
onValueChange={(itemValue, itemIndex) => this.setState({timerFunction: itemValue})}>
<Picker.Item label="Select Timer Function" value = "0" />
<Picker.Item label="Timeout" value = "1" />
<Picker.Item label="Interval" value = "2" />
</Picker>
</View>
<View style={styles.startTimerButtonView}>
<Button
disabled = {(this.state.timerFunction === "1" || this.state.timerFunction === "2") ? false : true }
onPress = {this.state.timerStarted ? this.onCancel : this.onStart}
title={this.state.timerStarted ? "Cancel" : "Start" }
color='#841584'
/>
</View>
<View style={styles.descriptionView}>
<Text style={styles.descriptionText}>
{description}
</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
timerView: {
margin: 20,
height: 100,
width: 100,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red'
},
timerText: {
fontSize: 50,
color: '#ccc'
},
timerFunctionView: {
backgroundColor: 'lightgreen'
},
timerFunctionPicker: {
height: 50,
width: 250
},
startTimerButtonView: {
height: 50,
width: 250,
},
descriptionView: {
width: 250
},
descriptionText: {
color: 'black'
}
});