Make QR Tips not polled when on other screens
moved function to useEffect instead on QrScanner Screen
This commit is contained in:
@@ -8,6 +8,7 @@ import ScannedDataBox from '../components/ScannedDataBox';
|
|||||||
import { scanQRCode, getQRTips } from '../api/qrCodeAPI';
|
import { scanQRCode, getQRTips } from '../api/qrCodeAPI';
|
||||||
import SettingsScreen from './SettingsScreen';
|
import SettingsScreen from './SettingsScreen';
|
||||||
import NetInfo from '@react-native-community/netinfo';
|
import NetInfo from '@react-native-community/netinfo';
|
||||||
|
import { useFocusEffect } from '@react-navigation/native';
|
||||||
|
|
||||||
const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
|
const { width: screenWidth, height: screenHeight } = Dimensions.get('window');
|
||||||
|
|
||||||
@@ -27,39 +28,40 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
|||||||
const { hasPermission, requestPermission } = useCameraPermission();
|
const { hasPermission, requestPermission } = useCameraPermission();
|
||||||
const device = useCameraDevice('back');
|
const device = useCameraDevice('back');
|
||||||
|
|
||||||
// Fetch QR Tips and manage polling
|
const fetchTips = async () => {
|
||||||
useEffect(() => {
|
try {
|
||||||
const fetchTips = async () => {
|
const response = await getQRTips();
|
||||||
try {
|
setQrTip(response.tips); // Set the qrTip state to the value of the tips property
|
||||||
const response = await getQRTips();
|
} catch (error) {
|
||||||
setQrTip(response.tips); // Set the qrTip state to the value of the tips property
|
console.error('Error fetching QR tips:', error);
|
||||||
} catch (error) {
|
}
|
||||||
console.error('Error fetching QR tips:', error);
|
};
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
requestPermission(); // Request camera permission on component mount
|
// Only run when the screen is focusd
|
||||||
|
useFocusEffect(
|
||||||
|
React.useCallback(() => {
|
||||||
|
requestPermission(); // Request camera permission when screen is focused
|
||||||
|
|
||||||
// Initial fetch for QR tips
|
// Initial fetch for QR tips
|
||||||
fetchTips();
|
fetchTips();
|
||||||
|
|
||||||
// Set interval for fetching QR tips every 5 seconds
|
// Set interval for fetching QR tips every 6 seconds
|
||||||
const intervalId = setInterval(fetchTips, 10000);
|
const intervalId = setInterval(fetchTips, 6000);
|
||||||
|
|
||||||
// Subscribe to network state updates
|
// Subscribe to network state updates
|
||||||
const unsubscribe = NetInfo.addEventListener(state => {
|
const unsubscribe = NetInfo.addEventListener(state => {
|
||||||
setIsConnected(state.isConnected);
|
setIsConnected(state.isConnected);
|
||||||
if (!state.isConnected) {
|
if (!state.isConnected) {
|
||||||
showBanner(); // Show banner if the device goes offline
|
showBanner(); // Show banner if the device goes offline
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Cleanup on component unmount
|
return () => {
|
||||||
return () => {
|
clearInterval(intervalId); // Clear interval when screen is unfocused
|
||||||
clearInterval(intervalId); // Clear interval
|
unsubscribe(); // Unsubscribe from network state updates
|
||||||
unsubscribe(); // Unsubscribe from network state updates
|
};
|
||||||
};
|
}, [])
|
||||||
}, []);
|
);
|
||||||
|
|
||||||
const showScannedDataBox = () => {
|
const showScannedDataBox = () => {
|
||||||
setIsScannedDataBoxVisible(true);
|
setIsScannedDataBoxVisible(true);
|
||||||
@@ -79,6 +81,7 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
|||||||
setIsScannedDataBoxVisible(false);
|
setIsScannedDataBoxVisible(false);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const clearSelectedQrCodeData = () => {
|
const clearSelectedQrCodeData = () => {
|
||||||
console.log("!!!!!clearSelectedQrCodeData");
|
console.log("!!!!!clearSelectedQrCodeData");
|
||||||
setQRCodeId(null);
|
setQRCodeId(null);
|
||||||
@@ -86,7 +89,7 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
|||||||
setScanned(false); // Reset the scanned state so the camera can scan again
|
setScanned(false); // Reset the scanned state so the camera can scan again
|
||||||
clearScanData(); // Call the clearScanData passed from App.tsx
|
clearScanData(); // Call the clearScanData passed from App.tsx
|
||||||
};
|
};
|
||||||
|
|
||||||
// Show an offline banner
|
// Show an offline banner
|
||||||
const showBanner = () => {
|
const showBanner = () => {
|
||||||
Animated.timing(bannerOpacity, {
|
Animated.timing(bannerOpacity, {
|
||||||
@@ -112,7 +115,7 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
|||||||
const response = await scanQRCode(payload);
|
const response = await scanQRCode(payload);
|
||||||
const qrCodeId = response.qrcode.data.id;
|
const qrCodeId = response.qrcode.data.id;
|
||||||
setQRCodeId(qrCodeId);
|
setQRCodeId(qrCodeId);
|
||||||
showScannedDataBox(); // Show the ScannedDataBox pop-up with animation
|
showScannedDataBox(); // Show the ScannedDataBox pop-up with animation
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error scanning QR code:", error);
|
console.error("Error scanning QR code:", error);
|
||||||
}
|
}
|
||||||
@@ -166,7 +169,6 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
|||||||
if (!device) {
|
if (!device) {
|
||||||
return <Text>Loading camera...</Text>;
|
return <Text>Loading camera...</Text>;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
{/* Banner for network connectivity */}
|
{/* Banner for network connectivity */}
|
||||||
@@ -299,7 +301,7 @@ const styles = StyleSheet.create({
|
|||||||
borderRadius: 10,
|
borderRadius: 10,
|
||||||
overflow: 'hidden',
|
overflow: 'hidden',
|
||||||
alignSelf: 'center',
|
alignSelf: 'center',
|
||||||
marginTop: '10%',
|
marginTop: '1%',
|
||||||
},
|
},
|
||||||
settingsButton: {
|
settingsButton: {
|
||||||
position: 'absolute',
|
position: 'absolute',
|
||||||
|
|||||||
Reference in New Issue
Block a user