import React, { useState, useEffect, createContext, useContext } from 'react';
import { Text, View, StyleSheet, ActivityIndicator, TouchableOpacity, FlatList } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { CameraView, Camera } from 'expo-camera';
import { Ionicons } from '@expo/vector-icons';
// Create a Context for QR code data
const QRCodeContext = createContext();
const Tab = createBottomTabNavigator();
function QRScannerScreen() {
const { qrCodes, setQrCodes } = useContext(QRCodeContext); // Access context
const [hasPermission, setHasPermission] = useState(null);
const [scanned, setScanned] = useState(false);
const [showSplash, setShowSplash] = useState(true);
const [scannedData, setScannedData] = useState('');
useEffect(() => {
const initializeApp = async () => {
const { status } = await Camera.requestCameraPermissionsAsync();
setHasPermission(status === 'granted');
setShowSplash(false); // Hide splash screen after initializing
};
initializeApp();
}, []);
const handleBarCodeScanned = ({ type, data }) => {
setScanned(true); // Mark as scanned
const newScannedData = `Type: ${type}\nData: ${data}`;
setScannedData(newScannedData); // Save scanned data
setQrCodes([...qrCodes, newScannedData]); // Add scanned data to history
alert(`Bar code with type ${type} and data ${data} has been scanned!`); // Show an alert
};
if (showSplash) {
return (
);
}
if (hasPermission === null) {
return Requesting for camera permission;
}
if (hasPermission === false) {
return No access to camera;
}
return (
{/* Header banner */}
SafeQR
{/* Welcome message */}
Welcome to SafeQR code Scanner
{/* Camera view container */}
{/* Display scanned data */}
{scannedData !== '' && (
{scannedData}
)}
{/* Button to scan again */}
{scanned && (
setScanned(false)}>
Tap to Scan Again
)}
{/* Menu (Placeholder for additional menu items) */}
{/* Your existing menu items */}
);
}
function HistoryScreen() {
const { qrCodes } = useContext(QRCodeContext); // Access context
return (
History Screen
(
{item}
)}
keyExtractor={(item, index) => index.toString()}
/>
);
}
function SettingsScreen() {
return (
Settings Screen
);
}
function ProfileScreen() {
return (
Profile Screen
);
}
// Main App component with bottom tab navigation
export default function App() {
const [qrCodes, setQrCodes] = useState([]); // State to hold QR codes
return (
({
tabBarIcon: ({ color, size }) => {
let iconName;
// Set different icons for each tab
if (route.name === 'QR Scanner') {
iconName = 'qr-code-outline';
} else if (route.name === 'History') {
iconName = 'time-outline';
} else if (route.name === 'Settings') {
iconName = 'settings-outline';
} else if (route.name === 'Profile') {
iconName = 'person-outline';
}
// Return the appropriate icon
return ;
},
})}
tabBarOptions={{
activeTintColor: 'tomato', // Active tab color
inactiveTintColor: 'gray', // Inactive tab color
}}
>
);
}
// StyleSheet for styling components
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fa5da2',
},
splashContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#f8f0fc", // light purple background
},
banner: {
backgroundColor: "#333", // dark background
paddingVertical: 10,
alignItems: "center",
},
headerText: {
color: "white",
fontSize: 24,
},
welcomeText: {
textAlign: "center",
fontSize: 20,
marginVertical: 10, // Adjusted margin
color: "white",
},
cameraContainer: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
camera: {
width: 300,
height: 300,
},
button: {
backgroundColor: '#333',
paddingHorizontal: 20,
paddingVertical: 10,
borderRadius: 30,
alignItems: 'center',
justifyContent: 'center',
marginVertical: 10,
},
buttonText: {
color: 'white',
fontSize: 16,
},
dataBox: {
marginVertical: 10,
padding: 10,
backgroundColor: "#fff",
borderRadius: 5,
alignItems: "center",
justifyContent: "center",
},
dataText: {
fontSize: 16,
color: "#000",
},
menu: {
flexDirection: "row",
justifyContent: "space-around",
alignItems: "center",
backgroundColor: "#ff69b4", // pink background
paddingVertical: 10,
},
});