-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
218 lines (206 loc) · 5.63 KB
/
App.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import React, { useState } from "react";
import {
StyleSheet,
View,
Text,
TextInput,
TouchableOpacity,
FlatList,
SafeAreaView,
} from "react-native";
import { MaterialIcons } from "@expo/vector-icons";
import {
useSafeAreaInsets,
SafeAreaProvider,
} from "react-native-safe-area-context";
import Settings from "./Settings";
function App() {
const [measurements, setMeasurements] = useState([]);
const [volume, setVolume] = useState(0);
const [defaultPole, setDefaultPole] = useState({
width: 10,
height: 10,
length: 300,
});
const [inputValues, setInputValues] = useState({
width: "",
height: "",
length: "",
});
const [showSettings, setShowSettings] = useState(false);
const insets = useSafeAreaInsets();
const addMeasurement = (width, height, length) => {
const newMeasurements = [
...measurements,
{ width, height, length, key: Date.now().toString() },
];
setMeasurements(newMeasurements);
const newVolume = newMeasurements.reduce(
(total, current) =>
total + current.width * current.height * current.length,
0
);
setVolume(newVolume);
};
const handleInputChange = (value, field) => {
if (value === "" || !isNaN(value)) {
setInputValues({ ...inputValues, [field]: value });
}
};
const handleInputSubmit = () => {
const { width, height, length } = inputValues;
if (width && height && length) {
addMeasurement(
parseFloat(width),
parseFloat(height),
parseFloat(length) * 100
);
setInputValues({ width: "", height: "", length: "" });
}
};
const onDelete = (key) => {
const newMeasurements = measurements.filter((item) => item.key !== key);
setMeasurements(newMeasurements);
const newVolume = newMeasurements.reduce(
(total, current) =>
total + current.width * current.height * current.length,
0
);
setVolume(newVolume);
};
const onDefaultPoleChange = (newDefaultPole) => {
setDefaultPole(newDefaultPole);
};
const renderItem = ({ item }) => {
return (
<View style={styles.listItem}>
<Text
style={styles.itemText}
>{`${item.width}cm x ${item.height}cm x ${item.length}cm`}</Text>
<TouchableOpacity onPress={() => onDelete(item.key)}>
<MaterialIcons name="delete" size={24} color="black" />
</TouchableOpacity>
</View>
);
};
const defaultPoleVolume =
defaultPole.width * defaultPole.height * defaultPole.length;
const defaultPiecesFromTotal = (volume / defaultPoleVolume).toFixed(2);
return (
<SafeAreaView style={[styles.container, { marginTop: insets.top }]}>
<View style={styles.header}>
<Text style={styles.title}>Cubic</Text>
<TouchableOpacity onPress={() => setShowSettings(!showSettings)}>
<MaterialIcons name="settings" size={24} color="black" />
</TouchableOpacity>
</View>
{showSettings && (
<Settings
defaultPole={defaultPole}
onDefaultPoleChange={onDefaultPoleChange}
/>
)}
<View style={styles.inputContainer}>
<TextInput
style={styles.input}
keyboardType="numeric"
placeholder="Ancho (cm)"
value={inputValues.width}
onChangeText={(value) => handleInputChange(value, "width")}
onSubmitEditing={handleInputSubmit}
/>
<TextInput
style={styles.input}
keyboardType="numeric"
placeholder="Alto (cm)"
value={inputValues.height}
onChangeText={(value) => handleInputChange(value, "height")}
onSubmitEditing={handleInputSubmit}
/>
<TextInput
style={styles.input}
keyboardType="numeric"
placeholder="Largo (m)"
value={inputValues.length}
onChangeText={(value) => handleInputChange(value, "length")}
onSubmitEditing={handleInputSubmit}
/>
</View>
<FlatList data={measurements} renderItem={renderItem} />
<View style={styles.footer}>
<Text style={styles.footerText}>
Volumen total: {volume.toFixed(2)} cm³
</Text>
<Text style={styles.footerText}>
Pieza predeterminada: {defaultPoleVolume.toFixed(2)} cm³
</Text>
<Text style={styles.footerText}>
Cantidad de piezas: {measurements.length}
</Text>
<Text style={styles.footerText}>
Piezas obtenibles: {defaultPiecesFromTotal}
</Text>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#F5F5F5",
},
header: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
paddingHorizontal: 20,
paddingTop: 10,
},
title: {
fontSize: 24,
fontWeight: "bold",
color: "#FFA500",
},
inputContainer: {
flexDirection: "row",
justifyContent: "space-around",
alignItems: "center",
marginVertical: 20,
},
input: {
borderWidth: 1,
borderColor: "#999",
borderRadius: 5,
width: "30%",
padding: 10,
},
listItem: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
backgroundColor: "#FFF",
borderBottomColor: "#ccc",
borderBottomWidth: 1,
paddingHorizontal: 20,
paddingVertical: 10,
},
itemText: {
fontSize: 18,
},
footer: {
backgroundColor: "#FFA500",
paddingHorizontal: 20,
paddingVertical: 10,
},
footerText: {
fontSize: 18,
color: "#FFF",
},
});
export default function Main() {
return (
<SafeAreaProvider>
<App />
</SafeAreaProvider>
);
}