import { useState, useRef, useEffect } from 'react'; import { View, Text, Pressable, StyleSheet, ActivityIndicator, Platform, Alert, } from 'react-native'; import { Camera } from 'expo-camera'; import { useRouter, useLocalSearchParams } from 'expo-router'; import { api } from '@/lib/api'; import type { Item } from '@shop/types'; /** * Barcode scanner screen * Uses device camera to scan barcodes and add items to list */ export default function BarcodeScanner() { const router = useRouter(); const { id } = useLocalSearchParams<{ id: string }>(); const [permission, requestPermission] = Camera.useCameraPermissions(); const [isScanning, setIsScanning] = useState(true); const [scannedBarcode, setScannedBarcode] = useState(null); const [isProcessing, setIsProcessing] = useState(false); const cameraRef = useRef(null); useEffect(() => { if (requestPermission) { requestPermission(); } }, [requestPermission]); const handleBarcodeScan = async (barcode: string) => { if (!isScanning || isProcessing) return; setIsScanning(false); setIsProcessing(true); setScannedBarcode(barcode); try { // Try to find item by barcode const item = await api.get(`/api/items/barcode/${barcode}`); if (item) { // Item found, add it to the list await api.post(`/api/lists/${id}/items`, { itemId: item.id, quantity: 1, }); // Log the scan await api.post('/api/scan-history', { barcode, itemId: item.id, }); Alert.alert('Success', `Added ${item.name} to list`, [ { text: 'Continue Scanning', onPress: () => { setScannedBarcode(null); setIsScanning(true); }, }, { text: 'Done', onPress: () => router.back(), }, ]); } else { // Item not found, prompt to create it Alert.alert( 'Item Not Found', 'This barcode is not in the system. Would you like to add it manually?', [ { text: 'Create Item', onPress: () => { router.push(`/(app)/list/${id}/add-item?barcode=${barcode}`); }, }, { text: 'Try Again', onPress: () => { setScannedBarcode(null); setIsScanning(true); }, }, { text: 'Cancel', onPress: () => router.back(), }, ] ); } } catch (err) { console.error('Barcode scan failed:', err); Alert.alert('Error', 'Failed to process barcode', [ { text: 'Try Again', onPress: () => { setScannedBarcode(null); setIsScanning(true); }, }, ]); } finally { setIsProcessing(false); } }; if (!permission) { return ( ); } if (!permission.granted) { return ( Camera Permission Required We need access to your camera to scan barcodes Grant Permission ); } return ( {/* Camera view */} { if (result?.data && !scannedBarcode) { handleBarcodeScan(result.data); } }} /> {/* Overlay frame */} {/* Instructions */} {scannedBarcode ? 'Processing...' : 'Point camera at barcode'} {/* Close button */} router.back()}> ✕ {/* Torch button (simplified) */} { // Torch functionality simplified for compatibility console.log('Torch toggle requested'); }} > 💡 ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#000', }, camera: { flex: 1, }, overlay: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, justifyContent: 'center', alignItems: 'center', }, top: { flex: 1, width: '100%', backgroundColor: 'rgba(0, 0, 0, 0.6)', }, middle: { flexDirection: 'row', height: 200, }, left: { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.6)', }, frame: { width: 250, height: 200, borderWidth: 2, borderColor: '#00FF00', borderRadius: 8, }, right: { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.6)', }, bottom: { flex: 1, width: '100%', backgroundColor: 'rgba(0, 0, 0, 0.6)', }, instructions: { position: 'absolute', bottom: 100, left: 0, right: 0, alignItems: 'center', }, instructionsText: { fontSize: 16, color: '#fff', fontWeight: '600', }, closeButton: { position: 'absolute', top: Platform.OS === 'ios' ? 44 : 12, left: 16, width: 40, height: 40, justifyContent: 'center', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.6)', borderRadius: 20, }, closeButtonText: { fontSize: 20, color: '#fff', fontWeight: '600', }, torchButton: { position: 'absolute', top: Platform.OS === 'ios' ? 44 : 12, right: 16, width: 40, height: 40, justifyContent: 'center', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.6)', borderRadius: 20, }, torchButtonText: { fontSize: 20, }, centerContent: { justifyContent: 'center', alignItems: 'center', paddingHorizontal: 20, }, permissionTitle: { fontSize: 18, fontWeight: '700', color: '#fff', marginBottom: 12, }, permissionText: { fontSize: 16, color: '#ccc', marginBottom: 24, textAlign: 'center', }, permissionButton: { backgroundColor: '#007AFF', paddingVertical: 12, paddingHorizontal: 24, borderRadius: 8, }, permissionButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });