added Polling Control with useFocusEffect to prevent GetScannedEmail on other screen

This commit is contained in:
2024-08-11 21:23:03 +08:00
parent 80e38660cd
commit cad9f2c097

View File

@@ -1,8 +1,8 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useCallback } from 'react';
import { View, Text, TouchableOpacity, FlatList, StyleSheet, ActivityIndicator, Alert, Animated, Dimensions } from 'react-native'; import { View, Text, TouchableOpacity, FlatList, StyleSheet, ActivityIndicator, Alert, Animated, Dimensions } from 'react-native';
import { Ionicons } from '@expo/vector-icons'; import { Ionicons } from '@expo/vector-icons';
import { useFocusEffect } from '@react-navigation/native';
import { getEmails, getScannedEmails, getUserInfo } from '../api/qrCodeAPI'; import { getEmails, getScannedEmails, getUserInfo } from '../api/qrCodeAPI';
import { fetchAuthSession } from 'aws-amplify/auth'; import { fetchAuthSession } from 'aws-amplify/auth';
import { Buffer } from 'buffer'; import { Buffer } from 'buffer';
@@ -18,8 +18,6 @@ const EmailScreen: React.FC = () => {
const [bannerOpacity] = useState(new Animated.Value(0)); const [bannerOpacity] = useState(new Animated.Value(0));
useEffect(() => { useEffect(() => {
startInboxScanning();
startPollingForScannedEmails();
fetchUserEmail(); fetchUserEmail();
}, []); }, []);
@@ -87,7 +85,7 @@ const EmailScreen: React.FC = () => {
}; };
// Function to poll for scanned emails // Function to poll for scanned emails
const startPollingForScannedEmails = () => { const startPollingForScannedEmails = useCallback(() => {
const pollingInterval = setInterval(async () => { const pollingInterval = setInterval(async () => {
try { try {
const scannedEmails = await getScannedEmails(); const scannedEmails = await getScannedEmails();
@@ -102,8 +100,8 @@ const EmailScreen: React.FC = () => {
} }
}, 10000); // Poll every 10 seconds }, 10000); // Poll every 10 seconds
return () => clearInterval(pollingInterval); return () => clearInterval(pollingInterval); // Cleanup the interval
}; }, []);
const handleSelectMessage = (message) => { const handleSelectMessage = (message) => {
setSelectedMessage(selectedMessage === message ? null : message); setSelectedMessage(selectedMessage === message ? null : message);
@@ -119,6 +117,17 @@ const EmailScreen: React.FC = () => {
} }
}; };
useFocusEffect(
useCallback(() => {
startInboxScanning();
const stopPolling = startPollingForScannedEmails();
return () => {
stopPolling(); // Stop polling when the screen is not in focus
};
}, [startPollingForScannedEmails])
);
return ( return (
<View style={styles.container}> <View style={styles.container}>
{loading && ( {loading && (
@@ -196,8 +205,6 @@ const EmailScreen: React.FC = () => {
); );
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
@@ -306,4 +313,5 @@ const styles = StyleSheet.create({
fontSize: screenWidth * 0.04, fontSize: screenWidth * 0.04,
}, },
}); });
export default EmailScreen;
export default EmailScreen;