-
Notifications
You must be signed in to change notification settings - Fork 3
/
App.js
187 lines (166 loc) · 5.59 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
import * as React from 'react';
import DeviceInfo from 'react-native-device-info';
import FlashMessage from 'react-native-flash-message';
import Toast from 'react-native-toast-message';
import {BackHandler, Platform, View} from 'react-native';
import {HumanIDProvider} from '@human-internet/react-native-humanid';
import {LogLevel, OneSignal} from 'react-native-onesignal';
import {NavigationContainer, DefaultTheme} from '@react-navigation/native';
import {OverlayProvider, Streami18n} from 'stream-chat-react-native';
import {RecoilDebugObserver} from 'reactotron-recoil-plugin';
import {RecoilRoot} from 'recoil';
import {
SafeAreaProvider,
useSafeAreaFrame,
useSafeAreaInsets
} from 'react-native-safe-area-context';
import {appUpgradeVersionCheck} from 'app-upgrade-react-native-sdk';
import {QueryClient, QueryClientProvider} from 'react-query';
import Store from './src/context/Store';
import getFeatureLoggerInstance, {EFeatureLogFlag} from './src/utils/log/FeatureLog';
import {APP_UPGRADE_API_KEY, ENV, ONE_SIGNAL_APP_ID} from './src/libraries/Configs/ENVConfig';
import {Analytics} from './src/libraries/analytics/firebaseAnalytics';
import {RootNavigator} from './src/navigations/root-stack';
import {fetchRemoteConfig} from './src/utils/FirebaseUtil';
import {linking} from './src/navigations/linking';
import {reactotronInstance} from './src/libraries/reactotron/reactotronInstance';
import {toastConfig} from './src/configs/ToastConfig';
import {COLORS} from './src/utils/theme';
const {featLog} = getFeatureLoggerInstance(EFeatureLogFlag.navigation);
const App = () => {
const {top, bottom} = useSafeAreaInsets();
const {height} = useSafeAreaFrame();
const streami18n = new Streami18n({
language: 'en'
});
const navigationRef = React.useRef();
const routeNameRef = React.useRef();
const [currentScreen, setCurrentScreen] = React.useState('InitialScreenName');
const navTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: COLORS.almostBlack
}
};
React.useEffect(() => {
const init = async () => {
try {
fetchRemoteConfig();
} catch (error) {
if (__DEV__) {
console.log('app ', error);
}
}
};
init();
// return unsubscribe;
}, []);
React.useEffect(() => {
appUpgradeVersionCheck(appInfo, APP_UPGRADE_API_KEY, alertConfig);
}, [currentScreen]);
const preventCloseApp = () => true;
const backFunc = () => {
navigationRef.current.goBack();
return true;
};
const handleStateChange = () => {
const isCanBack = navigationRef.current.canGoBack();
if (!isCanBack) {
BackHandler.removeEventListener('hardwareBackPress', backFunc);
BackHandler.addEventListener('hardwareBackPress', preventCloseApp);
} else {
BackHandler.removeEventListener('hardwareBackPress', preventCloseApp);
BackHandler.addEventListener('hardwareBackPress', backFunc);
}
// log event
const previousRouteName = routeNameRef.current;
const currentRouteName = navigationRef.current?.getCurrentRoute?.()?.name;
if (Platform.OS === 'ios') {
setCurrentScreen(currentRouteName);
}
if (currentRouteName && previousRouteName !== currentRouteName) {
Analytics.trackingScreen(currentRouteName);
}
if (__DEV__) {
featLog('current screen name: ', currentRouteName);
console.tron.log('current screen name: ', currentRouteName);
}
routeNameRef.current = currentRouteName;
};
const onReadyState = () => {
if (navigationRef.current) {
routeNameRef.current = navigationRef.current?.getCurrentRoute?.()?.name;
}
};
const appInfo = {
appId: Platform.OS === 'ios' ? '1615684520' : 'org.bettersocial', // Your app id in play store or app store
appName: DeviceInfo.getApplicationName(), // Your app name
appVersion: DeviceInfo.getVersion(), // Your app version
platform: Platform.OS, // App Platform, android or ios
environment: ENV // App Environment, production, development
};
// Alert config is optional
const alertConfig = {
title: 'Update Notice',
updateButtonTitle: 'Update Now',
laterButtonTitle: 'Later',
onDismissCallback: () => {
if (__DEV__) {
console.log('Dismiss');
}
},
onLaterCallback: () => {
if (__DEV__) {
console.log('Later');
}
}
};
// Remove this method to stop OneSignal Debugging
OneSignal.Debug.setLogLevel(ENV === 'Prod' ? LogLevel.None : LogLevel.Verbose);
// OneSignal Initialization
OneSignal.initialize(ONE_SIGNAL_APP_ID);
return (
<>
<HumanIDProvider />
{/* <RealmProvider> */}
<RecoilRoot>
{__DEV__ ? <RecoilDebugObserver instance={reactotronInstance} /> : null}
<Store>
<NavigationContainer
theme={navTheme}
onReady={onReadyState}
onStateChange={handleStateChange}
ref={navigationRef}
linking={linking}>
<View>
<RootNavigator areaHeight={height} currentScreen={currentScreen} />
</View>
</NavigationContainer>
</Store>
</RecoilRoot>
{/* </RealmProvider> */}
<Toast config={toastConfig} />
<FlashMessage position="top" />
</>
);
};
const RootApp = () => {
const queryClient = new QueryClient();
return (
<SafeAreaProvider
initialMetrics={{
insets: {
top: 0,
right: 0,
bottom: 0,
left: 0
}
}}>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</SafeAreaProvider>
);
};
export default RootApp;