Skip to content

Commit c21bb23

Browse files
committed
fix: 修复输入%-被错误识别--的判断, 优化输入处理
refactor: 修改 cursorHide 的数据源
1 parent 66c6dcc commit c21bb23

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId "io.github.kineks.composecalculator"
1212
minSdk 21
1313
targetSdk 33
14-
versionCode 1
15-
versionName "1.0"
14+
versionCode 2
15+
versionName "1.1"
1616

1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
vectorDrawables {

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.compose.animation.AnimatedVisibility
55
import androidx.compose.foundation.background
66
import androidx.compose.foundation.clickable
77
import androidx.compose.foundation.interaction.MutableInteractionSource
8-
import androidx.compose.foundation.interaction.collectIsPressedAsState
8+
import androidx.compose.foundation.interaction.collectIsFocusedAsState
99
import androidx.compose.foundation.layout.*
1010
import androidx.compose.foundation.shape.RoundedCornerShape
1111
import androidx.compose.material.icons.Icons
@@ -77,13 +77,13 @@ fun DefaultView() {
7777
val interactionSource: MutableInteractionSource by remember {
7878
mutableStateOf(MutableInteractionSource())
7979
}
80-
val textFieldPressed = interactionSource.collectIsPressedAsState()
80+
val textFieldPressed = interactionSource.collectIsFocusedAsState()
8181

8282
// 输入框 State 管理
8383
val state =
8484
rememberCalculatorTextFieldState(
8585
interactionSource = interactionSource,
86-
cursorHide = { textFieldPressed.value },
86+
cursorHide = { !textFieldPressed.value },
8787
onValueChange = { updateBracketsCounts(it) },
8888
onDone = { startCalculatingEquations(it) }
8989
)
@@ -261,19 +261,32 @@ val onClick: String.(String, CalculatorTextFieldState) -> Unit = { text, state -
261261
isOperator() -> {
262262
state.apply {
263263
when (true) {
264+
// 如果最后一个是数字直接添加,其他分支走符号处理
264265
isOperatorPercentage() -> {
265-
if (last.isNumber())
266-
add(text)
266+
when(true) {
267+
last.isNumber() -> add(text)
268+
(last.isOperatorMIN() && lastSecond.isOperator()) -> {
269+
deleteLast()
270+
add(text,true)
271+
}
272+
last.isOperator() -> {
273+
if (lastSecond.isOperatorPercentage())
274+
deleteLast()
275+
add(text, true)
276+
}
277+
else -> {}
278+
}
267279
}
268280
// 对于需要输入负数的情况
269281
(isOperatorMIN() && last.isOperator()) -> {
270282
add(text, lastSecond.isOperator())
271283
}
272284
// 替换 --
273-
(last.isOperatorMIN() && lastSecond.isOperator()) -> {
285+
(last.isOperatorMIN() && lastSecond.isOperator() && !lastSecond.isOperatorPercentage()) -> {
274286
deleteLast()
275287
add(text, true)
276288
}
289+
// 运算符号处理需要排除百分号
277290
(last.isOperator() && !last.isOperatorPercentage()) -> add(text, true)
278291
else -> add(text)
279292
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class CalculatorTextFieldState(
5858
}
5959

6060

61+
6162
fun isError() {
6263
isError.value = true
6364
}
@@ -98,7 +99,7 @@ class CalculatorTextFieldState(
9899
StringBuilder(value.text)
99100
.deleteAt(value.cursorWhere(cursorHide())-1)
100101
.toString(),
101-
TextRange(value.cursorWhere(cursorHide()))
102+
TextRange(value.cursorWhere(cursorHide())-1)
102103
)
103104
if (value.cursorSelection)
104105
setTextField(

0 commit comments

Comments
 (0)