-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
104 lines (85 loc) · 2.33 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
import React, { useState } from 'react'
import { SafeAreaView, StyleSheet, StatusBar, Alert } from 'react-native'
import { includes, isNumber } from 'lodash'
import Color from './src/enums/Color'
import OperationKey from './src/enums/OperationKey'
import ActionKey from './src/enums/ActionKey'
import Keyboard from './src/components/Keyboard'
import Display from './src/components/Display'
const App = () => {
const [display, setDisplay] = useState(0)
const [left, setLeft] = useState(null)
const [operand, setOperand] = useState(null)
const [shouldClean, setShouldClean] = useState(false)
const handleKeyPress = async value => {
if (includes([ActionKey.PERCENTAGE, ActionKey.POSITIVE_NEGATIVE], value)) {
Alert.alert('Not Implemented :(')
return
}
if (value === ActionKey.AC) {
setDisplay(0)
setLeft(null)
setOperand(null)
return
}
if (includes(Object.values(OperationKey), value)) {
setLeft(display)
setShouldClean(true)
if (value === operand) {
setOperand(null)
return
}
setOperand(value)
return
}
if (value === ActionKey.EQUAL || value === OperationKey.ADD) {
let result
switch (operand) {
case OperationKey.ADD:
result = Number(left) + Number(display)
break
case OperationKey.SUBTRACT:
result = Number(left) - Number(display)
break
case 'x':
result = Number(left) * Number(display)
break
case '/':
result = Number(left) / Number(display)
break
}
setLeft(result)
setDisplay(result.toString())
setOperand(null)
setShouldClean(true)
return
}
if (display === 0) {
setDisplay(value)
return
}
if (shouldClean) {
setDisplay(value)
setShouldClean(false)
return
}
if (display.length === 9 && isNumber(value)) {
return
}
setDisplay(`${display}${value}`)
}
return (
<SafeAreaView style={styles.container}>
<StatusBar barStyle="light-content" />
<Display value={display} />
<Keyboard onKeyPress={handleKeyPress} selectedKey={operand} />
</SafeAreaView>
)
}
const styles = StyleSheet.create({
container: {
backgroundColor: Color.SCREEN_BACKGROUND,
flex: 1,
},
})
export default App