diff --git a/App.tsx b/App.tsx index 7b8b61b..3289028 100644 --- a/App.tsx +++ b/App.tsx @@ -1,25 +1,29 @@ - import React, { useState, useEffect } from 'react'; -import { View, StyleSheet } from 'react-native'; +import { View, Text, StyleSheet } from 'react-native'; import Stopwatch from './src/StopWatch'; import StopwatchButton from './src/StopWatchButton'; export default function App() { + // State for tracking time, laps, and whether the stopwatch is running const [time, setTime] = useState(0); - const [laps, setLaps] = useState([]); // Explicitly typing laps + const [laps, setLaps] = useState([]); const [isRunning, setIsRunning] = useState(false); + // Effect for handling the stopwatch timing useEffect(() => { - let interval: number | null = null; // Explicitly typing interval + let interval: number | null = null; + // Set an interval when the stopwatch is running if (isRunning) { interval = setInterval(() => { - setTime(time => time + 1); - }, 1000) as number; // Casting to number + setTime(time => time + 10); // Increment time every 10 milliseconds + }, 10); } else if (interval) { + // Clear the interval if the stopwatch is stopped clearInterval(interval as number); } + // Cleanup function to clear the interval return () => { if (interval) { clearInterval(interval as number); @@ -27,10 +31,19 @@ export default function App() { }; }, [isRunning]); - return ( + + + {/* Stopwatch header */} + Stopwatch + + {/* Stopwatch display */} + + + + {/* Stopwatch control buttons */} setIsRunning(true)} @@ -40,8 +53,13 @@ export default function App() { setTime(0); setLaps([]); }} - onLap={() => setLaps(currentLaps => [...currentLaps, time])} + onLap={() => { + // Add the current time to the laps array + setLaps(currentLaps => [...currentLaps, time]); + }} /> + + ); } @@ -51,7 +69,16 @@ const styles = StyleSheet.create({ flex: 1, justifyContent: 'center', alignItems: 'center', + backgroundColor: '#F5F5F5', padding: 20, }, + spacer: { + flex: 1, + }, + header: { + fontSize: 24, + fontWeight: 'bold', + marginBottom: 20, + }, }); diff --git a/src/StopWatch.tsx b/src/StopWatch.tsx index e28b6b6..d42b165 100644 --- a/src/StopWatch.tsx +++ b/src/StopWatch.tsx @@ -1,61 +1,63 @@ -// import React, { useState, useEffect } from 'react'; -// import { View, Text } from 'react-native'; - -// export default function Stopwatch() { -// const [time, setTime] = useState(0); -// const [laps, setLaps] = useState([]); -// const [isRunning, setIsRunning] = useState(false); - -// useEffect(() => { -// // Use ReturnType to infer the type returned by setInterval -// let interval: ReturnType; -// if (isRunning) { -// interval = setInterval(() => { -// setTime(prevTime => prevTime + 1); -// }, 1000); -// } -// return () => clearInterval(interval as unknown as number); -// }, [isRunning]); - - - -// return ( -// -// {/* Display time and laps */} -// {time} -// {/* Display laps */} -// {/* Display buttons */} -// -// ); -// } -// src/Stopwatch.tsx import React from 'react'; -import { View, Text } from 'react-native'; +import { View, Text, ScrollView, StyleSheet } from 'react-native'; -// Define a type for the props +// Defining the types for the component props type StopwatchProps = { - time: number; - laps: number[]; + time: number; // Current time of the stopwatch + laps: number[]; // Array to store lap times }; const Stopwatch: React.FC = ({ time, laps }) => { - // Function to format time in seconds to MM:SS format + // Function to format time into a readable format (MM:SS.ms) const formatTime = (time: number): string => { - const minutes = Math.floor(time / 60); - const seconds = time % 60; - const miliseconds = - return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}:${milliseconds.toString().padStart(3, '0')}`; + const minutes = Math.floor(time / 60000); + const seconds = Math.floor((time % 60000) / 1000); + const milliseconds = Math.floor((time % 1000) / 10); + + return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}.${milliseconds.toString().padStart(2, '0')}`; }; return ( - - {formatTime(time)} - {laps.map((lap: number, index: number) => ( - Lap {index + 1}: {formatTime(lap)} - ))} + + {/* Displaying the formatted time */} + {formatTime(time)} + + {/* ScrollView to list lap times */} + + {laps.map((lap, index) => ( + + Lap {index + 1}: {formatTime(lap)} + + ))} + ); }; + +const styles = StyleSheet.create({ + container: { + flex: 1, + alignItems: 'center', + width: '100%', + padding: 20, + }, + timerDisplay: { + fontSize: 48, + fontWeight: 'bold', + marginBottom: 30, + }, + lapList: { + flex: 1, + width: '100%', + padding: 5, + }, + lapTime: { + fontSize: 16, + color: '#333', + marginBottom: 5, + }, +}); + export default Stopwatch; diff --git a/src/StopWatchButton.tsx b/src/StopWatchButton.tsx index a91bc44..6dac4ef 100644 --- a/src/StopWatchButton.tsx +++ b/src/StopWatchButton.tsx @@ -1,16 +1,7 @@ -// import { View } from 'react-native'; - -// export default function StopWatchButton() { -// return ( -// -// -// ); -// } -// src/StopwatchButton.tsx -// src/StopwatchButton.tsx import React from 'react'; -import { View, Button } from 'react-native'; +import { View, TouchableOpacity, Text, StyleSheet } from 'react-native'; +// Define the types for the component props type StopwatchButtonProps = { isRunning: boolean; onStart: () => void; @@ -19,19 +10,59 @@ type StopwatchButtonProps = { onLap: () => void; }; +// StopwatchButton component const StopwatchButton: React.FC = ({ isRunning, onStart, onStop, onReset, onLap }) => { return ( - - {isRunning ? ( -