-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
92 lines (79 loc) · 1.97 KB
/
App.js
File metadata and controls
92 lines (79 loc) · 1.97 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
import React, { useState, useRef } from 'react';
import { View, Text, Button, FlatList, StyleSheet } from 'react-native';
const StopwatchApp = () => {
const [timer, setTimer] = useState(0);
const [isRunning, setIsRunning] = useState(false);
const [records, setRecords] = useState([]);
const intervalRef = useRef();
const startTimer = () => {
setIsRunning(true);
intervalRef.current = setInterval(() => {
setTimer((prevTimer) => prevTimer + 1);
}, 1000);
};
const stopTimer = () => {
setIsRunning(false);
clearInterval(intervalRef.current);
};
const resetTimer = () => {
setTimer(0);
setRecords([]);
};
const formatTime = (time) => {
const pad = (num) => (num < 10 ? '0' + num : num);
const hours = Math.floor(time / 3600);
const minutes = Math.floor((time % 3600) / 60);
const seconds = time % 60;
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
};
return (
<View style={styles.container}>
<Text style={styles.timer}>{formatTime(timer)}</Text>
<View style={styles.buttonContainer}>
<Button
title={isRunning ? 'Stop' : 'Start'}
onPress={isRunning ? stopTimer : startTimer}
color="orange"
height="5"
/>
<Button
title="Reset"
onPress={resetTimer}
color="orange"
/>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: "center",
backgroundColor: 'black',
},
wrapper: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
timerContainer: {
marginBottom: 20,
},
timer: {
fontSize: 70,
color: 'white',
alignItems: 'center',
top: 10,
justifyContent: 'center',
},
buttonContainer: {
flexDirection: 'row',
alignItems: 'center',
top: 50,
height: 50,
justifyContent: 'center',
marginBottom: 20,
},
});
export default StopwatchApp;