import FontAwesome from '@expo/vector-icons/FontAwesome'; import { DarkTheme, DefaultTheme, ThemeProvider } from '@react-navigation/native'; import { useFonts } from 'expo-font'; import { Stack } from 'expo-router'; import * as SplashScreen from 'expo-splash-screen'; import * as Updates from 'expo-updates'; import { useEffect } from 'react'; import { Alert, Platform } from 'react-native'; import 'react-native-reanimated'; import { PaperProvider, MD3DarkTheme, MD3LightTheme } from 'react-native-paper'; // ✅ 从 hooks 目录导入 import { useColorScheme } from '@/hooks'; // ✅ 从 stores 目录导入 import { restoreUserState, restoreSettingsState, useTenantActions } from '@/stores'; export { // Catch any errors thrown by the Layout component. ErrorBoundary, } from 'expo-router'; export const unstable_settings = { // Ensure that reloading on `/modal` keeps a back button present. initialRouteName: '(tabs)', }; // Prevent the splash screen from auto-hiding before asset loading is complete. SplashScreen.preventAutoHideAsync(); export default function RootLayout() { const [loaded, error] = useFonts({ SpaceMono: require('../assets/fonts/SpaceMono-Regular.ttf'), ...FontAwesome.font, }); const { requestTenantInfo } = useTenantActions(); // Expo Router uses Error Boundaries to catch errors in the navigation tree. useEffect(() => { if (error) throw error; }, [error]); useEffect(() => { if (loaded) { SplashScreen.hideAsync(); } }, [loaded]); // 恢复持久化状态并初始化应用数据 useEffect(() => { async function initializeApp() { try { // 1. 恢复本地存储的状态 await Promise.all([restoreUserState(), restoreSettingsState()]); // 2. 调用初始化接口(获取平台数据等) if (__DEV__) { console.log('🚀 Initializing app data...'); } await requestTenantInfo(); if (__DEV__) { console.log('✅ Platform data loaded:'); } } catch (error) { console.error('Failed to initialize app:', error); // 初始化失败不应该阻止应用启动,只记录错误 } } initializeApp(); }, []); // 检查热更新 useEffect(() => { async function checkForUpdates() { if (__DEV__) { // 开发模式下不检查更新 return; } try { const update = await Updates.checkForUpdateAsync(); if (update.isAvailable) { await Updates.fetchUpdateAsync(); // 提示用户重启应用以应用更新 Alert.alert('更新可用', '发现新版本,是否立即重启应用?', [ { text: '稍后', style: 'cancel', }, { text: '立即重启', onPress: async () => { await Updates.reloadAsync(); }, }, ]); } } catch (error) { // 处理更新检查错误 console.error('检查更新失败:', error); } } checkForUpdates(); }, []); if (!loaded) { return null; } return ; } function RootLayoutNav() { const colorScheme = useColorScheme(); const paperTheme = colorScheme === 'dark' ? MD3DarkTheme : MD3LightTheme; return ( ); }