-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
158 lines (138 loc) · 3.62 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
import { StatusBar } from "expo-status-bar";
import React, { useState } from "react";
import { Alert, Button, StyleSheet, View, Text } from "react-native";
import Header from "./components/Header";
import NumberInput from "./components/NumberInput";
import OperandPicker from "./components/OperandPicker";
import Modal from "react-native-modal";
export default function App() {
const [modalVisibility, setModalVisibility] = useState(false);
const [firstNum, setFirstNum] = useState("");
const [secondNum, setSecondNum] = useState("");
const [resultString, setResultString] = useState("");
const [operand, setOperand] = useState("+");
const toggleModal = () => {
setModalVisibility(!modalVisibility);
};
const handleCalculate = () => {
const firstValue = parseFloat(firstNum.replace(",", "."));
const secondValue = parseFloat(secondNum.replace(",", "."));
if (
firstNum == "" ||
isNaN(firstValue) ||
secondNum == "" ||
isNaN(secondValue)
) {
Alert.alert("Error", "Please enter both numbers");
return;
}
let result = 0;
switch (operand) {
case "+":
result = firstValue + secondValue;
break;
case "-":
result = firstValue - secondValue;
break;
case "*":
result = firstValue * secondValue;
break;
case "/":
result = firstValue / secondValue;
break;
}
if (result % 1 != 0) {
result = parseFloat(result.toFixed(4));
}
toggleModal();
setResultString(`${firstValue} ${operand} ${secondValue} = ${result}`);
};
const handleCE = () => {
setFirstNum("");
setSecondNum("");
setOperand("+");
};
return (
<>
<View style={styles.container}>
<Header />
<Modal isVisible={modalVisibility}>
<View style={styles.modal}>
<Text style={styles.result}>{resultString}</Text>
</View>
<View style={styles.modal_actions}>
<Button onPress={toggleModal} title="Done" />
</View>
</Modal>
<View style={styles.body}>
<View style={styles.input_container}>
<NumberInput setter={setFirstNum} prompt="First" value={firstNum} />
<OperandPicker
currentOperand={operand}
operands={["+", "-", "*", "/"]}
setter={setOperand}
/>
<NumberInput
setter={setSecondNum}
prompt="Second"
value={secondNum}
/>
</View>
<View style={styles.button_container}>
<Button onPress={handleCalculate} title="Calculate" />
<Button onPress={handleCE} title="Clear" />
</View>
</View>
<StatusBar style="auto" />
</View>
</>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
body: {
flex: 1,
alignItems: "center",
},
modal: {
backgroundColor: "#fff",
justifyContent: "center",
alignItems: "center",
height: 100,
},
modal_actions: {
backgroundColor: "#fff",
paddingBottom: 20,
},
result: {
fontSize: 25,
marginTop: 40,
textAlign: "center",
},
input_container: {
flexDirection: "row",
alignItems: "center",
marginBottom: 60,
},
button_container: {
width: 200,
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
},
title: {
fontSize: 25,
},
input: {
height: 40,
margin: 12,
paddingLeft: 40,
paddingRight: 40,
fontSize: 18,
},
});