-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
205 lines (192 loc) · 6.13 KB
/
App.tsx
File metadata and controls
205 lines (192 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import React, { useState, useEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { TouchableOpacity, StyleSheet, ActivityIndicator, View, Platform } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { ThemeProvider, useTheme } from './src/context/ThemeContext';
import { authApi, tokenStorage } from './src/api';
import LoginScreen from './src/screens/LoginScreen';
import RegisterScreen from './src/screens/RegisterScreen';
import DiaryListScreen from './src/screens/DiaryListScreen';
import EditScreen from './src/screens/EditScreen';
import StatsScreen from './src/screens/StatsScreen';
import SettingsScreen from './src/screens/SettingsScreen';
// 在 Web 平台注册 Service Worker
if (Platform.OS === 'web' && 'serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('/service-worker.js')
.then((registration) => {
console.log('[PWA] Service Worker 注册成功:', registration.scope);
})
.catch((error) => {
console.log('[PWA] Service Worker 注册失败:', error);
});
});
}
const Stack = createStackNavigator();
const AppContent: React.FC<{
isAuthenticated: boolean;
showLogin: boolean;
setShowLogin: (show: boolean) => void;
handleLogin: () => void;
handleLogout: () => void;
}> = ({ isAuthenticated, showLogin, setShowLogin, handleLogin, handleLogout }) => {
const { theme, toggleTheme } = useTheme();
if (!isAuthenticated) {
return showLogin ? (
<LoginScreen
onLogin={handleLogin}
onSwitchToRegister={() => setShowLogin(false)}
/>
) : (
<RegisterScreen
onRegister={handleLogin}
onSwitchToLogin={() => setShowLogin(true)}
/>
);
}
return (
<NavigationContainer>
<Stack.Navigator
initialRouteName="List"
screenOptions={{
headerStyle: {
backgroundColor: theme.colors.primary,
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: '600',
},
}}
>
<Stack.Screen
name="List"
options={({ navigation }: any) => ({
title: '我的日记',
headerLeft: () => (
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<TouchableOpacity
onPress={() => navigation.navigate('Settings')}
style={styles.headerButton}
testID="settings-button"
>
<Ionicons name="settings-outline" size={24} color="#fff" />
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate('Stats')}
style={styles.headerButton}
testID="stats-button"
>
<Ionicons name="stats-chart" size={24} color="#fff" />
</TouchableOpacity>
</View>
),
headerRight: () => (
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<TouchableOpacity
onPress={toggleTheme}
style={styles.headerButton}
testID="theme-toggle"
>
<Ionicons name={theme.isDark ? 'sunny-outline' : 'moon-outline'} size={24} color="#fff" />
</TouchableOpacity>
<TouchableOpacity
onPress={() => navigation.navigate('Edit')}
style={styles.headerButton}
testID="add-button"
>
<Ionicons name="add" size={24} color="#fff" />
</TouchableOpacity>
<TouchableOpacity
onPress={handleLogout}
style={styles.headerButton}
testID="logout-button"
>
<Ionicons name="log-out-outline" size={24} color="#fff" />
</TouchableOpacity>
</View>
),
})}
>
{(props) => <DiaryListScreen {...props} />}
</Stack.Screen>
<Stack.Screen
name="Stats"
options={{
title: '统计',
}}
>
{(props) => <StatsScreen {...props} />}
</Stack.Screen>
<Stack.Screen
name="Settings"
options={{
title: '设置',
}}
>
{(props) => <SettingsScreen {...props} />}
</Stack.Screen>
<Stack.Screen
name="Edit"
options={({ route }: any) => ({
title: route?.params?.entry ? '编辑日记' : '新日记',
})}
>
{(props) => <EditScreen {...props} />}
</Stack.Screen>
</Stack.Navigator>
</NavigationContainer>
);
};
export default function App() {
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [showLogin, setShowLogin] = useState(true);
useEffect(() => {
checkAuth();
}, []);
const checkAuth = async () => {
try {
const token = await tokenStorage.getItem('token');
setIsAuthenticated(!!token);
} catch (error) {
console.error('Auth check error:', error);
} finally {
setIsLoading(false);
}
};
const handleLogin = () => {
setIsAuthenticated(true);
};
const handleLogout = async () => {
try {
await authApi.logout();
setIsAuthenticated(false);
} catch (error) {
console.error('Logout error:', error);
}
};
if (isLoading) {
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<ActivityIndicator size="large" color="#007aff" />
</View>
);
}
return (
<ThemeProvider>
<AppContent
isAuthenticated={isAuthenticated}
showLogin={showLogin}
setShowLogin={setShowLogin}
handleLogin={handleLogin}
handleLogout={handleLogout}
/>
</ThemeProvider>
);
}
const styles = StyleSheet.create({
headerButton: {
marginRight: 16,
},
});