This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
App.js
259 lines (231 loc) · 6.42 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, { useState, useEffect, useRef } from 'react'
import { StatusBar, AsyncStorage } from 'react-native'
import { useColorScheme, AppearanceProvider } from 'react-native-appearance'
import { SplashScreen } from 'expo'
import * as Font from 'expo-font'
import {
configureFonts,
DarkTheme,
DefaultTheme,
Provider as PaperProvider
} from 'react-native-paper'
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation'
import { getUserInfo, updateEditorPreferences } from './lib/network'
import CustomHeader from './components/ui/CustomHeader'
import SettingsContext from './components/wrappers/SettingsContext'
import InitialScreen from './screens/Initial'
import WelcomeScreen from './screens/auth/Welcome'
import LogInScreen from './screens/auth/LogIn'
import SignUpScreen from './screens/auth/SignUp'
import HelloScreen from './screens/auth/Hello'
import GoogleProviderScreen from './screens/auth/providers/Google'
import GitHubProviderScreen from './screens/auth/providers/GitHub'
import FacebookProviderScreen from './screens/auth/providers/Facebook'
import DashboardScreen from './screens/app/Dashboard'
import SettingsScreen from './screens/app/Settings'
import LoadReplScreen from './screens/app/LoadRepl'
import ReplScreen from './screens/app/Repl'
import FileScreen from './screens/app/File'
const AuthNavigator = createStackNavigator(
{
Welcome: WelcomeScreen,
LogIn: LogInScreen,
SignUp: SignUpScreen,
Hello: HelloScreen,
GoogleProvider: GoogleProviderScreen,
GitHubProvider: GitHubProviderScreen,
FacebookProvider: FacebookProviderScreen
},
{
initialRouteName: 'Welcome',
defaultNavigationOptions: {
header: (props) => <CustomHeader {...props} />
}
}
)
const AppNavigator = createStackNavigator(
{
Dashboard: DashboardScreen,
Settings: SettingsScreen,
LoadRepl: LoadReplScreen,
Repl: ReplScreen,
File: FileScreen
},
{
initialRouteName: 'Dashboard',
defaultNavigationOptions: {
header: (props) => <CustomHeader {...props} />
}
}
)
const Navigator = createSwitchNavigator(
{
Initial: createStackNavigator(
{
Initial: InitialScreen
},
{
defaultNavigationOptions: {
header: (props) => <CustomHeader {...props} />
}
}
),
Auth: AuthNavigator,
App: AppNavigator
},
{ initialRouteName: 'Initial' }
)
const App = createAppContainer(Navigator)
const roundness = 2
const allFonts = {
regular: {
fontFamily: 'IBM Plex Sans',
fontWeight: 'normal'
},
medium: {
fontFamily: 'IBM Plex Sans Medium',
fontWeight: 'normal'
},
light: {
fontFamily: 'IBM Plex Sans Light',
fontWeight: 'normal'
},
thin: {
fontFamily: 'IBM Plex Sans Thin',
fontWeight: 'normal'
}
}
const fonts = configureFonts({
default: allFonts,
ios: allFonts,
android: allFonts
})
const darkTheme = {
...DarkTheme,
roundness,
fonts,
colors: {
...DarkTheme.colors,
primary: '#ffffff',
accent: '#455a64'
}
}
const lightTheme = {
...DefaultTheme,
roundness,
fonts,
colors: {
...DefaultTheme.colors,
primary: '#222222',
accent: '#222222'
}
}
const updateSettings = async (settings) => {
const {
user: { editor_preferences }
} = await getUserInfo()
await updateEditorPreferences({
...editor_preferences,
theme: settings.theme,
wrapping: settings.softWrapping,
indentIsSpaces: settings.softTabs,
indentSize: settings.indentSize
})
await AsyncStorage.setItem('@useSystemTheme', settings.systemTheme ? 'yes' : 'no')
}
const systemThemeToTheme = (systemTheme) => {
switch (systemTheme) {
case 'dark': {
return 'replitDark'
}
default: {
return 'replitLight'
}
}
}
const Main = () => {
const [theme, setTheme] = useState('replitDark')
const [systemTheme, setSystemTheme] = useState(true)
const scheme = useColorScheme()
const [softWrapping, setSoftWrapping] = useState(false)
const [softTabs, setSoftTabs] = useState(true)
const [indentSize, setIndentSize] = useState(2)
const [redirectRoute, setRedirectRoute] = useState(null)
const finalTheme = systemTheme ? systemThemeToTheme(scheme) : theme
const firstUpdate = useRef(true)
useEffect(() => {
if (firstUpdate.current) {
firstUpdate.current = false
return
}
try {
updateSettings({
theme,
softWrapping,
softTabs,
indentSize,
systemTheme
})
} catch (error) {
// Unhandled on purpose
}
}, [theme, softWrapping, softTabs, indentSize, systemTheme])
useEffect(() => {
;(async () => {
SplashScreen.preventAutoHide()
await Font.loadAsync({
Inconsolata: require('./assets/fonts/Inconsolata-Regular.ttf'),
'IBM Plex Sans': require('./assets/fonts/IBMPlexSans-Regular.ttf'),
'IBM Plex Sans Medium': require('./assets/fonts/IBMPlexSans-Medium.ttf'),
'IBM Plex Sans Light': require('./assets/fonts/IBMPlexSans-Light.ttf'),
'IBM Plex Sans Thin': require('./assets/fonts/IBMPlexSans-Thin.ttf')
})
setSystemTheme((await AsyncStorage.getItem('@useSystemTheme')) !== 'no')
const { success, user } = await getUserInfo()
if (success) {
const {
editor_preferences: { theme, indentIsSpaces, indentSize, wrapping }
} = user
setTheme(theme)
setSoftTabs(indentIsSpaces)
setIndentSize(indentSize.toString())
setSoftWrapping(wrapping)
setRedirectRoute('App')
} else setRedirectRoute('Auth')
SplashScreen.hide()
})()
}, [])
return (
<PaperProvider theme={finalTheme === 'replitDark' ? darkTheme : lightTheme}>
<StatusBar barStyle="light-content" />
<SettingsContext.Provider
value={{
theme,
setTheme,
systemTheme,
setSystemTheme,
softWrapping,
setSoftWrapping,
softTabs,
setSoftTabs,
indentSize,
setIndentSize,
redirectRoute,
updateSettings
}}
>
{console.log(
`rendering, theme=${theme}, system scheme=${scheme}, using ${
systemTheme ? 'system theme' : 'repl.it theme'
}, final theme=${finalTheme}`
)}
{redirectRoute && <App />}
</SettingsContext.Provider>
</PaperProvider>
)
}
export default () => (
<AppearanceProvider>
<Main />
</AppearanceProvider>
)