Skip to content

Commit 440ab51

Browse files
committed
refactor: simplify number pad input
1 parent 42eb523 commit 440ab51

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

app/src/main/java/to/bitkit/ui/components/NumberPad.kt

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private const val ALPHA_PRESSED = 0.2f
6868
private val pressHaptic = HapticFeedbackType.VirtualKey
6969
private val errorHaptic = HapticFeedbackType.Reject
7070

71+
typealias OnKeyPress = (key: String) -> Unit
72+
7173
/** Pad dimming when disabled — white keys render at White64. */
7274
private const val DISABLED_ALPHA = 0.64f
7375

@@ -76,7 +78,7 @@ private const val DISABLED_ALPHA = 0.64f
7678
*/
7779
@Composable
7880
fun NumberPad(
79-
onPress: (String) -> Unit,
81+
onPress: OnKeyPress,
8082
modifier: Modifier = Modifier,
8183
type: NumberPadType = NumberPadType.SIMPLE,
8284
availableHeight: Dp = defaultHeight,
@@ -88,29 +90,25 @@ fun NumberPad(
8890
) {
8991
val focusRequester = remember { FocusRequester() }
9092
LaunchedEffect(Unit) {
91-
// Composing mid sheet/nav transition can drop the initial request, leaving
92-
// hardware keyboard input dead; retry briefly until the focus node takes it.
93+
// Composing mid-transition can drop the initial request, leaving input dead;
94+
// retry briefly until the focus node takes it.
9395
repeat(FOCUS_RETRY_COUNT) {
9496
if (runCatching { focusRequester.requestFocus() }.isSuccess) return@LaunchedEffect
9597
delay(FOCUS_RETRY_DELAY)
9698
}
9799
}
98-
val safeAreaModifier = if (includeNavigationBarsPadding) {
99-
modifier.navigationBarsPadding()
100-
} else {
101-
modifier
102-
}
103100
// Disabled: no-op input and dim the keys to White64.
104-
val keyOnPress: (String) -> Unit = if (enabled) onPress else { _ -> }
101+
val onPressIfEnabled: OnKeyPress = if (enabled) onPress else { _ -> }
105102

106103
BoxWithConstraints(
107-
modifier = safeAreaModifier
104+
modifier = modifier
105+
.let { if (includeNavigationBarsPadding) it.navigationBarsPadding() else it }
108106
.alpha(if (enabled) 1f else DISABLED_ALPHA)
109107
.focusRequester(focusRequester)
110108
.onPreviewKeyEvent { keyEvent ->
111109
if (keyEvent.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
112110
val mapped = mapHardwareKey(keyEvent.key, type) ?: return@onPreviewKeyEvent false
113-
keyOnPress(mapped)
111+
onPressIfEnabled(mapped)
114112
true
115113
}
116114
.focusable()
@@ -130,7 +128,7 @@ fun NumberPad(
130128
items((1..9).map { "$it" }) { number ->
131129
NumberPadKeyButton(
132130
text = number,
133-
onPress = keyOnPress,
131+
onPress = onPressIfEnabled,
134132
height = buttonHeight,
135133
hasError = errorKey == number,
136134
)
@@ -145,15 +143,15 @@ fun NumberPad(
145143

146144
NumberPadType.INTEGER -> NumberPadKeyButton(
147145
text = KEY_000,
148-
onPress = keyOnPress,
146+
onPress = onPressIfEnabled,
149147
height = buttonHeight,
150148
hasError = errorKey == KEY_000,
151149
testTag = "N000",
152150
)
153151

154152
NumberPadType.DECIMAL -> NumberPadKeyButton(
155153
text = decimalSeparator,
156-
onPress = keyOnPress,
154+
onPress = onPressIfEnabled,
157155
height = buttonHeight,
158156
key = KEY_DECIMAL,
159157
hasError = errorKey == KEY_DECIMAL,
@@ -164,14 +162,14 @@ fun NumberPad(
164162
item {
165163
NumberPadKeyButton(
166164
text = "0",
167-
onPress = keyOnPress,
165+
onPress = onPressIfEnabled,
168166
height = buttonHeight,
169167
hasError = errorKey == "0",
170168
)
171169
}
172170
item {
173171
NumberPadDeleteButton(
174-
onPress = { keyOnPress(KEY_DELETE) },
172+
onPress = { onPressIfEnabled(KEY_DELETE) },
175173
onLongPress = onDeleteLongPress,
176174
height = buttonHeight,
177175
modifier = Modifier.testTag("NRemove"),
@@ -234,7 +232,7 @@ private fun mapHardwareKey(key: Key, type: NumberPadType): String? {
234232
@Composable
235233
fun NumberPadKeyButton(
236234
text: String,
237-
onPress: (String) -> Unit,
235+
onPress: OnKeyPress,
238236
height: Dp,
239237
modifier: Modifier = Modifier,
240238
key: String = text,

0 commit comments

Comments
 (0)