u
This commit is contained in:
1 parent
458f0cd6ac
commit
2347d55823
8 files changed
+299
-150
No files matched your search
@@ -1,13 +1,14 @@
|
||||
import type { ErrorBoundaryProps } from 'expo-router'
|
||||
import { ToastProvider } from '@gluestack-ui/core/toast/creator'
|
||||
import { useFonts } from 'expo-font'
|
||||
import { Stack } from 'expo-router'
|
||||
import { Stack, useRouter } from 'expo-router'
|
||||
import * as SplashScreen from 'expo-splash-screen'
|
||||
import { StatusBar } from 'expo-status-bar'
|
||||
import { RefreshCw } from 'lucide-react-native'
|
||||
import { useEffect } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { EmptyState } from '@/components/common/empty-status'
|
||||
import { GluestackUIProvider } from '@/components/ui/gluestack-ui-provider'
|
||||
import { useUserStore } from '@/store/user'
|
||||
import { ToastProviderWrapper } from '@/utils/toast'
|
||||
import '@/styles/global.css'
|
||||
import 'react-native-reanimated'
|
||||
@@ -31,15 +32,34 @@ export function ErrorBoundary({ error, retry }: ErrorBoundaryProps) {
|
||||
}
|
||||
|
||||
export default function RootLayout() {
|
||||
const [loaded] = useFonts({
|
||||
const router = useRouter()
|
||||
|
||||
const token = useUserStore(state => state.token)
|
||||
|
||||
const [fontsLoaded] = useFonts({
|
||||
SpaceMono: require('@/static/fonts/SpaceMono-Regular.ttf'),
|
||||
})
|
||||
|
||||
const [storeLoaded, setStoreLoaded] = useState(false)
|
||||
|
||||
// 等待 store 恢复数据
|
||||
useEffect(() => {
|
||||
if (loaded) {
|
||||
SplashScreen.hideAsync()
|
||||
if (token !== undefined) {
|
||||
setStoreLoaded(true)
|
||||
}
|
||||
}, [loaded])
|
||||
}, [token])
|
||||
|
||||
// splash screen 隐藏 & 登录跳转
|
||||
useEffect(() => {
|
||||
if (fontsLoaded && storeLoaded) {
|
||||
SplashScreen.hideAsync()
|
||||
|
||||
// 如果没有 token,则跳转登录页
|
||||
if (token === null) {
|
||||
router.replace('/auth/login')
|
||||
}
|
||||
}
|
||||
}, [fontsLoaded, storeLoaded, token])
|
||||
|
||||
return (
|
||||
<GluestackUIProvider>
|
||||
@@ -49,12 +69,24 @@ export default function RootLayout() {
|
||||
screenOptions={{
|
||||
headerTitleAlign: 'center',
|
||||
animation: 'slide_from_right',
|
||||
gestureEnabled: true, // 启用手势返回
|
||||
gestureDirection: 'horizontal', // 水平手势
|
||||
contentStyle: {
|
||||
backgroundColor: '#fff',
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Stack.Screen
|
||||
name="(tabs)"
|
||||
options={{ headerShown: false }}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="auth/login"
|
||||
options={{
|
||||
title: '登录',
|
||||
headerShown: true,
|
||||
}}
|
||||
/>
|
||||
<Stack.Screen name="+not-found" />
|
||||
</Stack>
|
||||
<StatusBar style="auto" />
|
||||
|
||||
Reference in new issue
Block a user