-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
87 lines (76 loc) · 2.64 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
import "react-native-gesture-handler";
import { StatusBar } from "expo-status-bar";
import React, { useState } from "react";
import { SafeAreaProvider } from "react-native-safe-area-context";
import Navigation from "./navigation";
import { Context, downloadedBooks, indexURL, newIndex, remapBooks } from "./common/common";
import * as Font from "expo-font";
import { Ionicons } from "@expo/vector-icons";
import * as FileSystem from "expo-file-system";
import { BooksByIDType, BookType, ContextType } from "./types";
import { themes } from './components/Themed'
import AsyncStorage from "@react-native-async-storage/async-storage";
export default function App() {
let b: BooksByIDType = {}
const [context, setContext] = useState<ContextType>({
downloaded: new Array<BookType>(),
index: newIndex(),
booksByID: b,
theme: "light",
font: "space-mono",
fontSize: 14,
});
const [isLoadingComplete, setLoadingComplete] = React.useState(false);
React.useEffect(() => {
(async () => {
await Font.loadAsync({
...Ionicons.font,
"space-mono": require("./assets/fonts/SpaceMono-Regular.ttf"),
"libre-baskerville": require("./assets/fonts/LibreBaskerville-Regular.ttf"),
"merriweather": require("./assets/fonts/Merriweather-Regular.ttf"),
"3270": require("./assets/fonts/3270-Regular.ttf")
});
let theme = await AsyncStorage.getItem("theme")
let font = await AsyncStorage.getItem("font")
let fontSize = parseInt(await AsyncStorage.getItem("fontSize") || "14")
let downloaded: Array<BookType> = []
let booksByID = {}
let index = newIndex()
try {
let fi = await FileSystem.getInfoAsync(indexURL);
if (fi && fi.size > 0) {
let r = await fetch(indexURL)
let data = await r.text()
index.deserialize(data);
booksByID = remapBooks(index)
downloaded = await downloadedBooks(booksByID)
}
} catch (e) {
console.log(e);
} finally {
setContext({
...context,
downloaded: downloaded,
index: index,
booksByID: booksByID,
theme: theme || "light",
font: font || "space-mono",
fontSize: fontSize,
})
}
setLoadingComplete(true);
})();
}, []);
if (!isLoadingComplete) {
return null;
} else {
return (
<SafeAreaProvider>
<Context.Provider value={[context, setContext]}>
<Navigation />
<StatusBar hidden={false} translucent={false} backgroundColor={themes[context.theme].bgcolor}/>
</Context.Provider>
</SafeAreaProvider>
);
}
}