|
| 1 | +/* |
| 2 | + * This is the source code of Moon Meet CrossPlatform. |
| 3 | + * It is licensed under GNU GPL v. 3. |
| 4 | + * You should have received a copy of the license in this archive (see LICENSE). |
| 5 | + * |
| 6 | + * Copyright Rayen sbai, 2021-2022. |
| 7 | + */ |
| 8 | + |
| 9 | +import React, {useEffect} from 'react'; |
| 10 | +import StackNavigator from './config/Stack'; |
| 11 | +import {StatusBar, StyleSheet} from 'react-native'; |
| 12 | +import Toast from 'react-native-toast-message'; |
| 13 | +import {COLORS} from './config/Miscellaneous'; |
| 14 | +import {DefaultTheme, Provider as PaperProvider} from 'react-native-paper'; |
| 15 | +import {ThemeContext} from 'config/Theme/Context.ts'; |
| 16 | +import {StorageInstance} from 'config/MMKV/StorageInstance.ts'; |
| 17 | +import {MoonMeetDarkTheme} from 'config/Theme/Theme.ts'; |
| 18 | +import {GestureHandlerRootView} from 'react-native-gesture-handler'; |
| 19 | +import {BottomSheetModalProvider} from '@gorhom/bottom-sheet'; |
| 20 | +import crashlytics from '@react-native-firebase/crashlytics'; |
| 21 | +import analytics from '@react-native-firebase/analytics'; |
| 22 | +import appCheck from '@react-native-firebase/app-check'; |
| 23 | +import OneSignal from 'react-native-onesignal'; |
| 24 | +import {enableLayoutAnimations} from 'react-native-reanimated'; |
| 25 | +import {FIREBASE_APPCHECK_DEBUG_TOKEN} from './secrets/sensitive'; |
| 26 | + |
| 27 | +/** |
| 28 | + * It enables the firebase tools. |
| 29 | + * |
| 30 | + * @async |
| 31 | + * @function |
| 32 | + */ |
| 33 | + |
| 34 | +async function firebaseToolsEnablement() { |
| 35 | + try { |
| 36 | + const crashlyticsInstance = crashlytics(); |
| 37 | + const analyticsInstance = analytics(); |
| 38 | + |
| 39 | + if (crashlyticsInstance) { |
| 40 | + await crashlyticsInstance.setCrashlyticsCollectionEnabled(!__DEV__); |
| 41 | + } |
| 42 | + |
| 43 | + if (analyticsInstance) { |
| 44 | + await analyticsInstance.setAnalyticsCollectionEnabled(!__DEV__); |
| 45 | + } |
| 46 | + } catch (err) { |
| 47 | + if (__DEV__) { |
| 48 | + console.error('Failed to enable Firebase tools: ', err); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * It enables the firebase tools. |
| 55 | + * |
| 56 | + * @async |
| 57 | + * @function |
| 58 | + */ |
| 59 | +async function appCheckInitialization(rnFbProvider: any) { |
| 60 | + try { |
| 61 | + await appCheck().initializeAppCheck({ |
| 62 | + provider: rnFbProvider, |
| 63 | + isTokenAutoRefreshEnabled: true, |
| 64 | + }); |
| 65 | + } catch (err) { |
| 66 | + if (__DEV__) { |
| 67 | + console.error('Failed to initialize Firebase App Check: ', err); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const GHRVStyles = StyleSheet.create({ |
| 73 | + light: { |
| 74 | + flex: 1, |
| 75 | + flexGrow: 1, |
| 76 | + backgroundColor: COLORS.primaryLight, |
| 77 | + }, |
| 78 | + dark: { |
| 79 | + flex: 1, |
| 80 | + flexGrow: 1, |
| 81 | + backgroundColor: COLORS.primaryDark, |
| 82 | + }, |
| 83 | +}); |
| 84 | + |
| 85 | +/** |
| 86 | + * Play integrity initialisation |
| 87 | + */ |
| 88 | +let rnFbProvider: any = appCheck().newReactNativeFirebaseAppCheckProvider(); |
| 89 | +rnFbProvider.configure({ |
| 90 | + android: { |
| 91 | + provider: __DEV__ ? 'debug' : 'playIntegrity', |
| 92 | + debugToken: FIREBASE_APPCHECK_DEBUG_TOKEN, |
| 93 | + }, |
| 94 | + apple: { |
| 95 | + provider: __DEV__ ? 'debug' : 'appAttestWithDeviceCheckFallback', |
| 96 | + debugToken: 'NO-TOKEN', |
| 97 | + }, |
| 98 | + web: { |
| 99 | + provider: 'reCaptchaV3', |
| 100 | + siteKey: 'NO-TOKEN', |
| 101 | + }, |
| 102 | +}); |
| 103 | + |
| 104 | +/** |
| 105 | + * Disabling layoutAnimations fixes React Navigation Header. |
| 106 | + */ |
| 107 | +enableLayoutAnimations(false); |
| 108 | + |
| 109 | +const App = () => { |
| 110 | + const [isThemeDark, setIsThemeDark] = React.useState( |
| 111 | + StorageInstance.contains('isThemeDark') |
| 112 | + ? StorageInstance.getBoolean('isThemeDark') === true |
| 113 | + : false, |
| 114 | + ); |
| 115 | + |
| 116 | + useEffect(() => { |
| 117 | + appCheckInitialization(rnFbProvider); |
| 118 | + (async () => { |
| 119 | + await firebaseToolsEnablement(); |
| 120 | + })(); |
| 121 | + }, []); |
| 122 | + |
| 123 | + useEffect(() => { |
| 124 | + OneSignal.getDeviceState().then(deviceState => { |
| 125 | + if (deviceState !== null) { |
| 126 | + let {isSubscribed} = deviceState; |
| 127 | + if (isSubscribed) { |
| 128 | + OneSignal.addTrigger('unsubscribed', 'false'); |
| 129 | + } else { |
| 130 | + OneSignal.promptForPushNotificationsWithUserResponse(true); |
| 131 | + } |
| 132 | + } |
| 133 | + }); |
| 134 | + }, []); |
| 135 | + |
| 136 | + let theme = isThemeDark ? MoonMeetDarkTheme : DefaultTheme; |
| 137 | + |
| 138 | + const toggleTheme = React.useCallback(() => { |
| 139 | + return setIsThemeDark(!isThemeDark); |
| 140 | + }, [isThemeDark]); |
| 141 | + |
| 142 | + const themePreferences = React.useMemo( |
| 143 | + () => ({ |
| 144 | + toggleTheme, |
| 145 | + isThemeDark, |
| 146 | + }), |
| 147 | + [toggleTheme, isThemeDark], |
| 148 | + ); |
| 149 | + |
| 150 | + useEffect(() => { |
| 151 | + if (StorageInstance.contains('isThemeDark')) { |
| 152 | + if (StorageInstance.getBoolean('isThemeDark')) { |
| 153 | + if (!isThemeDark) { |
| 154 | + toggleTheme(); |
| 155 | + } |
| 156 | + } else { |
| 157 | + StorageInstance.set('isThemeDark', false); |
| 158 | + } |
| 159 | + } else { |
| 160 | + if (isThemeDark) { |
| 161 | + toggleTheme(); |
| 162 | + StorageInstance.set('isThemeDark', false); |
| 163 | + } |
| 164 | + } |
| 165 | + }, [isThemeDark, toggleTheme]); |
| 166 | + |
| 167 | + return ( |
| 168 | + <> |
| 169 | + <StatusBar |
| 170 | + backgroundColor={isThemeDark ? COLORS.primaryDark : COLORS.primaryLight} |
| 171 | + animated={true} |
| 172 | + barStyle={isThemeDark ? 'light-content' : 'dark-content'} |
| 173 | + /> |
| 174 | + <ThemeContext.Provider value={themePreferences}> |
| 175 | + <PaperProvider theme={theme}> |
| 176 | + <GestureHandlerRootView |
| 177 | + style={isThemeDark ? GHRVStyles.dark : GHRVStyles.light}> |
| 178 | + <BottomSheetModalProvider> |
| 179 | + <StackNavigator /> |
| 180 | + <Toast /> |
| 181 | + </BottomSheetModalProvider> |
| 182 | + </GestureHandlerRootView> |
| 183 | + </PaperProvider> |
| 184 | + </ThemeContext.Provider> |
| 185 | + </> |
| 186 | + ); |
| 187 | +}; |
| 188 | + |
| 189 | +export default App; |
0 commit comments