-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
173 lines (147 loc) · 5.14 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
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
import AsyncStorage from "@react-native-async-storage/async-storage";
import { useEffect, useState } from "react";
import { Button, ColorSchemeName, Image, StatusBar, StyleSheet, Text, TouchableOpacity, View, useColorScheme } from "react-native";
import { ToggleThemeIcon } from "./assets/Icons";
import Modal, { ModalProps } from "./components/Modal";
import { palette } from "./styles";
import { getTimeString } from "./utils";
const MODAL_PROPS: { [key: string]: Pick<ModalProps, "title" | "description"> } = {
add: {
title: "Añadir",
description: "¿Estás seguro/a de añadir 1 pancho?",
},
remove: {
title: "Eliminar",
description: "¿Estás seguro/a de eliminar 1 pancho?",
},
reset: {
title: "Reiniciar",
description: "¿Estás seguro/a de reiniciar tu historial?",
},
};
export default function App() {
const colorScheme = useColorScheme();
const [hotdogCount, setHotdogCount] = useState<number>(0);
const [theme, setTheme] = useState(colorScheme);
const [openModal, setOpenModal] = useState<ModalProps["open"]>(false);
const [modalProps, setModalProps] = useState<Omit<ModalProps, "open" | "setOpen">>();
const styles = getStyles(theme);
const storageKey = "killer-panchos-storage";
const timeString = getTimeString(hotdogCount);
const storeData = async (key: string, value: { [key: string]: any }) => {
try {
await AsyncStorage.setItem(key, JSON.stringify(value));
} catch (err) {
console.error(err);
}
};
const handlePressAddButton = () => {
setModalProps({ ...MODAL_PROPS.add, action: () => setHotdogCount((prev) => prev + 1) });
setOpenModal(true);
};
const handlePressRemoveButton = () => {
setModalProps({ ...MODAL_PROPS.remove, action: () => setHotdogCount((prev) => prev - 1) });
setOpenModal(true);
};
const handlePressResetButton = () => {
setModalProps({ ...MODAL_PROPS.reset, action: () => setHotdogCount(0) });
setOpenModal(true);
};
const handleToggleTheme = () => (theme === "light" ? setTheme("dark") : setTheme("light"));
const MainText = () => (
<Text style={styles.mainText}>
{hotdogCount ? (
<Text>
Hasta la fecha comiste <Text style={{ fontWeight: "700" }}>{hotdogCount}</Text> pancho{hotdogCount > 1 ? "s" : ""}. {hotdogCount >= 10 ? "😱" : ""}
</Text>
) : (
"Aún no comiste ningún pancho."
)}
</Text>
);
useEffect(() => {
(async () => {
try {
const data = await AsyncStorage.getItem(storageKey);
if (data !== null) {
const parsedData = JSON.parse(data);
setHotdogCount(parsedData.hotdogCount);
setTheme(parsedData.theme);
}
} catch (err) {
console.error(err);
}
})();
}, []);
useEffect(() => {
storeData(storageKey, { hotdogCount, theme });
}, [hotdogCount, theme]);
return (
<>
<View style={styles.container}>
<View style={styles.themeToggler}>
<TouchableOpacity onPress={handleToggleTheme}>
<ToggleThemeIcon width={25} height={25} fill={palette[theme || "light"].color} />
</TouchableOpacity>
</View>
<Image source={require("./assets/logo.png")} style={{ height: 130, width: 130 }} />
<View style={{ justifyContent: "space-between", height: 400 }}>
<MainText />
{hotdogCount ? (
<View style={{ alignItems: "center" }}>
<Text style={styles.secondaryText}>Lo cual equivale a</Text>
<Text style={styles.timeStats}>{timeString}</Text>
<Text style={styles.secondaryText}>menos de vida 💀</Text>
</View>
) : null}
<View style={{ justifyContent: "space-evenly", height: 150 }}>
<Button title={MODAL_PROPS.add.title!} onPress={handlePressAddButton} color="#841584" />
{hotdogCount ? (
<>
<Button title={MODAL_PROPS.remove.title!} onPress={handlePressRemoveButton} color="#841584" />
<Button title={MODAL_PROPS.reset.title!} onPress={handlePressResetButton} color="#841584" />
</>
) : null}
</View>
</View>
<StatusBar />
</View>
{modalProps && <Modal open={openModal} setOpen={setOpenModal} {...modalProps} />}
</>
);
}
const getStyles = (theme: ColorSchemeName) =>
StyleSheet.create({
container: {
flex: 1,
flexDirection: "column",
backgroundColor: palette[theme || "light"].backgroundColor,
alignItems: "center",
justifyContent: "space-around",
color: palette[theme || "light"].color,
paddingLeft: 15,
paddingRight: 15,
textAlign: "center",
},
mainText: {
textAlign: "center",
color: palette[theme || "light"].color,
fontSize: 18,
},
secondaryText: {
color: palette[theme || "light"].color,
fontWeight: "500",
fontSize: 18,
},
timeStats: {
color: palette[theme || "light"].color,
fontWeight: "700",
fontSize: 24,
},
themeToggler: {
width: "100%",
position: "absolute",
alignItems: "flex-end",
paddingRight: 15,
},
});