-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
146 lines (135 loc) · 4.32 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
import {
StyleSheet,
Text,
View,
FlatList,
TouchableOpacity,
Image,
} from 'react-native'
import React, { useState } from 'react'
import numberWithCommas from './utils/numberWithCommas'
import CustomButton from './components/CustomButton'
import { btns, BTN_ACTIONS } from './components/configBtn'
import removeImg from './assets/remove.png'
export default function App() {
const [expression, setExpression] = useState('')
const [result, setResult] = useState('Hello!!')
const [darkMode, setDarkMode] = useState(false)
const [checkCalc, setCheckCalc] = useState(false) // Kiểm tra xem đã ấn = chưa
const renderItem = ({ item }) => (
<CustomButton
data={item}
onPress={() => handlePressButton(item)}
darkMode={darkMode}
/>
)
const handlePressButton = (item) => {
console.log(item)
switch (item.action) {
case BTN_ACTIONS.THEME:
setDarkMode(!darkMode)
break
case BTN_ACTIONS.ADD:
const op = item.display === 'x' ? '*' : item.display
checkCalc
? setExpression(result + op)
: setExpression(expression + op)
// setResult('')
setCheckCalc(false)
break
case BTN_ACTIONS.DELETE:
setExpression(expression.slice(0, expression.length - 1))
break
case BTN_ACTIONS.CLEAR_ALL:
setExpression('')
setResult('')
break
case BTN_ACTIONS.CALC:
if (expression.trim().length < 0) return null
try {
const res = eval(expression)
setCheckCalc(true)
// setResult(res.toString())
setResult(Math.round(res * 1000000000) / 1000000000)
} catch {
setResult('Syntax error')
} finally {
console.log('finished')
}
break
default:
console.log(`Invalid action ${item.action}`)
}
}
return (
<View
style={{
flex: 1,
paddingLeft: 16,
paddingRight: 16,
backgroundColor: darkMode ? '#373737' : '#fff',
}}
>
{/* results Container */}
<View style={styles.resultsContainer}>
<Text
style={{
marginBottom: 12,
fontSize: 32,
color: darkMode ? '#fff' : '#000',
}}
>
{numberWithCommas(result)}
</Text>
<Text
style={{
marginBottom: 12,
fontSize: 40,
color: darkMode ? '#fff' : '#000',
}}
>
{numberWithCommas(expression)}
</Text>
</View>
{/* AC button -- clear all*/}
<View style={{ alignItems: 'flex-end', marginBottom: 12 }}>
<TouchableOpacity
onPress={() => handlePressButton({ action: 'CLEAR_ALL' })}
>
<Image
source={removeImg}
style={{ width: 40, height: 40, marginRight: 24 }}
/>
</TouchableOpacity>
</View>
{/* button Container */}
<View style={styles.buttonContainer}>
<FlatList
data={btns}
renderItem={renderItem}
keyExtractor={(item) => item.display}
numColumns={4}
/>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingLeft: 16,
paddingRight: 16,
backgroundColor: '#373737',
// backgroundColor: '#fff',
},
buttonContainer: {
marginBottom: 16,
},
resultsContainer: {
flex: 2,
paddingHorizontal: 16,
alignItems: 'flex-end',
justifyContent: 'flex-end',
marginBottom: 20,
},
})