Skip to content

Commit 88dd1af

Browse files
committed
fix: 修复输入小数点报 Unknown Operator Exception
refactor: 按钮回调优化
1 parent e4f671c commit 88dd1af

File tree

3 files changed

+69
-57
lines changed

3 files changed

+69
-57
lines changed

app/src/main/java/io/github/kineks/composecalculator/ui/DefaultView.kt

Lines changed: 64 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ fun DefaultView() {
120120

121121
// 菜单
122122
Box(
123-
modifier = Modifier.fillMaxHeight().statusBarsPadding(),
123+
modifier = Modifier
124+
.fillMaxHeight()
125+
.statusBarsPadding(),
124126
contentAlignment = Alignment.TopEnd
125127
) {
126128
var dropdownMenuExpanded by remember {
@@ -199,56 +201,8 @@ fun DefaultView() {
199201

200202
// 计算器按钮
201203
CalculatorButton(
202-
onNumberClick = { text: String -> state.add(text) },
203-
onOperatorClick = { text: String ->
204-
when (true) {
205-
// 复位
206-
isOperatorAC() -> {
207-
state.clearTextField()
208-
}
209-
// 删除键
210-
isOperatorBackSpace() -> {
211-
state.deleteLast()
212-
}
213-
// 括号
214-
isOperatorBrackets() -> {
215-
when (true) {
216-
(operatorBracketsCounts != 0 && !state.last.isOperatorBracketStart()) -> {
217-
state.add(")")
218-
operatorBracketsCounts--
219-
}
220-
else -> {
221-
state.add("(")
222-
operatorBracketsCounts++
223-
}
224-
}
225-
}
226-
// 其他符号
227-
isOperator() -> {
228-
state.apply {
229-
when (true) {
230-
isOperatorPercentage() -> {
231-
if (last.isNumber())
232-
add(text)
233-
}
234-
// 对于需要输入负数的情况
235-
(isOperatorMIN() && last.isOperator()) -> {
236-
add(text, lastSecond.isOperator())
237-
}
238-
// 替换 --
239-
(last.isOperatorMIN() && lastSecond.isOperator()) -> {
240-
deleteLast()
241-
add(text, true)
242-
}
243-
(last.isOperator() && !last.isOperatorPercentage()) -> add(text, true)
244-
else -> add(text)
245-
}
246-
}
247-
}
248-
else -> throw Exception("Unknown Operator : $text")
249-
}
250-
251-
},
204+
onNumberClick = { onClick(this, state) },
205+
onOperatorClick = { onClick(this, state) },
252206
startCalculatingEquations = { startCalculatingEquations(state) },
253207
modifier = Modifier
254208
.isHorizontal {
@@ -260,7 +214,8 @@ fun DefaultView() {
260214
.isNotHorizontal {
261215
weight(1.1f)
262216
.padding(10.dp)
263-
})
217+
}
218+
)
264219

265220

266221
}
@@ -269,6 +224,63 @@ fun DefaultView() {
269224
}
270225

271226

227+
val onClick: String.(String, CalculatorTextFieldState) -> Unit = { text, state ->
228+
if (state.value.text == getString(R.string.data_error))
229+
state.clearTextField()
230+
when (true) {
231+
// 复位
232+
isOperatorAC() -> {
233+
state.clearTextField()
234+
}
235+
// 删除键
236+
isOperatorBackSpace() -> {
237+
state.deleteLast()
238+
}
239+
isNumber() -> {
240+
state.add(text)
241+
}
242+
// 括号
243+
isOperatorBrackets() -> {
244+
when (true) {
245+
(operatorBracketsCounts != 0 && !state.last.isOperatorBracketStart()) -> {
246+
state.add(")")
247+
operatorBracketsCounts--
248+
}
249+
else -> {
250+
state.add("(")
251+
operatorBracketsCounts++
252+
}
253+
}
254+
}
255+
// 其他符号
256+
isOperator() -> {
257+
state.apply {
258+
when (true) {
259+
isOperatorPercentage() -> {
260+
if (last.isNumber())
261+
add(text)
262+
}
263+
// 对于需要输入负数的情况
264+
(isOperatorMIN() && last.isOperator()) -> {
265+
add(text, lastSecond.isOperator())
266+
}
267+
// 替换 --
268+
(last.isOperatorMIN() && lastSecond.isOperator()) -> {
269+
deleteLast()
270+
add(text, true)
271+
}
272+
(last.isOperator() && !last.isOperatorPercentage()) -> add(text, true)
273+
else -> add(text)
274+
}
275+
}
276+
}
277+
else -> throw Exception("Unknown Operator : $text")
278+
}
279+
280+
}
281+
282+
283+
272284
@Preview(showBackground = true)
273285
@Composable
274286
fun DefaultPreview() {

app/src/main/java/io/github/kineks/composecalculator/ui/view/CalculatorButton.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fun CalculatorButton(
7979
}
8080
item { NumberButton(text = "0", clickable = onNumberClick) }
8181
item {
82-
NumberButton(text = "", clickable = { onNumberClick(".") })
82+
NumberButton(text = "", clickable = { ".".apply { onNumberClick(".") } })
8383
}
8484
item {
8585
OperatorButton(

app/src/main/java/io/github/kineks/composecalculator/ui/view/CalculatorTextField.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,20 @@ fun CalculatorTextField(
170170
state.apply {
171171
TextField(
172172
value = value,
173-
onValueChange = {
173+
onValueChange = { textFieldValue ->
174174
// 避免键盘或者输入法删除括号导致计数错误
175175
operatorBracketsCounts = 0
176-
it.text.forEach {
176+
textFieldValue.text.forEach {
177177
when(it) {
178178
'(' -> operatorBracketsCounts++
179179
')' -> operatorBracketsCounts--
180180
}
181181
}
182-
value = it
182+
value = textFieldValue
183183
},
184184
label = {
185185
Text(
186-
text = if (label.isNotEmpty()) "$label=" else label,
186+
text = if (label.isNotEmpty()) "$label =" else label,
187187
style = MaterialTheme.typography.headlineMedium,
188188
modifier = Modifier
189189
.alpha(0.8f)

0 commit comments

Comments
 (0)