import React, { useEffect, useState, useRef } from 'react';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { Platform, Modal, View, StyleSheet, Text, TouchableOpacity, Animated, Keyboard } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Ionicons from '@expo/vector-icons/Ionicons';
import { useNavigation, useRoute } from '@react-navigation/native';
import type { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { RootStackParamList } from '../types/navigation';
import { useAuth } from '../contexts/AuthContext';
import { useTranslation } from 'react-i18next';
import { HomeScreen } from '../screens/HomeScreen';
import { CategoryScreen } from '../screens/CategoryScreen';
import { ChatScreen } from '../screens/ChatScreen';
import { CartScreen } from '../screens/CartScreen';
import { ProfileScreen } from '../screens/ProfileScreen';
type IconProps = {
name: string;
size: number;
color: string;
};
const IconComponent = ({ name, size, color }: IconProps) => {
const Icon = Ionicons as any;
return ;
};
type TabBarIconProps = {
color: string;
size: number;
};
const Tab = createBottomTabNavigator();
export const TabNavigator = () => {
const { t } = useTranslation();
const navigation = useNavigation>();
const route = useRoute();
const { isLoggedIn } = useAuth();
const [showLoginPrompt, setShowLoginPrompt] = useState(false);
const [keyboardVisible, setKeyboardVisible] = useState(false);
const [promptShownBefore, setPromptShownBefore] = useState(false);
// 动画值
const fadeAnim = useRef(new Animated.Value(0)).current;
const slideAnim = useRef(new Animated.Value(300)).current;
// 监听键盘事件
useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
() => {
setKeyboardVisible(true);
}
);
const keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
() => {
setKeyboardVisible(false);
}
);
return () => {
keyboardDidHideListener.remove();
keyboardDidShowListener.remove();
};
}, []);
// 检查是否已经显示过弹窗
useEffect(() => {
const checkPromptShown = async () => {
try {
const value = await AsyncStorage.getItem('loginPromptShown');
setPromptShownBefore(value === 'true');
} catch (error) {
console.error('Error checking prompt status:', error);
}
};
checkPromptShown();
}, []);
// 检查登录状态
useEffect(() => {
// 如果是从 CountrySelect 页面来的,不显示登录提示
if (route.name === 'CountrySelect') {
return;
}
// 如果已登录或已经显示过弹窗,不再显示
if (isLoggedIn || promptShownBefore) {
handleCloseModal();
} else {
setShowLoginPrompt(true);
// 标记弹窗已显示
AsyncStorage.setItem('loginPromptShown', 'true');
setPromptShownBefore(true);
Animated.parallel([
Animated.timing(fadeAnim, {
toValue: 1,
duration: 300,
useNativeDriver: true,
}),
Animated.spring(slideAnim, {
toValue: 0,
tension: 50,
friction: 9,
useNativeDriver: true,
})
]).start();
}
}, [isLoggedIn, route.name, promptShownBefore]);
const handleCloseModal = () => {
Animated.parallel([
Animated.timing(fadeAnim, {
toValue: 0,
duration: 200,
useNativeDriver: true,
}),
Animated.timing(slideAnim, {
toValue: 300,
duration: 200,
useNativeDriver: true,
})
]).start(() => {
setShowLoginPrompt(false);
});
};
const handleGoToLogin = () => {
handleCloseModal();
navigation.navigate('Login');
};
return (
<>
(
),
}}
/>
(
),
}}
/>
(
),
}}
/>
(
),
}}
/>
(
),
}}
/>
✕
{t('welcomeTitle')}
{t('welcomeMessage')}
{t('loginNow')}
>
);
};
const styles = StyleSheet.create({
modalContainer: {
flex: 1,
justifyContent: 'flex-end',
},
modalOverlay: {
...StyleSheet.absoluteFillObject,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
modalOverlayTouch: {
flex: 1,
},
modalContent: {
backgroundColor: '#fff',
borderTopLeftRadius: 24,
borderTopRightRadius: 24,
paddingHorizontal: 20,
paddingBottom: Platform.OS === 'ios' ? 40 : 20,
position: 'relative',
},
closeButton: {
position: 'absolute',
top: 20,
right: 20,
width: 24,
height: 24,
justifyContent: 'center',
alignItems: 'center',
zIndex: 1,
},
closeButtonText: {
fontSize: 20,
color: '#999',
},
modalHeader: {
alignItems: 'center',
paddingTop: 12,
paddingBottom: 20,
},
modalIndicator: {
width: 40,
height: 4,
backgroundColor: '#E0E0E0',
borderRadius: 2,
},
modalTitle: {
fontSize: 20,
fontWeight: '600',
color: '#000',
textAlign: 'center',
marginBottom: 12,
},
modalText: {
fontSize: 16,
color: '#666',
textAlign: 'center',
marginBottom: 24,
lineHeight: 22,
},
modalButton: {
backgroundColor: '#0066FF',
height: 50,
borderRadius: 25,
justifyContent: 'center',
alignItems: 'center',
},
modalButtonText: {
color: '#fff',
fontSize: 16,
fontWeight: '600',
},
});