-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
242 lines (206 loc) · 8.79 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import 'react-native-gesture-handler';
import React, { useState, useEffect } from 'react';
import { Platform, StatusBar, Alert } from 'react-native'
import SplashScreen from 'react-native-splash-screen';
import * as Permissions from 'expo-permissions';
import * as Font from 'expo-font';
import auth from '@react-native-firebase/auth';
import { Provider as PaperProvider } from 'react-native-paper';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import theme from './src/styles/theme.styles'
import {BleManager} from 'react-native-ble-plx';
import firestore from '@react-native-firebase/firestore';
import messaging from '@react-native-firebase/messaging'
//Redux
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import thunk from 'redux-thunk'
import reducer from './src/reducers';
//Navigation
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { createDrawerNavigator } from '@react-navigation/drawer';
import NavigationDrawer from "./src/components/NavigationDrawer";
//Action imports
import { wifiListener, userCountry } from "./src/actions/uiActions";
import {receiveLogin, saveNewSubscription, setActivePlan} from "./src/actions/authActions";
//Screen imports
import HomeScreen from "./src/screens/HomeScreen";
import AccountScreen from "./src/screens/AccountScreen";
import AuthScreen from "./src/screens/AuthScreen";
import NewDeviceScreen from "./src/screens/NewDeviceScreen";
import ManageSubscriptions from "./src/screens/ManageSubscriptions";
import QRCodeScannerScreen from "./src/screens/QRCodeScannerScreen";
import LoadingScreen from "./src/screens/LoadingScreen"
import * as RNLocalize from "react-native-localize";
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
//BLE manager
const DeviceManager = new BleManager();
//In app purchases
import * as RNIap from 'react-native-iap'
import IAPManager from "./src/context/IAPManager";
import {useValidateIos} from "./src/context/IAPManager"
const itemSubs = Platform.select({
ios: [
'ffsubtier1',
'ffsubtier2',
'ffsubtier3',
'ffsubtier4'
],
android: [
'ffsubtier1',
'ffsubtier2',
'ffsubtier3',
'ffsubtier4'
]
})
//configure redux store
const middleware = applyMiddleware(thunk.withExtraArgument(DeviceManager));
export const store = createStore(reducer, middleware);
const MainFlow = () => {
return (
<Drawer.Navigator
initialRouteName="Home"
drawerContent={props => <NavigationDrawer {...props} />}>
<Drawer.Screen name="Home" component={HomeScreen} />
<Drawer.Screen name="Account" component={AccountScreen} />
</Drawer.Navigator>
);
};
export default App = () => {
const [appReady, setAppReady ] = useState(false);
const [authCheck, setAuthCheck ] = useState(false);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [isConnected, setIsConnected] = useState(false);
const getUserAccount = async (uid) => {
const userDoc = await firestore().collection('accounts').doc(uid).get();
if (userDoc._data){
let userData = userDoc._data;
userData.uid = uid;
setIsAuthenticated(true);
store.dispatch(receiveLogin(userData))
if(userData.subscriptionAndroid && Platform.OS === 'android') {
let subscription = userData.subscriptionAndroid;
store.dispatch(setActivePlan(subscription))
}
if(userData.subscriptionIos && Platform.OS === 'ios'){
let subscription = userData.subscriptionIos;
useValidateIos(subscription.latestReceipt).then(res => {
store.dispatch(saveNewSubscription(res.subscription, res.purchaseData))
})
}
if(!authCheck) setAuthCheck(true);
}
else
setTimeout(async () => {
await getUserAccount(uid)
}, 100)
}
const onAuthStateChanged = async (user) => {
if(user){
await getUserAccount(user.uid)
} else{
setIsAuthenticated(false);
if(!authCheck) setAuthCheck(true);
}
SplashScreen.hide();
}
//Listen to the redux store and initialize app if we are both connected to the internet and the app has not yet initialized
const connectedListener = store.subscribe(() => {
if(store.getState().ui.isConnected){
setIsConnected(true)
connectedListener() //Stop listening once app is initialize
}
})
useEffect( () => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
SplashScreen.hide();
//Get subscriptions
async function getSubs() {
try {
const subscriptions = await RNIap.getSubscriptions(itemSubs);
} catch(err) {
console.warn(err); // standardized err.code and err.message available
}
}
getSubs()
//Start listening for wifi
//Only when we have wifi will the app initialize
store.dispatch(wifiListener())
initApp()
const unsubscribe = messaging().onMessage(async remoteMessage => {
if(remoteMessage)
Alert.alert(remoteMessage.notification.title, remoteMessage.notification.body);
});
return unsubscribe;
}, [])
const initApp = async () => {
await Font.loadAsync({
'Montserrat': require('./assets/fonts/Montserrat-Regular.ttf'),
'Montserrat-light': require('./assets/fonts/Montserrat-Light.ttf'),
'Montserrat-bold': require('./assets/fonts/Montserrat-Bold.ttf'),
'Lato': require('./assets/fonts/Lato-Regular.ttf'),
'Lato-light': require('./assets/fonts/Lato-Light.ttf'),
'Lato-bold': require('./assets/fonts/Lato-Bold.ttf'),
});
setAppReady(true)
if(Platform.OS === 'android')
try {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status === 'granted') {
console.log("You can use location");
} else {
console.log("Location permission denied");
}
} catch (err) {
console.log(err)
}
const locales = await RNLocalize.getLocales()
if(locales[0])
store.dispatch(userCountry(locales[0].countryCode))
}
return (
<Provider store={store}>
<IAPManager>
<PaperProvider>
<StatusBar backgroundColor={theme.COLOR_PRIMARY} />
<SafeAreaProvider>
<NavigationContainer>
{ appReady && isConnected && authCheck ? (
<>
{ isAuthenticated ? (
<Stack.Navigator
initialRouteName="Main"
screenOptions={{
headerShown: false
}}>
<Stack.Screen name="Main" component={MainFlow} />
<Stack.Screen name="NewDevice" component={NewDeviceScreen} />
<Stack.Screen name="ManageSubscriptions" component={ManageSubscriptions} />
<Stack.Screen name="QRCodeScanner" component={QRCodeScannerScreen} />
</Stack.Navigator>
) : (
<Stack.Navigator
screenOptions={{
headerShown: false
}}>
<Stack.Screen name="Auth" component={AuthScreen} />
</Stack.Navigator>
)}
</>
) : (
<Stack.Navigator
screenOptions={{
headerShown: false
}}>
<Stack.Screen name="Loading" component={LoadingScreen} />
</Stack.Navigator>
)}
</NavigationContainer>
</SafeAreaProvider>
</PaperProvider>
</IAPManager>
</Provider>
)
}