This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
188 lines (168 loc) · 6.4 KB
/
App.js
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
import React, { useEffect, useState, useRef } from 'react';
import {
StatusBar,
Image,
Platform,
ToastAndroid
} from 'react-native';
import Constants from 'expo-constants';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import colors from './assets/styles/colors';
import styles from './assets/styles/styles';
import { sf_api } from './config/secrets';
import { HomeScreen, ChapterScreen, MangaScreen, AboutScreen, MangasScreen, FollowsScreen, DownloadsScreen } from './components/screens';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { getFocusedRouteNameFromRoute } from '@react-navigation/native';
import * as Notifications from 'expo-notifications';
import * as Updates from 'expo-updates';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { post } from 'axios';
const Tab = createBottomTabNavigator();
const Stack = createStackNavigator();
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
function getTabIcon(route, focused) {
let icon_image;
switch (route) {
case "Mangas": icon_image = focused ? require(`./assets/img/listfilled_tabicon.png`) : require(`./assets/img/list_tabicon.png`); break;
case "Follows": icon_image = focused ? require(`./assets/img/bookmark_filled.png`) : require(`./assets/img/bookmark.png`); break;
case "Downloads": icon_image = focused ? require(`./assets/img/download_filled.png`) : require(`./assets/img/download.png`); break;
case "About": icon_image = focused ? require(`./assets/img/infofilled_tabicon.png`) : require(`./assets/img/info_tabicon.png`); break;
default: icon_image = focused ? require(`./assets/img/homefilled_tabicon.png`) : require(`./assets/img/home_tabicon.png`);
}
return <Image style={styles.tabIcon} source={icon_image} />;
}
function TabScreens({navigation, route}) {
useEffect(() => {
const routeName = getFocusedRouteNameFromRoute(route);
let options;
switch (routeName) {
case "About":
options = {
title: 'À PROPOS',
}
break;
case "Follows":
options = {
title: 'CHAPITRES SUIVIS',
};
break;
case "Downloads":
options = {
title: 'CHAPITRES TÉLÉCHARGÉS',
};
break;
case "Mangas":
options = {
title: 'LISTE DES MANGAS',
};
break;
default:
options = {
title: 'DERNIERS CHAPITRES',
};
}
navigation.setOptions(options);
});
return (
<Tab.Navigator
screenOptions={({ route }) => ({
tabBarIcon: ({ focused }) => getTabIcon(route.name, focused),
})}
tabBarOptions={{
tabStyle: styles.tabContainer,
showLabel: false
}}
>
<Tab.Screen name="Home" component={HomeScreen} />
<Tab.Screen name="Follows" component={FollowsScreen} />
<Tab.Screen name="Downloads" component={DownloadsScreen} />
<Tab.Screen name="Mangas" component={MangasScreen} />
<Tab.Screen name="About" component={AboutScreen} options={{ title: 'À propos' }} />
</Tab.Navigator>
);
}
const App = () => {
const [token, setToken] = useState('');
useEffect(() => {
Image.resolveAssetSource({uri: './assets/img/homefilled_tabicon.png'});
Image.resolveAssetSource({uri: './assets/img/home_tabicon.png'});
Image.resolveAssetSource({uri: './assets/img/listfilled_tabicon.png'});
Image.resolveAssetSource({uri: './assets/img/list_tabicon.png'});
Image.resolveAssetSource({uri: './assets/img/infofilled_tabicon.png'});
Image.resolveAssetSource({uri: './assets/img/info_tabicon.png'});
Image.resolveAssetSource({uri: './assets/img/bookmark_filled.png'});
Image.resolveAssetSource({uri: './assets/img/bookmark.png'});
Image.resolveAssetSource({uri: './assets/img/download_filled.png'});
Image.resolveAssetSource({uri: './assets/img/download.png'});
checkUpdate();
initPushNotifs();
}, []);
useEffect(() => {
AsyncStorage.setItem('token', token).catch(() => {});
}, [token]);
const checkUpdate = async () => {
try {
const update = await Updates.checkForUpdateAsync().catch(() => {});
if (update.isAvailable) {
ToastAndroid.show("Mise à jour de l'application...", ToastAndroid.SHORT);
await Updates.fetchUpdateAsync().catch(() => {});
await Updates.reloadAsync().catch(() => {});
}
} catch (_) {}
};
const initPushNotifs = async () => {
registerForPushNotificationsAsync().then(token => {
setToken(token);
post(sf_api.url + "users/token",
{ token: token },
{ headers: { Authorization: `Bearer ${sf_api.token}` }}
).catch(() => {});
}).catch(() => {});
};
async function registerForPushNotificationsAsync() {
let token;
// Must use physical device for Push Notifications
if (Constants.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
// Failed to get push token for push notification!
if (finalStatus !== 'granted') return;
token = ((await Notifications.getExpoPushTokenAsync()) || {}).data;
}
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
return token;
}
return (
<NavigationContainer theme={{ colors: { background: colors.background }}}>
<StatusBar barStyle="light-content" hidden={true} animated={true} translucent={true} backgroundColor="transparent" />
<Stack.Navigator screenOptions={{ headerStyle: { backgroundColor: colors.primary, borderBottomWidth: 3, borderBottomColor: colors.orange, elevation: 0, shadowOpacity: 0 }, headerTintColor: colors.white, headerTitleStyle: { fontSize: 18, fontWeight: "bold" } }}>
<Stack.Screen name="Home" component={TabScreens} />
<Stack.Screen name="Chapter" component={ChapterScreen} options={({ route }) => ({
title: route.params.chapter.number + " - " + route.params.chapter.manga.name.slice(0, 13) + (route.params.chapter.manga.name.length > 13 ? "..." : "" ),
})} />
<Stack.Screen name="Manga" component={MangaScreen} options={({ route }) => ({
title: route.params.manga.name
})} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;