-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
71 lines (60 loc) · 1.84 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
import { StatusBar } from 'expo-status-bar'
import React from 'react'
import { View, useColorScheme } from 'react-native'
import tw from './lib/tailwind'
import Home from './screens/Home'
import { noteStorageKey } from './lib/constants'
import { useState } from 'react'
import { useEffect } from 'react'
import { NoteStorage } from './lib/types'
import AsyncStorage from '@react-native-async-storage/async-storage'
import AnimatedAppLoader from './components/AnimatedAppLoader'
import Constants from 'expo-constants'
const App = (): JSX.Element => {
const colorScheme = useColorScheme()
const emptyNoteStorage: NoteStorage = {
notes: [],
}
const [noteStorage, setNoteStorage] = useState<NoteStorage | null>(null)
const retrieveFromStorage = async () => {
if (!noteStorage) {
const stringifiedSavedNoteStorage = await AsyncStorage.getItem(
noteStorageKey,
)
if (stringifiedSavedNoteStorage) {
const savedNoteStorage = JSON.parse(
stringifiedSavedNoteStorage,
) as NoteStorage
setNoteStorage(savedNoteStorage)
} else {
setNoteStorage(emptyNoteStorage)
}
}
}
const saveToStorage = async () => {
if (noteStorage) {
await AsyncStorage.setItem(noteStorageKey, JSON.stringify(noteStorage))
}
}
useEffect(() => {
retrieveFromStorage()
}, [])
useEffect(() => {
saveToStorage()
}, [noteStorage])
return (
// eslint-disable-next-line tsc/config
<AnimatedAppLoader image={{ uri: Constants.manifest.splash.image }}>
<View
style={[
tw`w-full h-full`,
colorScheme === 'light' ? tw`bg-white` : tw`bg-dark-bg`,
]}
>
<Home noteStorage={noteStorage} setNoteStorage={setNoteStorage} />
<StatusBar style="auto" />
</View>
</AnimatedAppLoader>
)
}
export default App