-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
147 lines (130 loc) · 4.26 KB
/
Copy pathApp.tsx
File metadata and controls
147 lines (130 loc) · 4.26 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
import "react-native-get-random-values";
import { decode, encode } from "base-64";
import {
DarkTheme as NavigationDarkTheme,
DefaultTheme as NavigationDefaultTheme,
} from "@react-navigation/native";
import { StatusBar } from "expo-status-bar";
import databasePromise from "model/database";
import { SettingSchema } from "model/setting.type";
import React, { useEffect, useState } from "react";
import {
DarkTheme as PaperDarkTheme,
DefaultTheme as PaperDefaultTheme,
Provider as PaperProvider,
} from "react-native-paper";
import { SafeAreaProvider } from "react-native-safe-area-context";
import { enableScreens } from "react-native-screens";
import { fonts as fontList } from "./fontconfig";
import useCachedResources from "./hooks/useCachedResources";
import Navigation from "./navigation";
import { PreferencesProvider } from "./PreferencesContext";
import { getSetting, SettingId } from "./model/setting.schema";
//Nooit NodeJS modules gebruiken
process.browser = true;
const ExtraTheme = {
colors: { textLight: "#ffffff", textDark: "#000000" },
};
const CombinedDefaultTheme = {
...PaperDefaultTheme,
...NavigationDefaultTheme,
colors: {
...PaperDefaultTheme.colors,
...NavigationDefaultTheme.colors,
...ExtraTheme.colors,
},
fonts: fontList,
};
const CombinedDarkTheme = {
...PaperDarkTheme,
...NavigationDarkTheme,
colors: {
...PaperDarkTheme.colors,
...NavigationDarkTheme.colors,
...ExtraTheme.colors,
},
fonts: fontList,
};
// Polyfill voor React Native
if (!global.btoa) {
global.btoa = encode;
}
if (!global.atob) {
global.atob = decode;
}
enableScreens();
export default function App(): JSX.Element {
const isLoadingComplete = useCachedResources();
const [isThemeDark, setIsThemeDark] = useState(false);
const [accentColor, setAccentColor] = useState("#0077ce");
const [isDBLoadingComplete, setIsDBLoadingComplete] = useState(false);
useEffect(() => {
(async () => {
const darkPromise = async () => {
const result = await getSetting(SettingId.DarkMode);
result.$.subscribe((changeEvent: SettingSchema) => {
setIsThemeDark(
typeof changeEvent.state == "boolean"
? changeEvent.state
: false
);
});
};
const accentPromise = async () => {
const result = await getSetting(SettingId.AccentColor);
result.$.subscribe((changeEvent: SettingSchema) => {
setAccentColor(
typeof changeEvent.state == "string"
? changeEvent.state
: "#0077ce"
);
});
};
await Promise.all([accentPromise(), darkPromise()]).catch(
(error) => {
console.error(error.message);
}
);
setIsDBLoadingComplete(true);
})();
}, []);
const baseTheme = isThemeDark ? CombinedDarkTheme : CombinedDefaultTheme;
const theme = {
...baseTheme,
colors: {
...baseTheme.colors,
primary: accentColor,
accent: accentColor,
},
fonts: fontList,
};
const toggleTheme = React.useCallback(() => {
return setIsThemeDark(!isThemeDark);
}, [isThemeDark]);
const preferences = React.useMemo(
() => ({
toggleTheme,
isThemeDark,
setAccentColor,
accentColor,
}),
[toggleTheme, isThemeDark, setAccentColor, accentColor]
);
if (!isLoadingComplete || !isDBLoadingComplete) {
return <></>;
}
return (
<PreferencesProvider value={preferences}>
<PaperProvider theme={theme}>
<SafeAreaProvider
style={{
overflow: "hidden",
}} /* Tijdelijke fix voor react-navigation */
>
<Navigation theme={theme} />
<StatusBar />
</SafeAreaProvider>
</PaperProvider>
</PreferencesProvider>
);
}