-
Notifications
You must be signed in to change notification settings - Fork 0
/
PaperApp.tsx
113 lines (95 loc) · 2.7 KB
/
PaperApp.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
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
import React, { useState, useEffect } from "react";
import { PaperProvider, MD3Theme } from "react-native-paper";
import { LocationObject } from "expo-location";
import AsyncStorage from "@react-native-async-storage/async-storage";
import App from "./App";
import { AppContext, initialAppState } from "./src/context/appContext";
import { AlarmItemType, AlarmsType } from "./src/types/stateTypes";
import { dark } from "./src/styles/paperTheme";
const PaperApp = () => {
const [theme, setTheme] = useState<MD3Theme>();
const [currentLocation, setCurrentLocation] = useState<LocationObject | null>(
null
);
const [alarms, setAlarms] = useState<AlarmsType>();
const [currentAlarm, setCurrentAlarm] = useState<AlarmItemType>();
const [showMapViewModal, setShowMapViewModal] = useState(false);
const [showCreateEditAlarm, setShowCreateEditAlarm] = useState(false);
const [formDataDraft, setFormDataDraft] = useState(null);
const deleteAlarm = (id: string) => {
const newAlarmsMap = { ...alarms };
delete newAlarmsMap?.[id];
setAlarms(newAlarmsMap);
};
const editAlarm = (id: string, newAlarmData: AlarmItemType) =>
setAlarms({
...alarms,
[id]: newAlarmData,
});
const saveThemeToAsync = async () => {
try {
await AsyncStorage.setItem("theme", JSON.stringify(theme));
} catch (e) {
console.log(e, "Theme Set Async");
}
};
const hydrateThemeFromAsync = async () => {
try {
const theTheme = await AsyncStorage.getItem("theme");
if (theTheme) setTheme(JSON.parse(theTheme));
} catch (e) {
console.log(e, "Alarms Get Async");
}
};
const saveAlarmsToAsync = async () => {
try {
await AsyncStorage.setItem("alarmsMap", JSON.stringify(alarms));
} catch (e) {
console.log(e, "Alarms Set Async");
}
};
const hydrateAlarmsFromAsync = async () => {
try {
const theAlarms = await AsyncStorage.getItem("alarmsMap");
if (theAlarms) setAlarms(JSON.parse(theAlarms));
} catch (e) {
console.log(e, "Alarms Get Async");
}
};
const value = {
...initialAppState,
theme,
setTheme,
currentLocation,
setCurrentLocation,
alarms,
setAlarms,
saveAlarmsToAsync,
deleteAlarm,
editAlarm,
currentAlarm,
setCurrentAlarm,
showMapViewModal,
setShowMapViewModal,
showCreateEditAlarm,
setShowCreateEditAlarm,
formDataDraft,
setFormDataDraft,
};
useEffect(() => {
if (alarms) saveAlarmsToAsync();
if (theme) saveThemeToAsync();
}, [JSON.stringify(alarms), JSON.stringify(theme)]);
useEffect(() => {
hydrateAlarmsFromAsync();
hydrateThemeFromAsync();
}, []);
return (
<AppContext.Provider value={value}>
<PaperProvider theme={value?.theme ?? dark}>
<App />
</PaperProvider>
</AppContext.Provider>
);
};
export default PaperApp;