-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
249 lines (233 loc) · 9.11 KB
/
Copy pathApp.tsx
File metadata and controls
249 lines (233 loc) · 9.11 KB
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
248
249
import 'react-native-gesture-handler';
import '~/global/buffer';
import '~/global/backgroundHandler';
import React, { createContext, useCallback, useEffect, useMemo, useState } from 'react';
import { StatusBar } from 'react-native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { createDrawerNavigator, DrawerContentComponentProps, DrawerNavigationOptions } from '@react-navigation/drawer';
import { DarkTheme as NavDarkTheme, NavigationContainer, RouteProp } from '@react-navigation/native';
import {
CardStyleInterpolators,
createStackNavigator,
StackHeaderProps,
StackNavigationOptions,
} from '@react-navigation/stack';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { Icon, MD3DarkTheme, Provider as PaperProvider, useTheme } from 'react-native-paper';
import Toast from 'react-native-toast-message';
import { Provider } from 'react-redux';
import ActiveCallBanner from '~/components/ActiveCallBanner';
import ConnectionIndicator from '~/components/ConnectionIndicator';
import Drawer from '~/components/Drawer';
import ErrorBoundary from '~/components/ErrorBoundary';
import ErrorPortal from '~/components/ErrorPortal';
import HeaderConversation from '~/components/HeaderConversation';
import { dbGetUnseenCallCount } from '~/global/database';
import { logger, showErrorPortal } from '~/global/logger';
import { FlagSecure } from '~/global/native';
import {
AuthStackParamList,
HomeStackParamList,
HomeTabParamList,
navigationRef,
RootDrawerParamList,
} from '~/global/navigation';
import { readFromStorage, StorageKeys } from '~/global/storage';
// App
import { ACCENT, DARKHEADER, DIVIDER, ERROR_RED, PRIMARY, SECONDARY, SECONDARY_LITE } from '~/global/variables';
import * as websocketManager from '~/global/websocketManager';
import DevTests from '~/pages/dev/DevTests';
import { store } from '~/store/store';
import {
AddContact,
Call,
CallHistory,
CameraView,
Conversation,
Home,
KeySetup,
Login,
NewConversation,
Settings,
Signup,
} from './src';
const defaultHeaderOptions: StackNavigationOptions & DrawerNavigationOptions = {
headerStyle: {
backgroundColor: DARKHEADER,
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
drawerIcon: ({ color }) => <Icon source="home" color={color} size={20} />,
};
const animationDefaults: StackNavigationOptions = {
cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
gestureEnabled: true,
gestureDirection: 'horizontal',
};
// Bottom tabs inside the drawer
const Tab = createBottomTabNavigator<HomeTabParamList>();
const renderMessagesIcon = ({ color, size }: { color: string; size: number }) => (
<Icon source="message-text" color={color} size={size} />
);
const renderCallsIcon = ({ color, size }: { color: string; size: number }) => (
<Icon source="phone" color={color} size={size} />
);
const HomeTabs = () => {
const { colors } = useTheme();
const [unseenCount, setUnseenCount] = useState(0);
const refreshBadge = useCallback(() => {
try {
setUnseenCount(dbGetUnseenCallCount());
} catch {
setUnseenCount(0);
}
}, []);
useEffect(() => {
refreshBadge();
}, [refreshBadge]);
return (
<Tab.Navigator
screenOptions={{
headerShown: false,
tabBarStyle: { backgroundColor: DARKHEADER, borderTopColor: DIVIDER },
tabBarActiveTintColor: colors.primary,
tabBarInactiveTintColor: SECONDARY_LITE,
tabBarBadgeStyle: { backgroundColor: ERROR_RED },
}}
screenListeners={{ state: refreshBadge }}
>
<Tab.Screen
name="Messages"
component={Home}
options={{
tabBarIcon: renderMessagesIcon,
}}
/>
<Tab.Screen
name="Calls"
component={CallHistory}
options={{
tabBarIcon: renderCallsIcon,
tabBarBadge: unseenCount > 0 ? unseenCount : undefined,
}}
/>
</Tab.Navigator>
);
};
const renderConnectionIndicator = () => <ConnectionIndicator />;
const AppNavigator = createDrawerNavigator<RootDrawerParamList>();
const AppDrawer = () => {
return (
<AppNavigator.Navigator screenOptions={{ swipeEdgeWidth: 200 }} drawerContent={renderDrawerContent}>
<AppNavigator.Screen
name="FoxTrot"
component={HomeTabs}
options={{ ...defaultHeaderOptions, headerRight: renderConnectionIndicator }}
/>
</AppNavigator.Navigator>
);
};
const renderDrawerContent = (props: DrawerContentComponentProps) => <Drawer {...props} />;
const HomeStack = createStackNavigator<HomeStackParamList>();
const HomeNavigator = () => {
useEffect(() => {
websocketManager.start();
return () => {
websocketManager.stop();
};
}, []);
return (
<HomeStack.Navigator initialRouteName="Home" screenOptions={{ ...defaultHeaderOptions, ...animationDefaults }}>
<HomeStack.Screen name="Home" component={AppDrawer} options={{ headerShown: false }} />
<HomeStack.Screen name="Conversation" component={Conversation} options={renderHeaderConversation} />
<HomeStack.Screen name="NewConversation" component={NewConversation} options={{ title: 'My Contacts' }} />
<HomeStack.Screen name="AddContact" component={AddContact} options={{ title: 'Search New Users' }} />
<HomeStack.Screen name="Call" component={Call as any} options={renderHeaderConversation} />
<HomeStack.Screen name="CameraView" component={CameraView} options={{ title: 'Camera' }} />
<HomeStack.Screen name="Settings" component={Settings} options={{ title: 'Settings' }} />
<HomeStack.Screen
name="KeySetup"
component={KeySetup}
options={{ title: 'Set Up Encryption', headerLeft: () => null }}
/>
{__DEV__ && (
<HomeStack.Screen name="DevTests" component={DevTests} options={{ title: 'Dev: Integration Tests' }} />
)}
</HomeStack.Navigator>
);
};
const renderHeaderConversation = ({
route,
}: {
route: RouteProp<HomeStackParamList, 'Call' | 'Conversation'>;
}): StackNavigationOptions => ({
header: (props: StackHeaderProps) => (
<HeaderConversation navigation={props.navigation as any} data={route.params.data} allowBack={true} />
),
});
const AuthStack = createStackNavigator<AuthStackParamList>();
const AuthNavigator = () => {
return (
<NavigationContainer ref={navigationRef} theme={NavDarkTheme}>
<AuthStack.Navigator screenOptions={{ ...defaultHeaderOptions, ...animationDefaults }}>
<AuthStack.Screen name="Login" component={Login} options={{ headerShown: false }} />
<AuthStack.Screen name="Signup" component={Signup} />
<AuthStack.Screen name="App" component={HomeNavigator} options={{ headerShown: false }} />
</AuthStack.Navigator>
</NavigationContainer>
);
};
export const PrimaryColorContext = createContext<(color: string) => void>(() => {});
export default function App() {
const [primaryColor, setPrimaryColor] = useState(PRIMARY);
const darkTheme = useMemo(
() => ({
...MD3DarkTheme,
colors: {
...MD3DarkTheme.colors,
primary: primaryColor,
onPrimary: '#fff',
background: SECONDARY,
accent: ACCENT,
},
}),
[primaryColor],
);
useEffect(() => {
readFromStorage(StorageKeys.SCREEN_SECURITY).then(val => {
if (val === 'true') {
FlagSecure.enable();
}
});
readFromStorage(StorageKeys.PRIMARY_COLOR).then(val => {
if (val) {
setPrimaryColor(val);
}
});
const defaultHandler = ErrorUtils.getGlobalHandler();
ErrorUtils.setGlobalHandler((error, isFatal) => {
logger.error(`Unhandled ${isFatal ? 'fatal ' : ''}error:`, error?.message, error?.stack);
showErrorPortal('Unhandled Error');
defaultHandler(error, isFatal);
});
}, []);
return (
<Provider store={store}>
<GestureHandlerRootView style={{ flex: 1 }}>
<PaperProvider theme={darkTheme}>
<PrimaryColorContext.Provider value={setPrimaryColor}>
<StatusBar backgroundColor={DARKHEADER} barStyle="light-content" />
<ErrorBoundary>
<AuthNavigator />
</ErrorBoundary>
<ActiveCallBanner />
<ErrorPortal />
<Toast />
</PrimaryColorContext.Provider>
</PaperProvider>
</GestureHandlerRootView>
</Provider>
);
}