Fixed the EmailScreen Logic and moved banner to middle

This commit is contained in:
2024-08-11 21:13:43 +08:00
parent 01fa62a58f
commit 80e38660cd

View File

@@ -18,6 +18,7 @@ const EmailScreen: React.FC = () => {
const [bannerOpacity] = useState(new Animated.Value(0)); const [bannerOpacity] = useState(new Animated.Value(0));
useEffect(() => { useEffect(() => {
startInboxScanning();
startPollingForScannedEmails(); startPollingForScannedEmails();
fetchUserEmail(); fetchUserEmail();
}, []); }, []);
@@ -35,7 +36,7 @@ const EmailScreen: React.FC = () => {
}; };
// Function to initiate the email fetching process // Function to initiate the email fetching process
const initiateEmailFetch = async () => { const startInboxScanning = async () => {
setRescanLoading(true); setRescanLoading(true);
showBanner(); showBanner();
@@ -54,7 +55,7 @@ const EmailScreen: React.FC = () => {
if (googleAccessToken && googleRefreshToken) { if (googleAccessToken && googleRefreshToken) {
// Use the fetched tokens to initiate email fetching // Use the fetched tokens to initiate email fetching
const response = await getEmails(googleAccessToken, googleRefreshToken); await getEmails(googleAccessToken, googleRefreshToken);
setRescanLoading(false); setRescanLoading(false);
} else { } else {
console.error('Google access token or refresh token not found in the payload'); console.error('Google access token or refresh token not found in the payload');
@@ -92,22 +93,32 @@ const EmailScreen: React.FC = () => {
const scannedEmails = await getScannedEmails(); const scannedEmails = await getScannedEmails();
if (scannedEmails) { if (scannedEmails) {
setEmailData(scannedEmails); setEmailData(scannedEmails);
clearInterval(pollingInterval);
setLoading(false); setLoading(false);
} }
} catch (error) { } catch (error) {
console.error('Error fetching scanned emails:', error); console.error('Error fetching scanned emails:', error);
setError('Error fetching emails.'); setError('Error fetching emails.');
clearInterval(pollingInterval);
setLoading(false); setLoading(false);
} }
}, 3000); // Poll every 3 seconds }, 10000); // Poll every 10 seconds
return () => clearInterval(pollingInterval);
}; };
const handleSelectMessage = (message) => { const handleSelectMessage = (message) => {
setSelectedMessage(selectedMessage === message ? null : message); setSelectedMessage(selectedMessage === message ? null : message);
}; };
const refreshScannedEmails = async () => {
try {
const scannedEmails = await getScannedEmails();
setEmailData(scannedEmails);
} catch (error) {
console.error('Error refreshing scanned emails:', error);
setError('Error refreshing emails.');
}
};
return ( return (
<View style={styles.container}> <View style={styles.container}>
{loading && ( {loading && (
@@ -125,7 +136,7 @@ const EmailScreen: React.FC = () => {
<> <>
<View style={styles.headerContainer}> <View style={styles.headerContainer}>
<Text style={styles.emailHeader}>Email: {userEmail}</Text> <Text style={styles.emailHeader}>Email: {userEmail}</Text>
<TouchableOpacity onPress={initiateEmailFetch} style={styles.refreshButton}> <TouchableOpacity onPress={refreshScannedEmails} style={styles.refreshButton}>
<Ionicons name="refresh" size={24} color="#ff69b4" /> <Ionicons name="refresh" size={24} color="#ff69b4" />
</TouchableOpacity> </TouchableOpacity>
</View> </View>
@@ -185,6 +196,8 @@ const EmailScreen: React.FC = () => {
); );
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
flex: 1, flex: 1,
@@ -275,20 +288,22 @@ const styles = StyleSheet.create({
}, },
banner: { banner: {
position: 'absolute', position: 'absolute',
top: 0, top: screenHeight * 0.4, // Adjusts the banner to appear in the middle of the screen
left: 0, left: screenWidth * 0.1, // Adjust these values to center the banner as needed
right: 0, right: screenWidth * 0.1,
backgroundColor: '#ff69b4', backgroundColor: '#ff69b4',
paddingVertical: 20, // Increase this value for more height paddingVertical: screenHeight * 0.02, // Adjust the height of the banner
paddingHorizontal: 10, paddingHorizontal: screenWidth * 0.05,
borderRadius: screenWidth * 0.05,
alignItems: 'center', alignItems: 'center',
justifyContent: 'center', justifyContent: 'center',
zIndex: 10, // Ensure it appears above other elements
}, },
bannerText: { bannerText: {
color: '#fff', color: '#fff',
fontWeight: 'bold', fontWeight: 'bold',
textAlign: 'center',
fontSize: screenWidth * 0.04,
}, },
}); });
export default EmailScreen; export default EmailScreen;