-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNumberPad.kt
More file actions
445 lines (420 loc) · 13 KB
/
Copy pathNumberPad.kt
File metadata and controls
445 lines (420 loc) · 13 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package to.bitkit.ui.components
import androidx.annotation.DrawableRes
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Devices.NEXUS_5
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import to.bitkit.R
import to.bitkit.models.BitcoinDisplayUnit
import to.bitkit.models.PrimaryDisplay
import to.bitkit.repositories.CurrencyState
import to.bitkit.ui.LocalCurrencies
import to.bitkit.ui.scaffold.ScreenColumn
import to.bitkit.ui.shared.modifiers.clickableAlpha
import to.bitkit.ui.theme.AppThemeSurface
import to.bitkit.ui.theme.Colors
import to.bitkit.viewmodels.AmountInputViewModel
import to.bitkit.viewmodels.previewAmountInputViewModel
import kotlin.time.Duration
const val KEY_DELETE = "delete"
const val KEY_000 = "000"
const val KEY_DECIMAL = "."
private val defaultHeight = 300.dp
private val idealButtonHeight = 75.dp
private val minButtonHeight = 50.dp
private const val ROWS = 4
private const val COLUMNS = 3
private const val ALPHA_PRESSED = 0.2f
private val pressHaptic = HapticFeedbackType.VirtualKey
private val errorHaptic = HapticFeedbackType.Reject
/**
* Numeric keyboard.
*/
@Composable
fun NumberPad(
onPress: (String) -> Unit,
modifier: Modifier = Modifier,
type: NumberPadType = NumberPadType.SIMPLE,
availableHeight: Dp = defaultHeight,
decimalSeparator: String = KEY_DECIMAL,
errorKey: String? = null,
includeNavigationBarsPadding: Boolean = false,
onDeleteLongPress: (() -> Unit)? = null,
) {
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) { focusRequester.requestFocus() }
val safeAreaModifier = if (includeNavigationBarsPadding) {
modifier.navigationBarsPadding()
} else {
modifier
}
BoxWithConstraints(
modifier = safeAreaModifier
.focusRequester(focusRequester)
.onPreviewKeyEvent { keyEvent ->
if (keyEvent.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
val mapped = mapHardwareKey(keyEvent.key, type) ?: return@onPreviewKeyEvent false
onPress(mapped)
true
}
.focusable()
) {
val buttonHeight = when {
constraints.hasFixedHeight -> maxHeight / ROWS
else -> (availableHeight / ROWS).coerceIn(minButtonHeight, idealButtonHeight)
}
val totalKeyboardHeight = buttonHeight * ROWS
LazyVerticalGrid(
columns = GridCells.Fixed(COLUMNS),
userScrollEnabled = false,
modifier = Modifier.height(totalKeyboardHeight),
) {
items((1..9).map { "$it" }) { number ->
NumberPadKeyButton(
text = number,
onPress = onPress,
height = buttonHeight,
hasError = errorKey == number,
)
}
item {
when (type) {
NumberPadType.SIMPLE -> Box(
modifier = Modifier
.height(buttonHeight)
.fillMaxWidth()
)
NumberPadType.INTEGER -> NumberPadKeyButton(
text = KEY_000,
onPress = onPress,
height = buttonHeight,
hasError = errorKey == KEY_000,
testTag = "N000",
)
NumberPadType.DECIMAL -> NumberPadKeyButton(
text = decimalSeparator,
onPress = onPress,
height = buttonHeight,
key = KEY_DECIMAL,
hasError = errorKey == KEY_DECIMAL,
testTag = "NDecimal",
)
}
}
item {
NumberPadKeyButton(
text = "0",
onPress = onPress,
height = buttonHeight,
hasError = errorKey == "0",
)
}
item {
NumberPadDeleteButton(
onPress = { onPress(KEY_DELETE) },
onLongPress = onDeleteLongPress,
height = buttonHeight,
modifier = Modifier.testTag("NRemove"),
)
}
}
}
}
/**
* Numeric keyboard for amount input. Can be used together with [NumberPadTextField].
*/
@Composable
fun NumberPad(
viewModel: AmountInputViewModel,
modifier: Modifier = Modifier,
currencies: CurrencyState = LocalCurrencies.current,
type: NumberPadType = viewModel.getNumberPadType(currencies),
availableHeight: Dp = defaultHeight,
decimalSeparator: String = KEY_DECIMAL,
includeNavigationBarsPadding: Boolean = false,
) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
NumberPad(
onPress = { key -> viewModel.handleNumberPadInput(key, currencies) },
modifier = modifier,
type = type,
availableHeight = availableHeight,
decimalSeparator = decimalSeparator,
errorKey = uiState.errorKey,
includeNavigationBarsPadding = includeNavigationBarsPadding,
onDeleteLongPress = viewModel::clearInput,
)
}
enum class NumberPadType { SIMPLE, INTEGER, DECIMAL }
private val hardwareKeyMap = mapOf(
Key.Zero to "0", Key.NumPad0 to "0",
Key.One to "1", Key.NumPad1 to "1",
Key.Two to "2", Key.NumPad2 to "2",
Key.Three to "3", Key.NumPad3 to "3",
Key.Four to "4", Key.NumPad4 to "4",
Key.Five to "5", Key.NumPad5 to "5",
Key.Six to "6", Key.NumPad6 to "6",
Key.Seven to "7", Key.NumPad7 to "7",
Key.Eight to "8", Key.NumPad8 to "8",
Key.Nine to "9", Key.NumPad9 to "9",
Key.Backspace to KEY_DELETE, Key.Delete to KEY_DELETE,
Key.Period to KEY_DECIMAL, Key.NumPadDot to KEY_DECIMAL, Key.Comma to KEY_DECIMAL,
)
private fun mapHardwareKey(key: Key, type: NumberPadType): String? {
val mapped = hardwareKeyMap[key] ?: return null
if (mapped == KEY_DECIMAL && type != NumberPadType.DECIMAL) return null
return mapped
}
@Composable
fun NumberPadKeyButton(
text: String,
onPress: (String) -> Unit,
height: Dp,
modifier: Modifier = Modifier,
key: String = text,
hasError: Boolean = false,
testTag: String = "N$text",
) {
NumberPadKey(
onClick = { onPress(key) },
height = height,
haptic = if (hasError) errorHaptic else pressHaptic,
modifier = modifier.testTag(testTag),
) {
Text(
text = text,
fontSize = when {
height < 60.dp -> 20.sp
height < 70.dp -> 22.sp
else -> 24.sp
},
textAlign = TextAlign.Center,
color = if (hasError) Colors.Red else Colors.White,
)
}
}
@Composable
internal fun NumberPadDeleteButton(
onPress: () -> Unit,
height: Dp,
modifier: Modifier = Modifier,
onLongPress: (() -> Unit)? = null,
) {
NumberPadKeyIcon(
icon = R.drawable.ic_backspace,
contentDescription = stringResource(R.string.common__delete),
onClick = onPress,
onLongClick = onLongPress,
height = height,
modifier = modifier
)
}
@Composable
fun NumberPadKeyIcon(
@DrawableRes icon: Int,
contentDescription: String?,
onClick: () -> Unit,
height: Dp,
modifier: Modifier = Modifier,
onLongClick: (() -> Unit)? = null,
) {
NumberPadKey(
onClick = onClick,
onLongClick = onLongClick,
height = height,
modifier = modifier
) {
Icon(
painter = painterResource(icon),
contentDescription = contentDescription,
)
}
}
@Composable
fun NumberPadKey(
onClick: () -> Unit,
height: Dp,
modifier: Modifier = Modifier,
haptic: HapticFeedbackType = pressHaptic,
onLongClick: (() -> Unit)? = null,
content: @Composable (BoxScope.() -> Unit),
) {
val haptics = LocalHapticFeedback.current
Box(
content = content,
contentAlignment = Alignment.Center,
modifier = modifier
.height(height)
.fillMaxWidth()
.clickableAlpha(
pressedAlpha = ALPHA_PRESSED,
debounce = Duration.ZERO,
onLongClick = onLongClick?.let {
{
haptics.performHapticFeedback(haptic)
it()
}
},
onClick = {
haptics.performHapticFeedback(haptic)
onClick()
},
),
)
}
@Preview(showSystemUi = true)
@Composable
private fun Preview() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
viewModel = previewAmountInputViewModel(),
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewClassic() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
viewModel = previewAmountInputViewModel(),
currencies = CurrencyState(
displayUnit = BitcoinDisplayUnit.CLASSIC,
),
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewFiat() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
viewModel = previewAmountInputViewModel(),
currencies = CurrencyState(
primaryDisplay = PrimaryDisplay.FIAT,
),
)
}
}
}
@Preview(showSystemUi = true, device = NEXUS_5)
@Composable
private fun PreviewSmall() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
viewModel = previewAmountInputViewModel(),
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewSimple() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
onPress = {},
type = NumberPadType.SIMPLE,
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewHeight() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
onPress = {},
type = NumberPadType.SIMPLE,
modifier = Modifier.height(350.dp),
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewMaxHeight() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
onPress = {},
type = NumberPadType.SIMPLE,
availableHeight = 350.dp,
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewHeightXs() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
onPress = {},
type = NumberPadType.SIMPLE,
modifier = Modifier.height(100.dp),
)
}
}
}
@Preview(showSystemUi = true)
@Composable
private fun PreviewMaxHeightXs() {
AppThemeSurface {
ScreenColumn {
FillHeight()
NumberPad(
onPress = {},
type = NumberPadType.SIMPLE,
availableHeight = 100.dp,
)
}
}
}