-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
185 lines (169 loc) · 6.94 KB
/
Copy pathApp.js
File metadata and controls
185 lines (169 loc) · 6.94 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
import { Ionicons } from '@expo/vector-icons';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { DarkTheme, DefaultTheme, NavigationContainer, createNavigationContainerRef } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import Constants from 'expo-constants';
import * as Notifications from 'expo-notifications';
import { StatusBar } from 'expo-status-bar';
import { useEffect } from 'react';
import {
AppState
} from 'react-native';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context';
import { registerBackgroundFetchAsync } from './src/services/BackgroundNotificationTask';
import ChiReadsScraper from './src/services/ChiReadsScraper';
import { StorageProvider } from './src/context/StorageContext';
import { ThemeProvider, useTheme } from './src/context/ThemeContext';
import FavoritesScreen from './src/screens/FavoritesScreen';
import HomeScreen from './src/screens/HomeScreen';
import LibraryScreen from './src/screens/LibraryScreen';
import NovelDetailScreen from './src/screens/NovelDetailScreen';
import ReaderScreen from './src/screens/ReaderScreen';
import SettingsScreen from './src/screens/SettingsScreen';
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
export const navigationRef = createNavigationContainerRef();
function MainTabs() {
const { theme, isDarkMode } = useTheme();
const insets = useSafeAreaInsets();
return (
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused, color, size }) => {
let iconName;
if (route.name === 'Home') {
iconName = focused ? 'home' : 'home-outline';
} else if (route.name === 'Favorites') {
iconName = focused ? 'heart' : 'heart-outline';
} else if (route.name === 'Library') {
iconName = focused ? 'library' : 'library-outline';
}
return <Ionicons name={iconName} size={size} color={color} />;
},
tabBarActiveTintColor: theme.tint,
tabBarInactiveTintColor: 'gray',
headerShown: false,
tabBarStyle: {
paddingBottom: insets.bottom + 5, // Dynamic padding
paddingTop: 10,
height: 60 + insets.bottom, // Dynamic height
backgroundColor: theme.tabBar,
borderTopWidth: 1,
borderTopColor: theme.tabBarBorder,
},
tabBarLabelStyle: {
fontSize: 12,
fontWeight: '600',
},
})}
>
<Tab.Screen
name="Home"
component={HomeScreen}
options={{
title: 'Accueil',
tabBarLabel: 'Accueil'
}}
/>
<Tab.Screen
name="Library"
component={LibraryScreen}
options={{
title: 'Bibliothèque',
tabBarLabel: 'Bibliothèque'
}}
/>
<Tab.Screen
name="Favorites"
component={FavoritesScreen}
options={{
title: 'Favoris',
tabBarLabel: 'Favoris'
}}
/>
</Tab.Navigator>
);
}
function AppContent() {
const { isDarkMode } = useTheme();
return (
<NavigationContainer ref={navigationRef} theme={isDarkMode ? DarkTheme : DefaultTheme}>
<Stack.Navigator screenOptions={{ headerShown: false }}>
<Stack.Screen name="Main" component={MainTabs} />
<Stack.Screen name="NovelDetail" component={NovelDetailScreen} />
<Stack.Screen name="Reader" component={ReaderScreen} />
<Stack.Screen name="Settings" component={SettingsScreen} />
</Stack.Navigator>
<StatusBar style={isDarkMode ? "light" : "dark"} />
</NavigationContainer>
);
}
export default function App() {
useEffect(() => {
const initNotifications = async () => {
// Expo Go SDK 53+ throws errors if we use Push Notifications APIs
// So we skip notification initialization completely in Expo Go.
if (Constants.appOwnership === 'expo') {
console.log('Running in Expo Go: Skipping Push Notifications init to avoid SDK 53+ errors.');
return;
}
try {
// 1. Request permissions
const { status } = await Notifications.requestPermissionsAsync();
if (status === 'granted') {
console.log('Notification permissions granted');
// 2. Register background task
await registerBackgroundFetchAsync();
} else {
console.log('Notification permissions denied');
}
} catch (err) {
console.warn('Notification init failed:', err);
}
};
const timer = setTimeout(initNotifications, 3000);
let subscription;
if (Constants.appOwnership !== 'expo') {
// Listener for notification interaction (tap)
subscription = Notifications.addNotificationResponseReceivedListener(response => {
const data = response.notification.request.content.data;
if (data && data.url) {
console.log('Notification tapped, navigating to:', data.url);
if (navigationRef.isReady()) {
navigationRef.navigate('NovelDetail', {
url: data.url,
title: data.title || 'Détails' // Fallback title
});
}
}
});
}
return () => {
clearTimeout(timer);
if (subscription) subscription.remove();
};
}, []);
// Clear chapter cache when App goes to background
useEffect(() => {
const subscription = AppState.addEventListener('change', nextAppState => {
if (nextAppState === 'background' || nextAppState === 'inactive') {
ChiReadsScraper.clearChapterCache();
}
});
return () => {
subscription.remove();
};
}, []);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<StorageProvider>
<ThemeProvider>
<SafeAreaProvider>
<AppContent />
</SafeAreaProvider>
</ThemeProvider>
</StorageProvider>
</GestureHandlerRootView>
);
}