import { View, Text, ScrollView, Image, Switch, Pressable, Dimensions, Linking, Alert, AppState } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { router, useFocusEffect } from 'expo-router'; import { useAuth } from '../../context/AuthProvider'; import * as Notifications from 'expo-notifications'; import { useColorScheme } from 'nativewind'; import Constants from 'expo-constants'; import { useState, useEffect, useCallback } from 'react'; import { AccountSettingIconSvg, ActivitySvg, MyCarSvg, DarkModeSvg, QuestionMarkIconSvg, SettingIconSvg, VipCodeIcoonSvg, WalletSvg, NotificationSvg } from '../global/SVG'; import { usePushNotifications } from '../../app/hooks/usePushNotifications'; import useUserInfoStore from '../../providers/userinfo_store'; import { authenticationService } from '../../service/authService'; const AccountMainPageComponent = () => { const { user, logout } = useAuth(); const { colorScheme, toggleColorScheme } = useColorScheme(); const screenWidth = Dimensions.get('window').width; const imageWidth = screenWidth * 0.9; // 90% of screen width const aspectRatio = 210 / 375; const [notificationsEnabled, setNotificationsEnabled] = useState(false); const { expoPushToken, notification } = usePushNotifications(); // const notify_session_id = expoPushToken?.data || ''; // Check initial notification permission status const { notifySessionID } = useUserInfoStore(); //everytime i am on this screen, i check if permission is granted. useFocusEffect( useCallback(() => { const checkPermissionsAndToken = async () => { const { status } = await Notifications.getPermissionsAsync(); setNotificationsEnabled(status === 'granted'); if (status === 'granted') { //if status is granted, then i will fetch latest expo token to see if it is the same as notify_session_id const latestToken = await Notifications.getExpoPushTokenAsync({ projectId: Constants.expoConfig?.extra?.eas?.projectId }); console.log('latestToken.data', latestToken.data); console.log('notifySessionID', notifySessionID); //TODO: if latestToken = notify_session_id, do nothing if (latestToken.data === notifySessionID) { return; } else { const res = await authenticationService.changeNotifySessionID(latestToken.data); if (res) { console.log('Change notifySessionID Successfully!'); } else { Alert.alert('更新通知權限失敗', '請稍後再試'); } } //TODO: if latestToken != notify_session_id, update notify_session_id to latestToken } }; checkPermissionsAndToken(); const subscription = AppState.addEventListener('change', (nextAppState: string) => { if (nextAppState === 'active') { checkPermissionsAndToken(); } }); return () => { subscription.remove(); }; }, [expoPushToken]) // Add expoPushToken as a dependency ); const toggleNotifications = async () => { Alert.alert('通知設定', '要更改通知設定,請前往手機的設定頁面,允許CrazyCharge通知。', [ { text: '取消', style: 'cancel' }, { text: '前往設定', onPress: () => Linking.openSettings() } ]); }; return ( {user?.nickname} router.push('/(account)/(wallet)/walletPage')} > 錢包 {/* router.replace('myVehiclePage')} > 我的車輛 */} router.replace('vipQrPage')} > VIP Code router.push('/activityRecordPage')} > 充電記錄 router.push('uberVerificationPage')}> router.push('/accountSettingPage')} className="flex-row items-center" hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} > 帳戶管理 router.push('/assistancePage')} className="flex-row items-center" hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} > 排除解難 router.push('/assistancePage')} className="flex-row items-center" hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} > 允許通知 {/* router.push('/assistancePage')} className="flex-row items-center" hitSlop={{ top: 10, bottom: 10, left: 10, right: 10 }} > 深色模式 */} 登出 ); }; export default AccountMainPageComponent;