forked from zehfernandes/wordnote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
220 lines (188 loc) · 5.6 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
import React, { useEffect, useState, useRef, useCallback } from "react";
import { AppState,UIManager, Platform, View, Text, Alert } from "react-native";
import { useFonts } from "expo-font";
import * as SplashScreen from "expo-splash-screen";
import i18n from "./lib/i18n";
import {
NavigationContainer,
DarkTheme,
DefaultTheme,
} from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import {
firstLaunch,
getAllData,
getSettings,
updateSettings,
clearData,
} from "./lib/appDB.js";
import { DatabaseInit } from "./lib/dictDB.js";
import Editor from "./pages/Editor";
import Notes from "./pages/Notes";
import Languages from "./pages/Languages";
import Settings from "./pages/Settings";
import About from "./pages/About";
import { LightThemeTokens, DarkThemeTokens } from "./assets/theme/tokens";
import * as FileSystem from 'expo-file-system';
const Stack = createNativeStackNavigator();
if (Platform.OS === "android") {
if (UIManager.setLayoutAnimationEnabledExperimental) {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
}
//TODO: Move Appereance to AppDB
let MyTheme = generateTheme(false);
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
export default function App() {
const db = useRef(null);
const [isDarkMode, setIsDarkMode] = useState(false);
const [globalData, setGlobalData] = useState({});
const [loadedDatabase, setLoadedDatabase] = useState(false);
const [loaded] = useFonts({
"iA Writer Duo": require("./assets/fonts/iAWriterDuoS-Regular.ttf"),
"iA Writer Duo Bold": require("./assets/fonts/iAWriterDuoS-Bold.ttf"),
"iA Writer Quattro Italic": require("./assets/fonts/iAWriterQuattroS-Italic.ttf"),
"iA Writer Quattro": require("./assets/fonts/iAWriterQuattroS-Regular.ttf"),
});
var [appState, setAppState] = React.useState(AppState.currentState);
var handleAppStateChange = (nextAppState) => {
if (nextAppState !== 'active') {
const analysisData = J$.analysis.endExecution();
FileSystem.writeAsStringAsync(FileSystem.documentDirectory + 'DCG.json', JSON.stringify(analysisData)).then(()=>{
alert('DCG written to '+ FileSystem.documentDirectory + 'DCG.json')
}).catch((e)=>{
alert(e)
});
}
}
useEffect(() => {
AppState.addEventListener('change', handleAppStateChange);
return () => {
AppState.removeEventListener('change', handleAppStateChange);
};
}, []);
useEffect(() => {
const fetchData = async () => {
try {
const v = await Promise.all([
getAllData(),
getSettings(),
firstLaunch(),
]);
const data = v[0];
const settings = v[1];
db.current = new DatabaseInit();
await db.current.init();
setGlobalData(data);
setLoadedDatabase(true);
updateTheme(settings.darkMode);
await SplashScreen.hideAsync();
} catch (e) {
console.log(e);
// Alert.alert(e);
}
};
fetchData();
}, []);
// Theme
const updateTheme = async (newValue) => {
MyTheme = generateTheme(newValue);
setIsDarkMode(newValue);
const settings = await getSettings();
settings.darkMode = newValue;
console.log(settings);
updateSettings(settings);
};
if (!loaded || !loadedDatabase) {
return null;
}
const commonProps = {
globalData,
setGlobalData,
i18n,
};
return (
<NavigationContainer theme={MyTheme}>
<Stack.Navigator>
<Stack.Screen
name="Notes"
options={{
title: "",
headerShadowVisible: false,
}}
>
{(props) => <Notes {...props} {...commonProps} />}
</Stack.Screen>
<Stack.Screen
name="Editor"
options={{
title: "",
headerBackTitleVisible: false,
headerShadowVisible: false,
}}
>
{(props) => (
<Editor {...props} {...commonProps} langsDB={db.current} />
)}
</Stack.Screen>
<Stack.Screen
name="Settings"
options={{
title: i18n.t("settings.title"),
headerShadowVisible: false,
}}
>
{(props) => (
<Settings
{...props}
isDarkMode={isDarkMode}
updateTheme={updateTheme}
{...commonProps}
/>
)}
</Stack.Screen>
<Stack.Screen
name="Settings.languages"
options={{
title: i18n.t("languages.title"),
headerShadowVisible: false,
presentation: "modal",
}}
>
{(props) => (
<Languages {...props} {...commonProps} langsDB={db.current} />
)}
</Stack.Screen>
<Stack.Screen
name="Settings.about"
options={{
title: i18n.t("about.title"),
headerShadowVisible: false,
presentation: "modal",
}}
>
{(props) => <About {...props} {...commonProps} />}
</Stack.Screen>
</Stack.Navigator>
</NavigationContainer>
);
}
// -----------------
// Helpers
// -----------------
function generateTheme(dark) {
const theme = dark ? DarkThemeTokens : LightThemeTokens;
const colorSpread = dark ? DarkTheme : DefaultTheme;
const MyTheme = {
...colorSpread,
colors: {
...colorSpread.colors,
primary: theme.primary,
card: theme.surface0,
background: theme.surface0,
backgroundLevel2: theme.surface1,
},
};
return MyTheme;
}