@@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.BoxScope
77import androidx.compose.foundation.layout.BoxWithConstraints
88import androidx.compose.foundation.layout.fillMaxWidth
99import androidx.compose.foundation.layout.height
10+ import androidx.compose.foundation.layout.navigationBarsPadding
1011import androidx.compose.foundation.lazy.grid.GridCells
1112import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
1213import androidx.compose.foundation.lazy.grid.items
@@ -71,13 +72,21 @@ fun NumberPad(
7172 modifier : Modifier = Modifier ,
7273 type : NumberPadType = NumberPadType .SIMPLE ,
7374 availableHeight : Dp = defaultHeight,
75+ decimalSeparator : String = KEY_DECIMAL ,
7476 errorKey : String? = null,
77+ includeNavigationBarsPadding : Boolean = false,
78+ onDeleteLongPress : (() -> Unit )? = null,
7579) {
7680 val focusRequester = remember { FocusRequester () }
7781 LaunchedEffect (Unit ) { focusRequester.requestFocus() }
82+ val safeAreaModifier = if (includeNavigationBarsPadding) {
83+ modifier.navigationBarsPadding()
84+ } else {
85+ modifier
86+ }
7887
7988 BoxWithConstraints (
80- modifier = modifier
89+ modifier = safeAreaModifier
8190 .focusRequester(focusRequester)
8291 .onPreviewKeyEvent { keyEvent ->
8392 if (keyEvent.type != KeyEventType .KeyDown ) return @onPreviewKeyEvent false
@@ -124,9 +133,10 @@ fun NumberPad(
124133 )
125134
126135 NumberPadType .DECIMAL -> NumberPadKeyButton (
127- text = KEY_DECIMAL ,
136+ text = decimalSeparator ,
128137 onPress = onPress,
129138 height = buttonHeight,
139+ key = KEY_DECIMAL ,
130140 hasError = errorKey == KEY_DECIMAL ,
131141 testTag = " NDecimal" ,
132142 )
@@ -143,6 +153,7 @@ fun NumberPad(
143153 item {
144154 NumberPadDeleteButton (
145155 onPress = { onPress(KEY_DELETE ) },
156+ onLongPress = onDeleteLongPress,
146157 height = buttonHeight,
147158 modifier = Modifier .testTag(" NRemove" ),
148159 )
@@ -161,14 +172,19 @@ fun NumberPad(
161172 currencies : CurrencyState = LocalCurrencies .current,
162173 type : NumberPadType = viewModel.getNumberPadType(currencies),
163174 availableHeight : Dp = defaultHeight,
175+ decimalSeparator : String = KEY_DECIMAL ,
176+ includeNavigationBarsPadding : Boolean = false,
164177) {
165178 val uiState by viewModel.uiState.collectAsStateWithLifecycle()
166179 NumberPad (
167180 onPress = { key -> viewModel.handleNumberPadInput(key, currencies) },
168181 modifier = modifier,
169182 type = type,
170183 availableHeight = availableHeight,
184+ decimalSeparator = decimalSeparator,
171185 errorKey = uiState.errorKey,
186+ includeNavigationBarsPadding = includeNavigationBarsPadding,
187+ onDeleteLongPress = viewModel::clearInput,
172188 )
173189}
174190
@@ -186,7 +202,7 @@ private val hardwareKeyMap = mapOf(
186202 Key .Eight to " 8" , Key .NumPad8 to " 8" ,
187203 Key .Nine to " 9" , Key .NumPad9 to " 9" ,
188204 Key .Backspace to KEY_DELETE , Key .Delete to KEY_DELETE ,
189- Key .Period to KEY_DECIMAL , Key .NumPadDot to KEY_DECIMAL ,
205+ Key .Period to KEY_DECIMAL , Key .NumPadDot to KEY_DECIMAL , Key . Comma to KEY_DECIMAL ,
190206)
191207
192208private fun mapHardwareKey (key : Key , type : NumberPadType ): String? {
@@ -201,11 +217,12 @@ fun NumberPadKeyButton(
201217 onPress : (String ) -> Unit ,
202218 height : Dp ,
203219 modifier : Modifier = Modifier ,
220+ key : String = text,
204221 hasError : Boolean = false,
205222 testTag : String = "N $text",
206223) {
207224 NumberPadKey (
208- onClick = { onPress(text ) },
225+ onClick = { onPress(key ) },
209226 height = height,
210227 haptic = if (hasError) errorHaptic else pressHaptic,
211228 modifier = modifier.testTag(testTag),
@@ -228,13 +245,15 @@ internal fun NumberPadDeleteButton(
228245 onPress : () -> Unit ,
229246 height : Dp ,
230247 modifier : Modifier = Modifier ,
248+ onLongPress : (() -> Unit )? = null,
231249) {
232250 NumberPadKeyIcon (
233251 icon = R .drawable.ic_backspace,
234252 contentDescription = stringResource(R .string.common__delete),
235253 onClick = onPress,
254+ onLongClick = onLongPress,
236255 height = height,
237- modifier = modifier,
256+ modifier = modifier
238257 )
239258}
240259
@@ -245,11 +264,13 @@ fun NumberPadKeyIcon(
245264 onClick : () -> Unit ,
246265 height : Dp ,
247266 modifier : Modifier = Modifier ,
267+ onLongClick : (() -> Unit )? = null,
248268) {
249269 NumberPadKey (
250270 onClick = onClick,
271+ onLongClick = onLongClick,
251272 height = height,
252- modifier = modifier,
273+ modifier = modifier
253274 ) {
254275 Icon (
255276 painter = painterResource(icon),
@@ -264,6 +285,7 @@ fun NumberPadKey(
264285 height : Dp ,
265286 modifier : Modifier = Modifier ,
266287 haptic : HapticFeedbackType = pressHaptic,
288+ onLongClick : (() -> Unit )? = null,
267289 content : @Composable (BoxScope .() -> Unit ),
268290) {
269291 val haptics = LocalHapticFeedback .current
@@ -273,10 +295,20 @@ fun NumberPadKey(
273295 modifier = modifier
274296 .height(height)
275297 .fillMaxWidth()
276- .clickableAlpha(ALPHA_PRESSED , debounce = Duration .ZERO ) {
277- haptics.performHapticFeedback(haptic)
278- onClick()
279- },
298+ .clickableAlpha(
299+ pressedAlpha = ALPHA_PRESSED ,
300+ debounce = Duration .ZERO ,
301+ onLongClick = onLongClick?.let {
302+ {
303+ haptics.performHapticFeedback(haptic)
304+ it()
305+ }
306+ },
307+ onClick = {
308+ haptics.performHapticFeedback(haptic)
309+ onClick()
310+ },
311+ ),
280312 )
281313}
282314
0 commit comments