Skip to content

Commit 7d80767

Browse files
committed
feat: 新增错误状态
1 parent 7be0262 commit 7d80767

File tree

4 files changed

+107
-27
lines changed

4 files changed

+107
-27
lines changed

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package io.github.kineks.composecalculator.ui
22

33
import androidx.compose.foundation.background
44
import androidx.compose.foundation.isSystemInDarkTheme
5-
import androidx.compose.foundation.layout.*
5+
import androidx.compose.foundation.layout.Box
6+
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.foundation.layout.padding
68
import androidx.compose.foundation.shape.RoundedCornerShape
79
import androidx.compose.material3.MaterialTheme
810
import androidx.compose.material3.Surface
9-
import androidx.compose.material3.surfaceColorAtElevation
1011
import androidx.compose.runtime.*
1112
import androidx.compose.ui.Alignment
1213
import androidx.compose.ui.Modifier
@@ -28,14 +29,14 @@ import io.github.kineks.composecalculator.ui.view.*
2829
fun DefaultView() {
2930
val systemUiController = rememberSystemUiController()
3031
val useDarkIcons = !isSystemInDarkTheme()
31-
val color = MaterialTheme.colorScheme.surfaceColorAtElevation(5.dp)
3232
SideEffect {
3333
systemUiController.setStatusBarColor(Color.Transparent, useDarkIcons)
3434
}
3535

3636
var colorDisplay by remember {
3737
mutableStateOf(false)
3838
}
39+
3940
val startCalculatingEquations: (CalculatorTextFieldState) -> Unit by remember {
4041
mutableStateOf({ textFieldState ->
4142
textFieldState.apply {
@@ -44,12 +45,17 @@ fun DefaultView() {
4445
clearTextField()
4546
} else {
4647
textFieldLabel = textFieldValue.text
48+
val number = textFieldValue.text.parse().toString()
4749
setTextField(
48-
textFieldValue.text.parse().toString().subZeroAndDot()
50+
number.subZeroAndDot()
4951
)
52+
if (number == "NaN") {
53+
textFieldState.isError()
54+
}
5055
}
5156
} catch (e: Exception) {
5257
setTextField(getString(R.string.data_error))
58+
textFieldState.isError()
5359
}
5460
}
5561
})
@@ -76,13 +82,12 @@ fun DefaultView() {
7682
tonalElevation = 5.dp,
7783
shadowElevation = 5.dp,
7884
modifier = Modifier
79-
.fillMaxWidth()
80-
.fillMaxHeight()
85+
.fillMaxSize()
8186
.isNotHorizontal {
8287
padding(bottom = 50.dp)
8388
}
8489

85-
) {}
90+
) { }
8691

8792

8893
CalculatorTextField(
@@ -95,18 +100,24 @@ fun DefaultView() {
95100
padding(bottom = 50.dp)
96101
}
97102
)
103+
104+
105+
RowOperatorButton {
106+
textFieldState.append(it)
107+
}
108+
98109
}
99110

100111
CalculatorButton(
101112
onNumberClick = { text: String ->
102113
textFieldState.textFieldLabel = ""
103114
textFieldState.setTextField(
104-
textFieldState.textFieldValue.text.replaceIfEqual("0",text)
115+
textFieldState.textFieldValue.text.replaceIfEqual("0", text)
105116
)
106117
},
107118
onOperatorClick = { text: String ->
108119
textFieldState.textFieldLabel = ""
109-
when(true) {
120+
when (true) {
110121
text.isOperatorAC() -> {
111122
textFieldState.clearTextField()
112123
}
@@ -130,7 +141,9 @@ fun DefaultView() {
130141
text.isOperator() -> {
131142
textFieldState.setTextField(
132143
textFieldState.textFieldValue.text.let {
133-
(if (OperatorMap[it.lastOrNull().toString()] != null) it.substring(
144+
(if (OperatorMap[it.lastOrNull()
145+
.toString()] != null
146+
) it.substring(
134147
0,
135148
it.lastIndex
136149
) else it) + text
@@ -161,7 +174,6 @@ fun DefaultView() {
161174
}
162175

163176

164-
165177
@Preview(showBackground = true)
166178
@Composable
167179
fun DefaultPreview() {

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package io.github.kineks.composecalculator.ui.view
22

3-
import androidx.compose.foundation.layout.Arrangement
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.*
45
import androidx.compose.foundation.lazy.grid.GridCells
56
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
67
import androidx.compose.material.icons.Icons
78
import androidx.compose.material.icons.outlined.Backspace
89
import androidx.compose.material3.Icon
910
import androidx.compose.material3.MaterialTheme
11+
import androidx.compose.material3.Text
1012
import androidx.compose.runtime.Composable
13+
import androidx.compose.ui.Alignment
1114
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.text.style.TextAlign
1216
import androidx.compose.ui.unit.dp
17+
import io.github.kineks.composecalculator.ui.layout.isHorizontal
1318

1419
@Composable
1520
fun CalculatorButton(
16-
onNumberClick: (text:String) -> Unit,
17-
onOperatorClick: (text:String) -> Unit,
21+
onNumberClick: (text: String) -> Unit,
22+
onOperatorClick: (text: String) -> Unit,
1823
startCalculatingEquations: () -> Unit,
1924
modifier: Modifier = Modifier
2025
) {
@@ -83,8 +88,47 @@ fun CalculatorButton(
8388
}
8489
}
8590

86-
val CalculatorOperatorButton = listOf("AC","()","%","÷","×","","+","","=")
87-
val CalculatorNumberButton = listOf("1","2","3","4","5","6","7","8","9","0",".")
91+
@Composable
92+
fun RowOperatorButton(
93+
modifier: Modifier = Modifier,
94+
rowOperatorButton: @Composable (text: String) -> Unit = {
95+
Text(
96+
text = it,
97+
color = MaterialTheme.colorScheme.onSurface,
98+
textAlign = TextAlign.Center,
99+
style = MaterialTheme.typography.headlineLarge,
100+
modifier = modifier
101+
//.size(width = 68.dp, height = 35.dp)
102+
.clickable { clickable(it) }
103+
)
104+
},
105+
clickable: (text: String) -> Unit
106+
) {
107+
108+
Box(
109+
contentAlignment = if (isHorizontal()) Alignment.TopStart else Alignment.BottomStart,
110+
modifier = Modifier
111+
.fillMaxSize()
112+
.isHorizontal { padding(start = 2.dp, top = 18.dp).statusBarsPadding() }
113+
) {
114+
LazyVerticalGrid(columns = GridCells.Fixed(4)) {
115+
item { rowOperatorButton("sin") }
116+
item { rowOperatorButton("cos") }
117+
item { rowOperatorButton("pi") }
118+
item { rowOperatorButton("e") }
119+
}/*
120+
LazyRow {
121+
item { rowOperatorButton("sin") }
122+
item { rowOperatorButton("cos") }
123+
item { rowOperatorButton("pi") }
124+
item { rowOperatorButton("e") }
125+
}*/
126+
}
127+
128+
}
129+
130+
val CalculatorOperatorButton = listOf("AC", "()", "%", "÷", "×", "", "+", "", "=")
131+
val CalculatorNumberButton = listOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".")
88132
fun String.isNumber(): Boolean = CalculatorNumberButton.indexOf(this) != -1
89133
fun String.isOperator(): Boolean = CalculatorOperatorButton.indexOf(this) != -1
90134
fun String.isOperatorAC(): Boolean = "AC" == this

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import io.github.kineks.composecalculator.append
3131
class CalculatorTextFieldState(
3232
private var _textFieldValue: MutableState<TextFieldValue>,
3333
private var _textFieldLabel: MutableState<String>,
34-
val textFieldCursorEnabled: () -> Boolean,
34+
val isError: MutableState<Boolean>,
35+
val textFieldCursorEnabled: (CalculatorTextFieldState) -> Boolean,
3536
val onDone: (KeyboardActionScope.(CalculatorTextFieldState) -> Unit)
3637
) {
3738
var textFieldValue
@@ -45,16 +46,27 @@ class CalculatorTextFieldState(
4546
_textFieldLabel.value = value
4647
}
4748

49+
fun isError() {
50+
isError.value = true
51+
}
52+
53+
fun isNoError() {
54+
isError.value = false
55+
}
56+
4857
fun append(text: String, offset: Int = 0) {
58+
isNoError()
4959
_textFieldValue.value = _textFieldValue.value.append(text, offset)
5060
}
5161

5262
fun clearTextField() {
63+
isNoError()
5364
_textFieldValue.value = TextFieldValue("0")
5465
_textFieldLabel.value = ""
5566
}
5667

5768
fun setTextField(text: String) {
69+
isNoError()
5870
_textFieldValue.value = TextFieldValue(
5971
text = text,
6072
selection = TextRange(text.length)
@@ -65,9 +77,10 @@ class CalculatorTextFieldState(
6577

6678
@Composable
6779
fun rememberCalculatorTextFieldState(
68-
textFieldValue: MutableState<TextFieldValue> = mutableStateOf(TextFieldValue("0")),
69-
textFieldLabel: MutableState<String> = mutableStateOf(""),
70-
cursorEnabled: () -> Boolean = { textFieldValue.value.text.isNotEmpty() },
80+
textFieldValue: TextFieldValue = TextFieldValue("0"),
81+
textFieldLabel: String = "",
82+
isError: Boolean = false,
83+
cursorEnabled: (CalculatorTextFieldState) -> Boolean = { it.textFieldValue.text.isNotEmpty() },
7184
onDone: (KeyboardActionScope.(CalculatorTextFieldState) -> Unit) = { }
7285
) = rememberSaveable(saver = Saver(
7386
save = {
@@ -77,13 +90,18 @@ fun rememberCalculatorTextFieldState(
7790
CalculatorTextFieldState(
7891
mutableStateOf(TextFieldValue(it[1])),
7992
mutableStateOf(it[0]),
93+
mutableStateOf(isError),
8094
cursorEnabled,
8195
onDone
8296
)
8397
}
8498
)) {
8599
CalculatorTextFieldState(
86-
textFieldValue, textFieldLabel, cursorEnabled, onDone
100+
mutableStateOf(textFieldValue),
101+
mutableStateOf(textFieldLabel),
102+
mutableStateOf(isError),
103+
cursorEnabled,
104+
onDone
87105
)
88106
}
89107

@@ -112,6 +130,7 @@ fun CalculatorTextField(
112130
}
113131
)
114132
},
133+
isError = state.isError.value,
115134
keyboardActions = KeyboardActions(onDone = { onDone(state) }),
116135
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
117136
shape = RoundedCornerShape(bottomStart = 20.dp, bottomEnd = 20.dp),
@@ -122,13 +141,17 @@ fun CalculatorTextField(
122141
), textAlign = TextAlign.End
123142
),
124143
colors = TextFieldDefaults.textFieldColors(
125-
textColor = MaterialTheme.colorScheme.onSurface,
144+
textColor =
145+
if (state.isError.value)
146+
MaterialTheme.colorScheme.error
147+
else
148+
MaterialTheme.colorScheme.onSurface,
126149
disabledTextColor = MaterialTheme.colorScheme.onSurface,
127-
cursorColor = if (textFieldCursorEnabled()) MaterialTheme.colorScheme.primary else Color.Transparent,
128-
errorCursorColor = Color.Transparent,
150+
cursorColor = if (textFieldCursorEnabled(state)) MaterialTheme.colorScheme.primary else Color.Transparent,
151+
//errorCursorColor = Color.Transparent,
129152
focusedIndicatorColor = Color.Transparent,
130153
unfocusedIndicatorColor = Color.Transparent,
131-
errorIndicatorColor = Color.Transparent,
154+
//errorIndicatorColor = Color.Transparent,
132155
disabledIndicatorColor = Color.Transparent,
133156
focusedLeadingIconColor = Color.Transparent,
134157
unfocusedLeadingIconColor = Color.Transparent,
@@ -143,7 +166,7 @@ fun CalculatorTextField(
143166
focusedLabelColor = MaterialTheme.colorScheme.onSurface,
144167
unfocusedLabelColor = MaterialTheme.colorScheme.onSurface,
145168
disabledLabelColor = MaterialTheme.colorScheme.onSurface,
146-
errorLabelColor = MaterialTheme.colorScheme.onSurface,
169+
//errorLabelColor = MaterialTheme.colorScheme.onSurface,
147170

148171
placeholderColor = Color.Transparent,
149172
disabledPlaceholderColor = Color.Transparent

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ fun OperatorButton(
5252
ratio: Float = 1.135f,
5353
alpha: Float = 0.9f,
5454
tonalElevation: Dp = 0.dp,
55+
shadowElevation: Dp = 1.dp,
5556
content: @Composable (BoxScope.() -> Unit)? = null,
5657
clickable: (text: String) -> Unit
5758
) {
@@ -66,7 +67,7 @@ fun OperatorButton(
6667
shape = RoundedCornerShape(50),
6768
onClick = { clickable(text) },
6869
tonalElevation = tonalElevation,
69-
shadowElevation = 1.dp,
70+
shadowElevation = shadowElevation,
7071
//contentColor = backgroundColor,
7172
color = backgroundColor.copy(alpha = alpha),
7273
modifier = modifier

0 commit comments

Comments
 (0)