This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
App.tsx
167 lines (152 loc) · 4.88 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
import Bugsnag from '@bugsnag/react-native';
import { ActionSheetProvider } from '@expo/react-native-action-sheet';
import { MobileApplication } from '@Lib/application';
import { ApplicationGroup } from '@Lib/application_group';
import { navigationRef } from '@Lib/navigation_service';
import { DefaultTheme, NavigationContainer } from '@react-navigation/native';
import { DeinitSource } from '@standardnotes/snjs';
import { MobileThemeVariables } from '@Style/Themes/styled-components';
import { ThemeService, ThemeServiceContext } from '@Style/theme_service';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { StatusBar } from 'react-native';
import { ThemeProvider } from 'styled-components/native';
import { ApplicationContext } from './ApplicationContext';
import { MainStackComponent } from './ModalStack';
export type HeaderTitleParams = {
title?: string;
subTitle?: string;
subTitleColor?: string;
};
const AppComponent: React.FC<{
application: MobileApplication;
env: 'prod' | 'dev';
}> = ({ application, env }) => {
const themeService = useRef<ThemeService>();
const appReady = useRef(false);
const navigationReady = useRef(false);
const [activeTheme, setActiveTheme] = useState<
MobileThemeVariables | undefined
>();
const setThemeServiceRef = useCallback((node: ThemeService | undefined) => {
if (node) {
node.addThemeChangeObserver(() => {
setActiveTheme(node.variables);
});
}
/**
* We check if both application and navigation are ready and launch application afterwads
*/
themeService.current = node;
}, []);
/**
* We check if both application and navigation are ready and launch application afterwads
*/
const launchApp = useCallback(
(setAppReady: boolean, setNavigationReady: boolean) => {
if (setAppReady) {
appReady.current = true;
}
if (setNavigationReady) {
navigationReady.current = true;
}
if (navigationReady.current && appReady.current) {
application.launch();
}
},
[application]
);
useEffect(() => {
let themeServiceInstance: ThemeService;
const loadApplication = async () => {
themeServiceInstance = new ThemeService(application);
setThemeServiceRef(themeServiceInstance);
await application?.prepareForLaunch({
receiveChallenge: async challenge => {
application!.promptForChallenge(challenge);
},
});
await themeServiceInstance.init();
launchApp(true, false);
};
loadApplication();
return () => {
themeServiceInstance?.deinit();
setThemeServiceRef(undefined);
if (!application.hasStartedDeinit()) {
application.deinit(DeinitSource.Lock);
}
};
}, [application, application.Uuid, env, launchApp, setThemeServiceRef]);
if (!themeService.current || !activeTheme) {
return null;
}
return (
<NavigationContainer
onReady={() => launchApp(false, true)}
theme={{
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: activeTheme.stylekitBackgroundColor,
border: activeTheme.stylekitBorderColor,
},
}}
ref={navigationRef}
>
<StatusBar translucent />
{themeService.current && (
<>
<ThemeProvider theme={activeTheme}>
<ActionSheetProvider>
<ThemeServiceContext.Provider value={themeService.current}>
<MainStackComponent env={env} />
</ThemeServiceContext.Provider>
</ActionSheetProvider>
</ThemeProvider>
</>
)}
</NavigationContainer>
);
};
/**
* AppGroupInstance is only created once per application lifetime
* so it is created outside of a component
*/
const AppGroupInstance = new ApplicationGroup();
AppGroupInstance.initialize();
export const App = (props: { env: 'prod' | 'dev'; bugsnagOptOut: true }) => {
const applicationGroupRef = useRef(AppGroupInstance);
const [application, setApplication] = useState<
MobileApplication | undefined
>();
useEffect(() => {
if (!props.bugsnagOptOut) {
try {
Bugsnag.start();
} catch {
console.error('Bugnsag failed to start');
}
}
}, [props.bugsnagOptOut]);
useEffect(() => {
const removeAppChangeObserver = applicationGroupRef.current.addApplicationChangeObserver(
() => {
const mobileApplication = applicationGroupRef.current
.primaryApplication as MobileApplication;
setApplication(mobileApplication);
}
);
return removeAppChangeObserver;
}, [applicationGroupRef.current.primaryApplication]);
return (
<ApplicationContext.Provider value={application}>
{application && (
<AppComponent
env={props.env}
key={application.Uuid}
application={application}
/>
)}
</ApplicationContext.Provider>
);
};