1
1
package com.equationl.calculator_compose.view
2
2
3
3
import androidx.compose.animation.*
4
+ import androidx.compose.foundation.BorderStroke
4
5
import androidx.compose.foundation.background
5
6
import androidx.compose.foundation.clickable
6
7
import androidx.compose.foundation.layout.*
7
- import androidx.compose.material.Card
8
- import androidx.compose.material.ExperimentalMaterialApi
9
- import androidx.compose.material.MaterialTheme
10
- import androidx.compose.material.Text
8
+ import androidx.compose.material.*
11
9
import androidx.compose.runtime.Composable
12
10
import androidx.compose.ui.Alignment
13
11
import androidx.compose.ui.Modifier
@@ -19,8 +17,8 @@ import androidx.compose.ui.unit.dp
19
17
import androidx.compose.ui.unit.sp
20
18
import androidx.hilt.navigation.compose.hiltViewModel
21
19
import com.equationl.calculator_compose.dataModel.InputBase
22
- import com.equationl.calculator_compose.dataModel.programmerLeftKeyBoardBtn
23
- import com.equationl.calculator_compose.dataModel.programmerRightKeyBoardBtn
20
+ import com.equationl.calculator_compose.dataModel.programmerFunctionKeyBoardBtn
21
+ import com.equationl.calculator_compose.dataModel.programmerNumberKeyBoardBtn
24
22
import com.equationl.calculator_compose.ui.theme.*
25
23
import com.equationl.calculator_compose.utils.formatNumber
26
24
import com.equationl.calculator_compose.view.widgets.AutoSizeText
@@ -34,18 +32,22 @@ fun ProgrammerScreen(
34
32
Row (modifier = Modifier .fillMaxWidth(),
35
33
horizontalArrangement = Arrangement .SpaceBetween ) {
36
34
// 左侧键盘
37
- Row (modifier = Modifier .weight(1.5f )) {
38
- LeftKeyBoard (viewModel = viewModel)
35
+ Row (modifier = Modifier .weight(1.3f )) {
36
+ FunctionKeyBoard (viewModel = viewModel)
39
37
}
40
38
39
+ Divider (modifier = Modifier .fillMaxHeight().width(1 .dp). padding(vertical = 16 .dp, horizontal = 0 .dp))
40
+
41
41
// 显示数据
42
42
Row (modifier = Modifier .weight(2f )) {
43
43
CenterScreen (viewModel = viewModel)
44
44
}
45
45
46
+ Divider (modifier = Modifier .fillMaxHeight().width(1 .dp). padding(vertical = 16 .dp, horizontal = 0 .dp))
47
+
46
48
// 右侧键盘
47
- Row (modifier = Modifier .weight(1.3f )) {
48
- RightKeyBoard (viewModel = viewModel)
49
+ Row (modifier = Modifier .weight(1.5f )) {
50
+ NumberBoard (viewModel = viewModel)
49
51
}
50
52
}
51
53
}
@@ -64,7 +66,13 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
64
66
horizontalAlignment = Alignment .End
65
67
) {
66
68
AnimatedContent (targetState = viewState.showText) { targetState: String ->
67
- Text (text = targetState, modifier = Modifier .padding(8 .dp), fontSize = ShowNormalFontSize , fontWeight = FontWeight .Light )
69
+ Text (
70
+ text = targetState,
71
+ modifier = Modifier .padding(8 .dp),
72
+ fontSize = ShowNormalFontSize ,
73
+ fontWeight = FontWeight .Light ,
74
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
75
+ )
68
76
}
69
77
AnimatedContent (
70
78
targetState = viewState.inputValue,
@@ -91,7 +99,8 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
91
99
)
92
100
,
93
101
fontSize = InputLargeFontSize ,
94
- fontWeight = FontWeight .Bold
102
+ fontWeight = FontWeight .Bold ,
103
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
95
104
)
96
105
}
97
106
}
@@ -112,13 +121,15 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
112
121
fontSize =
113
122
if (viewState.inputBase == InputBase .HEX ) InputTitleContentSize
114
123
else InputNormalFontSize ,
115
- fontWeight = if (viewState.inputBase == InputBase .HEX ) FontWeight .Bold else null
124
+ fontWeight = if (viewState.inputBase == InputBase .HEX ) FontWeight .Bold else null ,
125
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
116
126
)
117
127
118
128
Text (
119
129
text = viewState.inputHexText.formatNumber(addSplitChar = " " , splitLength = 4 ),
120
130
fontSize = InputNormalFontSize ,
121
- modifier = Modifier .padding(start = 8 .dp)
131
+ modifier = Modifier .padding(start = 8 .dp),
132
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
122
133
)
123
134
}
124
135
Row (verticalAlignment = Alignment .CenterVertically ,
@@ -131,13 +142,15 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
131
142
fontSize =
132
143
if (viewState.inputBase == InputBase .DEC ) InputTitleContentSize
133
144
else InputNormalFontSize ,
134
- fontWeight = if (viewState.inputBase == InputBase .DEC ) FontWeight .Bold else null
145
+ fontWeight = if (viewState.inputBase == InputBase .DEC ) FontWeight .Bold else null ,
146
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
135
147
)
136
148
137
149
Text (
138
150
text = viewState.inputDecText.formatNumber(),
139
151
fontSize = InputNormalFontSize ,
140
- modifier = Modifier .padding(start = 8 .dp)
152
+ modifier = Modifier .padding(start = 8 .dp),
153
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
141
154
)
142
155
}
143
156
Row (verticalAlignment = Alignment .CenterVertically ,
@@ -150,13 +163,15 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
150
163
fontSize =
151
164
if (viewState.inputBase == InputBase .OCT ) InputTitleContentSize
152
165
else InputNormalFontSize ,
153
- fontWeight = if (viewState.inputBase == InputBase .OCT ) FontWeight .Bold else null
166
+ fontWeight = if (viewState.inputBase == InputBase .OCT ) FontWeight .Bold else null ,
167
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
154
168
)
155
169
156
170
Text (
157
171
text = viewState.inputOctText.formatNumber(addSplitChar = " " ),
158
172
fontSize = InputNormalFontSize ,
159
- modifier = Modifier .padding(start = 8 .dp)
173
+ modifier = Modifier .padding(start = 8 .dp),
174
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
160
175
)
161
176
}
162
177
Row (verticalAlignment = Alignment .CenterVertically ,
@@ -169,26 +184,28 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
169
184
fontSize =
170
185
if (viewState.inputBase == InputBase .BIN ) InputTitleContentSize
171
186
else InputNormalFontSize ,
172
- fontWeight = if (viewState.inputBase == InputBase .BIN ) FontWeight .Bold else null
187
+ fontWeight = if (viewState.inputBase == InputBase .BIN ) FontWeight .Bold else null ,
188
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
173
189
)
174
190
175
191
Text (
176
192
text = viewState.inputBinText.formatNumber(addSplitChar = " " , splitLength = 4 , isAddLeadingZero = viewState.inputBinText != " 0" ),
177
193
fontSize = InputNormalFontSize ,
178
194
modifier = Modifier
179
195
.padding(start = 8 .dp),
196
+ color = if (MaterialTheme .colors.isLight) Color .Unspecified else MaterialTheme .colors.primary
180
197
)
181
198
}
182
199
}
183
200
}
184
201
}
185
202
186
203
@Composable
187
- private fun LeftKeyBoard (viewModel : ProgrammerViewModel ) {
204
+ private fun NumberBoard (viewModel : ProgrammerViewModel ) {
188
205
val viewState = viewModel.viewStates
189
206
190
207
Column (modifier = Modifier .fillMaxSize()) {
191
- for (btnRow in programmerLeftKeyBoardBtn ()) {
208
+ for (btnRow in programmerNumberKeyBoardBtn ()) {
192
209
Row (modifier = Modifier
193
210
.fillMaxWidth()
194
211
.weight(1f )) {
@@ -206,6 +223,7 @@ private fun LeftKeyBoard(viewModel: ProgrammerViewModel) {
206
223
onClick = { viewModel.dispatch(ProgrammerAction .ClickBtn (btn.index)) },
207
224
isAvailable = isAvailable,
208
225
backGround = btn.background,
226
+ isFilled = btn.isFilled,
209
227
paddingValues = PaddingValues (0.5 .dp)
210
228
)
211
229
}
@@ -216,11 +234,11 @@ private fun LeftKeyBoard(viewModel: ProgrammerViewModel) {
216
234
}
217
235
218
236
@Composable
219
- private fun RightKeyBoard (viewModel : ProgrammerViewModel ) {
237
+ private fun FunctionKeyBoard (viewModel : ProgrammerViewModel ) {
220
238
val viewState = viewModel.viewStates
221
239
222
240
Column (modifier = Modifier .fillMaxSize()) {
223
- for (btnRow in programmerRightKeyBoardBtn ()) {
241
+ for (btnRow in programmerFunctionKeyBoardBtn ()) {
224
242
Row (modifier = Modifier
225
243
.fillMaxWidth()
226
244
.weight(1f )) {
@@ -238,6 +256,7 @@ private fun RightKeyBoard(viewModel: ProgrammerViewModel) {
238
256
onClick = { viewModel.dispatch(ProgrammerAction .ClickBtn (btn.index)) },
239
257
isAvailable = isAvailable,
240
258
backGround = btn.background,
259
+ isFilled = btn.isFilled,
241
260
paddingValues = PaddingValues (0.5 .dp)
242
261
)
243
262
}
@@ -254,19 +273,28 @@ private fun KeyBoardButton(
254
273
onClick : () -> Unit ,
255
274
isAvailable : Boolean = true,
256
275
backGround : Color = Color .White ,
276
+ isFilled : Boolean = false,
257
277
paddingValues : PaddingValues = PaddingValues (0.dp)
258
278
) {
259
279
Card (
260
280
onClick = { onClick() },
261
281
modifier = Modifier
262
282
.fillMaxSize()
263
283
.padding(paddingValues),
264
- backgroundColor = backGround,
284
+ backgroundColor = if (isFilled) backGround else MaterialTheme .colors.surface ,
265
285
shape = MaterialTheme .shapes.large,
286
+ elevation = 0 .dp,
287
+ border = BorderStroke (0 .dp, Color .Transparent ),
266
288
enabled = isAvailable
267
289
) {
268
290
Row (Modifier .fillMaxSize(), horizontalArrangement = Arrangement .Center , verticalAlignment = Alignment .CenterVertically ) {
269
- Text (text, fontSize = 24 .sp, color = if (isAvailable) Color .Unspecified else Color .LightGray )
291
+ Text (
292
+ text,
293
+ fontSize = 24 .sp,
294
+ color = if (isAvailable) {
295
+ if (isFilled) Color .Unspecified else backGround
296
+ } else Color .DarkGray
297
+ )
270
298
}
271
299
}
272
300
}
0 commit comments