-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.tsx
247 lines (213 loc) · 8.15 KB
/
App.tsx
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
243
244
245
246
247
import { NavigationContainer, NavigationProp } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import React, { useEffect, useState } from 'react';
import * as SecureStore from 'expo-secure-store';
import { ActivityIndicator, View } from 'react-native';
import { WelcomeScreen } from './src/screens/welcome.screen';
import { CreatorCoinHODLer, EventType, ToggleProfileManagerEvent, User } from './src/types';
import { settingsGlobals } from './src/globals/settingsGlobals';
import { themeStyles, updateThemeStyles } from './styles/globalColors';
import { globals } from './src/globals/globals';
import { constants } from './src/globals/constants';
import { AppNavigator } from './src/navigation/appNavigator';
import { SnackbarComponent } from './src/components/snackbarComponent';
import { cache, initCache } from './src/services/dataCaching/cache';
import { notificationsService } from './src/services/notificationsService';
import { LoginNavigator } from './src/navigation/loginNavigator';
import { ActionSheet } from './src/components/actionSheet.component';
import { DiamondAnimationComponent } from '@components/diamondAnimation.component';
import { cloutFeedApi } from '@services/cloutfeedApi';
import { enableScreens } from 'react-native-screens';
import { signing } from '@services/authorization/signing';
import { authentication } from '@services/authorization/authentication';
import { ProfileManagerComponent } from '@components/profileManager.component';
import { eventManager } from '@globals/injector';
import { StatusBar } from 'expo-status-bar';
enableScreens();
const Stack = createStackNavigator();
export default function App() {
const [isLoading, setLoading] = useState(true);
const [isLoggedIn, setLoggedIn] = useState(false);
const [areTermsAccepted, setTermsAccepted] = useState(false);
const [theme, setGlobalTheme] = useState('light');
const [showProfileManager, setShowProfileManager] = useState(false);
const [navigation, setNavigation] = useState<NavigationProp<any>>();
let mount = true;
useEffect(
() => {
checkAuthenticatedUser();
const unsubscribe = eventManager.addEventListener(
EventType.ToggleProfileManager,
(event: ToggleProfileManagerEvent) => {
if (mount) {
setShowProfileManager(event.visible);
setNavigation(event.navigation);
}
}
);
return () => {
unsubscribe();
};
},
[]
);
async function checkAuthenticatedUser() {
SecureStore.getItemAsync(constants.localStorage_publicKey).then(
async p_publicKey => {
if (p_publicKey) {
const cloutFeedIdentity = await SecureStore.getItemAsync(constants.localStorage_cloutFeedIdentity);
if (cloutFeedIdentity) {
globals.user.publicKey = p_publicKey;
const readonlyValue = await SecureStore.getItemAsync(constants.localStorage_readonly);
globals.readonly = readonlyValue !== 'false';
globals.onLoginSuccess();
} else {
await SecureStore.deleteItemAsync(constants.localStorage_publicKey);
await SecureStore.deleteItemAsync(constants.localStorage_readonly);
if (mount) {
setTermsAccepted(true);
setLoading(false);
}
}
} else {
const termsAccepted = await SecureStore.getItemAsync(constants.localStorage_termsAccepted);
if (mount) {
setTermsAccepted(termsAccepted === 'true');
setLoading(false);
}
}
}
).catch(() => { });
}
globals.acceptTermsAndConditions = () => {
if (mount) {
setTermsAccepted(true);
}
};
globals.setGlobalTheme = (p_theme: string) => {
setGlobalTheme(p_theme);
}
globals.onLoginSuccess = async () => {
cache.user.reset();
if (mount) {
setLoading(true);
}
Promise.all(
[
cache.user.getData()
]
).then(
async p_responses => {
setInvestorFeatures(p_responses[0]);
setFollowerFeatures(p_responses[0]);
globals.user.username = p_responses[0].ProfileEntryResponse?.Username;
if (globals.readonly === false) {
notificationsService.registerPushToken().catch(() => { });
}
initCache();
await setTheme();
}
).catch(() => { })
.finally(
() => {
if (mount) {
setLoggedIn(true);
setTermsAccepted(true);
setLoading(false);
}
}
);
}
globals.onLogout = async () => {
setLoading(true);
await SecureStore.deleteItemAsync(constants.localStorage_publicKey);
await SecureStore.deleteItemAsync(constants.localStorage_readonly);
if (globals.readonly === false) {
const jwt = await signing.signJWT();
cloutFeedApi.unregisterNotificationsPushToken(globals.user.publicKey, jwt).catch(() => { });
await authentication.removeAuthenticatedUser(globals.user.publicKey);
const loggedInPublicKeys = await authentication.getAuthenticatedUserPublicKeys();
if (loggedInPublicKeys?.length > 0) {
const publicKey = loggedInPublicKeys[0];
await SecureStore.setItemAsync(constants.localStorage_publicKey, publicKey);
await SecureStore.setItemAsync(constants.localStorage_readonly, 'false');
globals.user = { publicKey, username: '' };
globals.readonly = false;
globals.onLoginSuccess();
return;
}
}
globals.user.publicKey = '';
globals.investorFeatures = false;
globals.followerFeatures = false;
globals.readonly = true;
if (mount) {
setLoggedIn(false);
setTermsAccepted(true);
setLoading(false);
}
cache.user.reset();
};
function setInvestorFeatures(p_user: User) {
globals.investorFeatures = true;
const usersYouHODL = p_user.UsersYouHODL as CreatorCoinHODLer[];
const cloutFeedCoin = usersYouHODL?.find(p_user => p_user.CreatorPublicKeyBase58Check === constants.cloutfeed_publicKey);
if ((cloutFeedCoin && cloutFeedCoin.BalanceNanos > 30000000) || p_user.PublicKeyBase58Check === constants.cloutfeed_publicKey) {
cloutFeedCoin?.BalanceNanos
globals.investorFeatures = true;
}
}
function setFollowerFeatures(p_user: User) {
globals.followerFeatures = globals.user.publicKey === constants.cloutfeed_publicKey;
const followedByUserPublicKeys = p_user.PublicKeysBase58CheckFollowedByUser as string[];
if (followedByUserPublicKeys?.length > 0 && followedByUserPublicKeys.indexOf(constants.cloutfeed_publicKey) !== -1) {
globals.followerFeatures = true;
}
}
async function setTheme() {
settingsGlobals.darkMode = false;
if (globals.followerFeatures) {
const key = globals.user.publicKey + constants.localStorage_appearance;
const theme = await SecureStore.getItemAsync(key);
settingsGlobals.darkMode = theme === 'dark';
}
updateThemeStyles();
}
return isLoading ?
<View style={[{ flex: 1 }, themeStyles.containerColorMain]}>
<ActivityIndicator style={{ marginTop: 200 }} color={themeStyles.fontColorMain.color} />
</View>
:
<NavigationContainer>
<StatusBar style={settingsGlobals.darkMode ? 'light' : 'dark'} hidden={false}/>
<Stack.Navigator>
{
!isLoggedIn ?
areTermsAccepted ?
<Stack.Screen
options={{
headerShown: false
}}
name="LoginNavigator" component={LoginNavigator} />
:
<Stack.Screen name="Welcome" component={WelcomeScreen} />
:
<Stack.Screen
name="AppNavigator"
component={AppNavigator}
options={{
headerShown: false,
}}
/>
}
</Stack.Navigator >
<DiamondAnimationComponent></DiamondAnimationComponent>
<ActionSheet></ActionSheet>
<SnackbarComponent></SnackbarComponent>
{
showProfileManager ?
<ProfileManagerComponent navigation={navigation as NavigationProp<any>}></ProfileManagerComponent> : undefined
}
</NavigationContainer >
;
}