|
1 |
| -import React, { useContext, useState } from "react"; |
2 |
| -import { View, Text, TextInput, TouchableOpacity } from "react-native"; |
3 | 1 | import FontAwesome from "@expo/vector-icons/FontAwesome";
|
4 |
| -import { TimerContext } from "../context/TimerContext"; |
| 2 | +import { useContext, useState } from "react"; |
| 3 | +import { Text, TextInput, TouchableOpacity, View } from "react-native"; |
| 4 | +import { TimerContext } from "../context/TimerContext"; |
5 | 5 |
|
6 | 6 | const Timer = () => {
|
7 |
| - const { |
8 |
| - timeLeft, |
9 |
| - isRunning, |
10 |
| - isPaused, |
11 |
| - startTimer, |
12 |
| - pauseTimer, |
13 |
| - resumeTimer, |
14 |
| - resetTimer, |
15 |
| - } = useContext(TimerContext); |
| 7 | + const { |
| 8 | + timeLeft, |
| 9 | + isRunning, |
| 10 | + isPaused, |
| 11 | + startTimer, |
| 12 | + pauseTimer, |
| 13 | + resumeTimer, |
| 14 | + resetTimer, |
| 15 | + } = useContext(TimerContext); |
16 | 16 |
|
17 |
| - const [inputHours, setInputHours] = useState(""); |
18 |
| - const [inputMinutes, setInputMinutes] = useState(""); |
19 |
| - const [inputSeconds, setInputSeconds] = useState(""); |
| 17 | + const [inputHours, setInputHours] = useState(""); |
| 18 | + const [inputMinutes, setInputMinutes] = useState("10"); |
| 19 | + const [inputSeconds, setInputSeconds] = useState(""); |
20 | 20 |
|
21 |
| - const formatTime = (time: number) => { |
22 |
| - const hours = Math.floor(time / 3600); |
23 |
| - const minutes = Math.floor((time % 3600) / 60); |
24 |
| - const seconds = time % 60; |
25 |
| - return { |
26 |
| - hours: String(hours).padStart(2, "0"), |
27 |
| - minutes: String(minutes).padStart(2, "0"), |
28 |
| - seconds: String(seconds).padStart(2, "0"), |
| 21 | + const formatTime = (time: number) => { |
| 22 | + const hours = Math.floor(time / 3600); |
| 23 | + const minutes = Math.floor((time % 3600) / 60); |
| 24 | + const seconds = time % 60; |
| 25 | + return { |
| 26 | + hours: String(hours).padStart(2, "0"), |
| 27 | + minutes: String(minutes).padStart(2, "0"), |
| 28 | + seconds: String(seconds).padStart(2, "0"), |
| 29 | + }; |
29 | 30 | };
|
30 |
| - }; |
31 | 31 |
|
32 |
| - const { hours, minutes, seconds } = formatTime(timeLeft); |
| 32 | + const { hours, minutes, seconds } = formatTime(timeLeft); |
33 | 33 |
|
34 |
| - return ( |
35 |
| - <View className="flex-1 items-center justify-center bg-gray-100 mb-2"> |
36 |
| - <View className="bg-white justify-center items-center shadow-md rounded-lg w-[1000px] min-h-[323px] p-4"> |
37 |
| - <View className="flex-row space-x-4"> |
38 |
| - {/* Hours Input */} |
39 |
| - <View className="bg-[rgba(16,83,102,0.6)] w-[195px] h-[195px] rounded-lg flex justify-center items-center"> |
40 |
| - <TextInput |
41 |
| - style={{ fontSize: 70, fontWeight: 'bold', color: 'white', textAlign: 'center', width: 100 }} |
42 |
| - value={isRunning || isPaused ? hours : inputHours} |
43 |
| - onChangeText={(text) => !isRunning && setInputHours(text)} |
44 |
| - keyboardType="numeric" |
45 |
| - maxLength={2} |
46 |
| - placeholder="00" |
47 |
| - editable={!isRunning} |
48 |
| - /> |
49 |
| - </View> |
| 34 | + return ( |
| 35 | + <View className="flex-1 items-center justify-center bg-gray-100 mb-2"> |
| 36 | + <View className="bg-white justify-center items-center shadow-md rounded-lg w-[1000px] min-h-[323px] p-4"> |
| 37 | + <View className="flex-row space-x-4"> |
| 38 | + {/* Hours Input */} |
| 39 | + <View className="bg-[rgba(16,83,102,0.6)] w-[195px] h-[195px] rounded-lg flex justify-center items-center"> |
| 40 | + <TextInput |
| 41 | + style={{ |
| 42 | + fontSize: 70, |
| 43 | + fontWeight: "bold", |
| 44 | + color: "white", |
| 45 | + textAlign: "center", |
| 46 | + width: 100, |
| 47 | + }} |
| 48 | + value={isRunning || isPaused ? hours : inputHours} |
| 49 | + onChangeText={(text) => |
| 50 | + !isRunning && setInputHours(text) |
| 51 | + } |
| 52 | + keyboardType="numeric" |
| 53 | + maxLength={2} |
| 54 | + placeholder="00" |
| 55 | + editable={!isRunning} |
| 56 | + /> |
| 57 | + </View> |
50 | 58 |
|
51 |
| - {/* Minutes Separator */} |
52 |
| - <View className="items-center justify-center"> |
53 |
| - <Text className="text-[20px] font-bold text-black">:</Text> |
54 |
| - </View> |
| 59 | + {/* Minutes Separator */} |
| 60 | + <View className="items-center justify-center"> |
| 61 | + <Text className="text-[20px] font-bold text-black"> |
| 62 | + : |
| 63 | + </Text> |
| 64 | + </View> |
55 | 65 |
|
56 |
| - {/* Minutes Input */} |
57 |
| - <View className="bg-[rgba(16,83,102,0.6)] w-[195px] h-[195px] rounded-lg flex justify-center items-center"> |
58 |
| - <TextInput |
59 |
| - style={{ fontSize: 70, fontWeight: 'bold', color: 'white', textAlign: 'center', width: 100 }} |
60 |
| - value={isRunning || isPaused ? minutes : inputMinutes} |
61 |
| - onChangeText={(text) => !isRunning && setInputMinutes(text)} |
62 |
| - keyboardType="numeric" |
63 |
| - maxLength={2} |
64 |
| - placeholder="00" |
65 |
| - editable={!isRunning} |
66 |
| - /> |
67 |
| - </View> |
| 66 | + {/* Minutes Input */} |
| 67 | + <View className="bg-[rgba(16,83,102,0.6)] w-[195px] h-[195px] rounded-lg flex justify-center items-center"> |
| 68 | + <TextInput |
| 69 | + style={{ |
| 70 | + fontSize: 70, |
| 71 | + fontWeight: "bold", |
| 72 | + color: "white", |
| 73 | + textAlign: "center", |
| 74 | + width: 100, |
| 75 | + }} |
| 76 | + value={ |
| 77 | + isRunning || isPaused ? minutes : inputMinutes |
| 78 | + } |
| 79 | + onChangeText={(text) => |
| 80 | + !isRunning && setInputMinutes(text) |
| 81 | + } |
| 82 | + keyboardType="numeric" |
| 83 | + maxLength={2} |
| 84 | + placeholder="00" |
| 85 | + editable={!isRunning} |
| 86 | + /> |
| 87 | + </View> |
68 | 88 |
|
69 |
| - {/* Seconds Separator */} |
70 |
| - <View className="items-center justify-center"> |
71 |
| - <Text className="text-[20px] font-bold text-black">:</Text> |
72 |
| - </View> |
| 89 | + {/* Seconds Separator */} |
| 90 | + <View className="items-center justify-center"> |
| 91 | + <Text className="text-[20px] font-bold text-black"> |
| 92 | + : |
| 93 | + </Text> |
| 94 | + </View> |
73 | 95 |
|
74 |
| - <View className="bg-[rgba(16,83,102,0.6)] w-[195px] h-[195px] rounded-lg flex justify-center items-center"> |
75 |
| - <TextInput |
76 |
| - style={{ fontSize: 70, fontWeight: 'bold', color: 'white', textAlign: 'center', width: 100 }} |
77 |
| - value={isRunning || isPaused ? seconds : inputSeconds} |
78 |
| - onChangeText={(text) => !isRunning && setInputSeconds(text)} |
79 |
| - keyboardType="numeric" |
80 |
| - maxLength={2} |
81 |
| - placeholder="00" |
82 |
| - editable={!isRunning} |
83 |
| - /> |
84 |
| - </View> |
85 |
| - </View> |
86 |
| - </View> |
| 96 | + <View className="bg-[rgba(16,83,102,0.6)] w-[195px] h-[195px] rounded-lg flex justify-center items-center"> |
| 97 | + <TextInput |
| 98 | + style={{ |
| 99 | + fontSize: 70, |
| 100 | + fontWeight: "bold", |
| 101 | + color: "white", |
| 102 | + textAlign: "center", |
| 103 | + width: 100, |
| 104 | + }} |
| 105 | + value={ |
| 106 | + isRunning || isPaused ? seconds : inputSeconds |
| 107 | + } |
| 108 | + onChangeText={(text) => |
| 109 | + !isRunning && setInputSeconds(text) |
| 110 | + } |
| 111 | + keyboardType="numeric" |
| 112 | + maxLength={2} |
| 113 | + placeholder="00" |
| 114 | + editable={!isRunning} |
| 115 | + /> |
| 116 | + </View> |
| 117 | + </View> |
| 118 | + </View> |
87 | 119 |
|
88 |
| - <View className="flex flex-row gap-4 mt-4"> |
89 |
| - {isRunning || isPaused ? ( |
90 |
| - <TouchableOpacity className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" onPress={resetTimer}> |
91 |
| - <FontAwesome name="rotate-left" size={12} color="white" /> |
92 |
| - <Text className="text-white font-bold p-1">Reset</Text> |
93 |
| - </TouchableOpacity> |
94 |
| - ) : ( |
95 |
| - <TouchableOpacity |
96 |
| - className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" |
97 |
| - onPress={() => |
98 |
| - startTimer( |
99 |
| - parseInt(inputHours, 10) || 0, |
100 |
| - parseInt(inputMinutes, 10) || 0, |
101 |
| - parseInt(inputSeconds, 10) || 0 |
102 |
| - ) |
103 |
| - } |
104 |
| - > |
105 |
| - <FontAwesome name="play" size={12} color="white" /> |
106 |
| - <Text className="text-white font-bold p-1">Start</Text> |
107 |
| - </TouchableOpacity> |
108 |
| - )} |
| 120 | + <View className="flex flex-row gap-4 mt-4"> |
| 121 | + {isRunning || isPaused ? ( |
| 122 | + <TouchableOpacity |
| 123 | + className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" |
| 124 | + onPress={resetTimer} |
| 125 | + > |
| 126 | + <FontAwesome |
| 127 | + name="rotate-left" |
| 128 | + size={12} |
| 129 | + color="white" |
| 130 | + /> |
| 131 | + <Text className="text-white font-bold p-1">Reset</Text> |
| 132 | + </TouchableOpacity> |
| 133 | + ) : ( |
| 134 | + <TouchableOpacity |
| 135 | + className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" |
| 136 | + onPress={() => |
| 137 | + startTimer( |
| 138 | + parseInt(inputHours, 10) || 0, |
| 139 | + parseInt(inputMinutes, 10) || 0, |
| 140 | + parseInt(inputSeconds, 10) || 0 |
| 141 | + ) |
| 142 | + } |
| 143 | + > |
| 144 | + <FontAwesome name="play" size={12} color="white" /> |
| 145 | + <Text className="text-white font-bold p-1">Start</Text> |
| 146 | + </TouchableOpacity> |
| 147 | + )} |
109 | 148 |
|
110 |
| - {/* Can only pause when running */} |
111 |
| - {isRunning && !isPaused && ( |
112 |
| - <TouchableOpacity className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" onPress={pauseTimer}> |
113 |
| - <FontAwesome name="pause" size={12} color="white" /> |
114 |
| - <Text className="text-white font-bold p-1">Pause</Text> |
115 |
| - </TouchableOpacity> |
116 |
| - )} |
| 149 | + {/* Can only pause when running */} |
| 150 | + {isRunning && !isPaused && ( |
| 151 | + <TouchableOpacity |
| 152 | + className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" |
| 153 | + onPress={pauseTimer} |
| 154 | + > |
| 155 | + <FontAwesome name="pause" size={12} color="white" /> |
| 156 | + <Text className="text-white font-bold p-1">Pause</Text> |
| 157 | + </TouchableOpacity> |
| 158 | + )} |
117 | 159 |
|
118 |
| - {/* Can only resume when paused */} |
119 |
| - {isPaused && ( |
120 |
| - <TouchableOpacity className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" onPress={resumeTimer}> |
121 |
| - <FontAwesome name="play" size={12} color="white" /> |
122 |
| - <Text className="text-white font-bold p-1">Resume</Text> |
123 |
| - </TouchableOpacity> |
124 |
| - )} |
125 |
| - </View> |
126 |
| - </View> |
127 |
| - ); |
| 160 | + {/* Can only resume when paused */} |
| 161 | + {isPaused && ( |
| 162 | + <TouchableOpacity |
| 163 | + className="bg-[#105366] shadow-md items-center justify-center w-[104px] h-[41px] p-2 gap-2 rounded flex-row" |
| 164 | + onPress={resumeTimer} |
| 165 | + > |
| 166 | + <FontAwesome name="play" size={12} color="white" /> |
| 167 | + <Text className="text-white font-bold p-1">Resume</Text> |
| 168 | + </TouchableOpacity> |
| 169 | + )} |
| 170 | + </View> |
| 171 | + </View> |
| 172 | + ); |
128 | 173 | };
|
129 | 174 |
|
130 | 175 | export default Timer;
|
0 commit comments