302 lines
7.1 KiB
TypeScript
302 lines
7.1 KiB
TypeScript
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<string | null>(null);
|
|
const [isProcessing, setIsProcessing] = useState(false);
|
|
const cameraRef = useRef<Camera>(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<Item | null>(`/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 (
|
|
<View style={styles.container}>
|
|
<ActivityIndicator size="large" color="#007AFF" />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
if (!permission.granted) {
|
|
return (
|
|
<View style={styles.container}>
|
|
<View style={styles.centerContent}>
|
|
<Text style={styles.permissionTitle}>Camera Permission Required</Text>
|
|
<Text style={styles.permissionText}>We need access to your camera to scan barcodes</Text>
|
|
<Pressable style={styles.permissionButton} onPress={requestPermission}>
|
|
<Text style={styles.permissionButtonText}>Grant Permission</Text>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<View style={styles.container}>
|
|
{/* Camera view */}
|
|
<Camera
|
|
ref={cameraRef}
|
|
style={styles.camera}
|
|
onBarCodeScanned={(result: any) => {
|
|
if (result?.data && !scannedBarcode) {
|
|
handleBarcodeScan(result.data);
|
|
}
|
|
}}
|
|
/>
|
|
|
|
{/* Overlay frame */}
|
|
<View style={styles.overlay}>
|
|
<View style={styles.top} />
|
|
<View style={styles.middle}>
|
|
<View style={styles.left} />
|
|
<View style={styles.frame} />
|
|
<View style={styles.right} />
|
|
</View>
|
|
<View style={styles.bottom} />
|
|
</View>
|
|
|
|
{/* Instructions */}
|
|
<View style={styles.instructions}>
|
|
<Text style={styles.instructionsText}>
|
|
{scannedBarcode ? 'Processing...' : 'Point camera at barcode'}
|
|
</Text>
|
|
</View>
|
|
|
|
{/* Close button */}
|
|
<Pressable style={styles.closeButton} onPress={() => router.back()}>
|
|
<Text style={styles.closeButtonText}>✕</Text>
|
|
</Pressable>
|
|
|
|
{/* Torch button (simplified) */}
|
|
<Pressable
|
|
style={styles.torchButton}
|
|
onPress={() => {
|
|
// Torch functionality simplified for compatibility
|
|
console.log('Torch toggle requested');
|
|
}}
|
|
>
|
|
<Text style={styles.torchButtonText}>💡</Text>
|
|
</Pressable>
|
|
</View>
|
|
);
|
|
}
|
|
|
|
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',
|
|
},
|
|
});
|