diff --git a/App.tsx b/App.tsx index 0329d0c..0466b55 100644 --- a/App.tsx +++ b/App.tsx @@ -1,20 +1,65 @@ -import { StatusBar } from 'expo-status-bar'; -import { StyleSheet, Text, View } from 'react-native'; +import React, { useState, useEffect } from 'react'; +import { View, StyleSheet } from 'react-native'; +import StopWatch from './StopWatch'; +import StopWatchButton from './StopWatchButton'; +// The main App component export default function App() { - return ( - - Open up App.tsx to start working on your app! - - - ); + // State for tracking time + const [time, setTime] = useState(0); + // State for tracking whether the stopwatch is running + const [running, setRunning] = useState(false); + // State for storing lap times + const [laps, setLaps] = useState([]); + + // Timer logic to update the time every second when running + useEffect(() => { + let interval: ReturnType | null = null; + if (running) { + interval = setInterval(() => { + setTime(prevTime => prevTime + 1); + }, 1000); + } + return () => { + // Clear the interval when the component is unmounted or running state changes + if (interval) clearInterval(interval); + }; + }, [running]); + + // Function to start the stopwatch + const handleStart = () => setRunning(true); + // Function to stop the stopwatch + const handleStop = () => setRunning(false); + // Function to reset the stopwatch + const handleReset = () => { + setTime(0); + setLaps([]); + setRunning(false); + }; + // Function to record a lap time + const handleLap = () => setLaps(prevLaps => [...prevLaps, time]); + + return ( + + // Stopwatch display component + + // Buttons for controlling the stopwatch + + + ); } +// Styles for the App component const styles = StyleSheet.create({ container: { - flex: 1, - backgroundColor: '#fff', - alignItems: 'center', - justifyContent: 'center', - }, + flex: 1, // Take up the entire screen + justifyContent: 'center', // Center content vertically + alignItems: 'center', // Center content horizontally + backgroundColor: '#F0F0F0', // Light grey background + } }); diff --git a/package.json b/package.json index bf0f5a3..c7fd90e 100644 --- a/package.json +++ b/package.json @@ -12,10 +12,12 @@ "dependencies": { "expo": "~49.0.15", "expo-status-bar": "~1.6.0", + "jest": "^29.2.1", + "jest-expo": "~49.0.0", "react": "18.2.0", "react-native": "0.72.6", - "jest-expo": "~49.0.0", - "jest": "^29.2.1" + "react-native-web": "~0.19.6", + "react-dom": "18.2.0" }, "devDependencies": { "@babel/core": "^7.20.0", @@ -23,6 +25,7 @@ "@tsconfig/react-native": "^3.0.2", "@types/jest": "^29.5.11", "@types/react": "~18.2.14", + "@types/react-native": "^0.73.0", "@types/react-test-renderer": "^18.0.7", "typescript": "^5.3.3" }, diff --git a/src/StopWatch.tsx b/src/StopWatch.tsx index 5c7eb74..d8eb32e 100644 --- a/src/StopWatch.tsx +++ b/src/StopWatch.tsx @@ -1,8 +1,31 @@ -import { View } from 'react-native'; - -export default function StopWatch() { - return ( - - - ); -} \ No newline at end of file +import React from 'react'; +import { View, Text, StyleSheet } from 'react-native'; + +// Define the props interface for the StopWatch component +interface StopWatchProps { + time: number; // Represents the total time in seconds + laps: number[]; // Array of lap times in seconds +} + +// The StopWatch functional component +export default function StopWatch({ time, laps }: StopWatchProps) { + return ( + + // Display the total time + {time}s + + // Map through the laps array and render each lap time + {laps.map((lap, index) => ( + Lap {index + 1}: {lap}s + ))} + + ); +} + +// StyleSheet for the component +const styles = StyleSheet.create({ + text: { + fontSize: 18, // Set font size to 18 for better readability + color: '#333', // Dark grey color for the text + } +}); diff --git a/src/StopWatchButton.tsx b/src/StopWatchButton.tsx index 8768555..633388b 100644 --- a/src/StopWatchButton.tsx +++ b/src/StopWatchButton.tsx @@ -1,8 +1,50 @@ -import { View } from 'react-native'; +import React from 'react'; +import { View, TouchableOpacity, Text, StyleSheet } from 'react-native'; -export default function StopWatchButton() { +// Define the props interface for the StopWatchButton component +interface StopWatchButtonProps { + onStart: () => void; // Function to be called when the Start button is pressed + onStop: () => void; // Function to be called when the Stop button is pressed + onReset: () => void; // Function to be called when the Reset button is pressed + onLap: () => void; // Function to be called when the Lap button is pressed +} + +// The StopWatchButton functional component +export default function StopWatchButton({ onStart, onStop, onReset, onLap }: StopWatchButtonProps) { return ( - + + // Start button with an onPress event to trigger onStart + + Start + + + // Stop button with an onPress event to trigger onStop + + Stop + + + // Reset button with an onPress event to trigger onReset + + Reset + + + // Lap button with an onPress event to trigger onLap + + Lap + ); -} \ No newline at end of file +} + +// StyleSheet for the component +const styles = StyleSheet.create({ + button: { + margin: 5, // Margin around each button + borderWidth: 1, // Border width of 1 for the button + borderColor: '#333', // Border color set to a dark grey + padding: 10, // Padding inside the button for better touch area + alignItems: 'center', // Align text to the center of the button + backgroundColor: '#A0A0A0' // Background color of the button + }, + // Add other styles as needed +});