Skip to content

Commit d5dd9f5

Browse files
authored
Merge pull request #296 from synonymdev/fix/adapt-height-keyboard-2
Adapt keyboard to short screens
2 parents 9dda8fb + 489bc55 commit d5dd9f5

File tree

3 files changed

+239
-112
lines changed

3 files changed

+239
-112
lines changed

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

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import androidx.annotation.DrawableRes
44
import androidx.compose.foundation.layout.Arrangement
55
import androidx.compose.foundation.layout.Box
66
import androidx.compose.foundation.layout.BoxScope
7+
import androidx.compose.foundation.layout.BoxWithConstraints
78
import androidx.compose.foundation.layout.Column
89
import androidx.compose.foundation.layout.fillMaxSize
910
import androidx.compose.foundation.layout.fillMaxWidth
10-
import androidx.compose.foundation.layout.heightIn
11+
import androidx.compose.foundation.layout.height
1112
import androidx.compose.foundation.lazy.grid.GridCells
1213
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
1314
import androidx.compose.material3.Icon
@@ -22,15 +23,21 @@ import androidx.compose.ui.res.painterResource
2223
import androidx.compose.ui.res.stringResource
2324
import androidx.compose.ui.text.style.TextAlign
2425
import androidx.compose.ui.tooling.preview.Devices
26+
import androidx.compose.ui.tooling.preview.Devices.NEXUS_5
2527
import androidx.compose.ui.tooling.preview.Preview
28+
import androidx.compose.ui.unit.Dp
2629
import androidx.compose.ui.unit.dp
2730
import androidx.compose.ui.unit.sp
2831
import to.bitkit.R
2932
import to.bitkit.ui.shared.util.clickableAlpha
3033
import to.bitkit.ui.theme.AppThemeSurface
3134
import to.bitkit.ui.theme.Colors
3235

33-
private val buttonHeight = 75.dp // 75 * 4 = 300 height
36+
private val maxKeyboardHeight = 300.dp
37+
private val idealButtonHeight = 75.dp
38+
private val minButtonHeight = 50.dp
39+
private const val KEYBOARD_ROWS_NUMBER = 4
40+
private const val KEYBOARD_COLUMNS_NUMBER = 3
3441
val keyButtonHaptic = HapticFeedbackType.VirtualKey
3542

3643
@Composable
@@ -39,30 +46,51 @@ fun Keyboard(
3946
onClickBackspace: () -> Unit,
4047
modifier: Modifier = Modifier,
4148
isDecimal: Boolean = true,
49+
availableHeight: Dp? = null,
4250
) {
43-
LazyVerticalGrid(
44-
columns = GridCells.Fixed(3),
45-
userScrollEnabled = false,
46-
modifier = modifier,
47-
) {
48-
item { KeyTextButton(text = "1", onClick = onClick) }
49-
item { KeyTextButton(text = "2", onClick = onClick) }
50-
item { KeyTextButton(text = "3", onClick = onClick) }
51-
item { KeyTextButton(text = "4", onClick = onClick) }
52-
item { KeyTextButton(text = "5", onClick = onClick) }
53-
item { KeyTextButton(text = "6", onClick = onClick) }
54-
item { KeyTextButton(text = "7", onClick = onClick) }
55-
item { KeyTextButton(text = "8", onClick = onClick) }
56-
item { KeyTextButton(text = "9", onClick = onClick) }
57-
item { KeyTextButton(text = if (isDecimal) "." else "000", onClick = onClick) }
58-
item { KeyTextButton(text = "0", onClick = onClick) }
59-
item {
60-
KeyIconButton(
61-
icon = R.drawable.ic_backspace,
62-
contentDescription = stringResource(R.string.common__delete),
63-
onClick = onClickBackspace,
64-
modifier = Modifier.testTag("KeyboardButton_backspace"),
65-
)
51+
BoxWithConstraints(modifier = modifier) {
52+
val constraintsHeight = this.maxHeight
53+
val effectiveHeight = availableHeight ?: constraintsHeight
54+
val idealTotalHeight = idealButtonHeight * KEYBOARD_ROWS_NUMBER
55+
56+
val maxAllowedHeight = minOf(maxKeyboardHeight, effectiveHeight)
57+
58+
val buttonHeight = when {
59+
// If we have plenty of space, use ideal height
60+
maxAllowedHeight >= idealTotalHeight -> idealButtonHeight
61+
// If space is limited, calculate proportional height but ensure minimum
62+
maxAllowedHeight >= (minButtonHeight * KEYBOARD_ROWS_NUMBER) -> maxAllowedHeight / KEYBOARD_ROWS_NUMBER
63+
// If extremely limited, use absolute minimum
64+
else -> minButtonHeight
65+
}
66+
67+
val totalKeyboardHeight = buttonHeight * KEYBOARD_ROWS_NUMBER
68+
69+
LazyVerticalGrid(
70+
columns = GridCells.Fixed(KEYBOARD_COLUMNS_NUMBER),
71+
userScrollEnabled = false,
72+
modifier = Modifier.height(totalKeyboardHeight),
73+
) {
74+
item { KeyTextButton(text = "1", onClick = onClick, buttonHeight = buttonHeight) }
75+
item { KeyTextButton(text = "2", onClick = onClick, buttonHeight = buttonHeight) }
76+
item { KeyTextButton(text = "3", onClick = onClick, buttonHeight = buttonHeight) }
77+
item { KeyTextButton(text = "4", onClick = onClick, buttonHeight = buttonHeight) }
78+
item { KeyTextButton(text = "5", onClick = onClick, buttonHeight = buttonHeight) }
79+
item { KeyTextButton(text = "6", onClick = onClick, buttonHeight = buttonHeight) }
80+
item { KeyTextButton(text = "7", onClick = onClick, buttonHeight = buttonHeight) }
81+
item { KeyTextButton(text = "8", onClick = onClick, buttonHeight = buttonHeight) }
82+
item { KeyTextButton(text = "9", onClick = onClick, buttonHeight = buttonHeight) }
83+
item { KeyTextButton(text = if (isDecimal) "." else "000", onClick = onClick, buttonHeight = buttonHeight) }
84+
item { KeyTextButton(text = "0", onClick = onClick, buttonHeight = buttonHeight) }
85+
item {
86+
KeyIconButton(
87+
icon = R.drawable.ic_backspace,
88+
contentDescription = stringResource(R.string.common__delete),
89+
onClick = onClickBackspace,
90+
buttonHeight = buttonHeight,
91+
modifier = Modifier.testTag("KeyboardButton_backspace"),
92+
)
93+
}
6694
}
6795
}
6896
}
@@ -72,10 +100,12 @@ fun KeyIconButton(
72100
@DrawableRes icon: Int,
73101
contentDescription: String?,
74102
onClick: () -> Unit,
103+
buttonHeight: Dp = idealButtonHeight,
75104
modifier: Modifier = Modifier,
76105
) {
77106
KeyButtonBox(
78107
onClick = onClick,
108+
buttonHeight = buttonHeight,
79109
modifier = modifier,
80110
) {
81111
Icon(
@@ -89,15 +119,21 @@ fun KeyIconButton(
89119
fun KeyTextButton(
90120
text: String,
91121
onClick: (String) -> Unit,
122+
buttonHeight: Dp = idealButtonHeight,
92123
modifier: Modifier = Modifier,
93124
) {
94125
KeyButtonBox(
95126
onClick = { onClick(text) },
127+
buttonHeight = buttonHeight,
96128
modifier = modifier.testTag("KeyboardButton_$text"),
97129
) {
98130
Text(
99131
text = text,
100-
fontSize = 24.sp,
132+
fontSize = when {
133+
buttonHeight < 60.dp -> 20.sp
134+
buttonHeight < 70.dp -> 22.sp
135+
else -> 24.sp
136+
},
101137
textAlign = TextAlign.Center,
102138
color = Colors.White,
103139
)
@@ -107,6 +143,7 @@ fun KeyTextButton(
107143
@Composable
108144
private fun KeyButtonBox(
109145
onClick: () -> Unit,
146+
buttonHeight: Dp,
110147
modifier: Modifier = Modifier,
111148
content: @Composable (BoxScope.() -> Unit),
112149
) {
@@ -115,8 +152,8 @@ private fun KeyButtonBox(
115152
content = content,
116153
contentAlignment = Alignment.Center,
117154
modifier = modifier
118-
.heightIn(buttonHeight)
119-
.fillMaxSize()
155+
.height(buttonHeight)
156+
.fillMaxWidth()
120157
.clickableAlpha(0.2f) {
121158
haptic.performHapticFeedback(keyButtonHaptic)
122159
onClick()
@@ -167,3 +204,17 @@ private fun Preview3() {
167204
}
168205
}
169206
}
207+
208+
@Preview(showBackground = true, device = NEXUS_5)
209+
@Composable
210+
private fun PreviewShortScreen() {
211+
AppThemeSurface {
212+
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
213+
Keyboard(
214+
onClick = {},
215+
onClickBackspace = {},
216+
modifier = Modifier.fillMaxWidth(),
217+
)
218+
}
219+
}
220+
}

app/src/main/java/to/bitkit/ui/screens/wallets/receive/EditInvoiceScreen.kt

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import androidx.compose.animation.slideInVertically
88
import androidx.compose.animation.slideOutVertically
99
import androidx.compose.foundation.Image
1010
import androidx.compose.foundation.layout.Arrangement
11-
import androidx.compose.foundation.layout.Box
11+
import androidx.compose.foundation.layout.BoxWithConstraints
1212
import androidx.compose.foundation.layout.Column
1313
import androidx.compose.foundation.layout.FlowRow
1414
import androidx.compose.foundation.layout.Row
@@ -18,6 +18,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
1818
import androidx.compose.foundation.layout.height
1919
import androidx.compose.foundation.layout.navigationBarsPadding
2020
import androidx.compose.foundation.layout.padding
21+
import androidx.compose.foundation.layout.sizeIn
2122
import androidx.compose.foundation.text.KeyboardOptions
2223
import androidx.compose.material3.HorizontalDivider
2324
import androidx.compose.material3.Icon
@@ -37,6 +38,7 @@ import androidx.compose.ui.platform.testTag
3738
import androidx.compose.ui.res.painterResource
3839
import androidx.compose.ui.res.stringResource
3940
import androidx.compose.ui.text.input.ImeAction
41+
import androidx.compose.ui.tooling.preview.Devices.PIXEL_5
4042
import androidx.compose.ui.tooling.preview.Preview
4143
import androidx.compose.ui.unit.dp
4244
import androidx.hilt.navigation.compose.hiltViewModel
@@ -176,12 +178,14 @@ fun EditInvoiceContent(
176178
onInputChanged: (String) -> Unit,
177179
modifier: Modifier = Modifier,
178180
) {
179-
Box(
181+
BoxWithConstraints(
180182
modifier = modifier
181183
.fillMaxSize()
182184
.gradientBackground()
183185
.navigationBarsPadding()
184186
) {
187+
val maxHeight = this.maxHeight
188+
185189
AnimatedVisibility(
186190
visible = !numericKeyboardVisible && !isSoftKeyboardVisible,
187191
enter = fadeIn(),
@@ -252,7 +256,7 @@ fun EditInvoiceContent(
252256
UnitButton(modifier = Modifier.height(28.dp))
253257
}
254258

255-
HorizontalDivider(modifier = Modifier.padding(vertical = 24.dp))
259+
HorizontalDivider(modifier = Modifier.padding(top = 24.dp))
256260

257261
Keyboard(
258262
onClick = { number ->
@@ -262,12 +266,17 @@ fun EditInvoiceContent(
262266
onInputChanged(if (input.length > 1) input.dropLast(1) else "0")
263267
},
264268
isDecimal = primaryDisplay == PrimaryDisplay.FIAT,
269+
availableHeight = maxHeight,
265270
modifier = Modifier
266271
.fillMaxWidth()
267272
.testTag("amount_keyboard"),
268273
)
269274

270-
Spacer(modifier = Modifier.height(41.dp))
275+
Spacer(
276+
modifier = Modifier
277+
.weight(1f)
278+
.sizeIn(minHeight = 16.dp, maxHeight = 41.dp)
279+
)
271280

272281
PrimaryButton(
273282
text = stringResource(R.string.common__continue),
@@ -444,3 +453,30 @@ private fun Preview3() {
444453
}
445454
}
446455
}
456+
457+
@Preview(showSystemUi = true, device = PIXEL_5)
458+
@Composable
459+
private fun Preview4() {
460+
AppThemeSurface {
461+
BottomSheetPreview {
462+
EditInvoiceContent(
463+
input = "123",
464+
noteText = "Note text",
465+
primaryDisplay = PrimaryDisplay.BITCOIN,
466+
displayUnit = BitcoinDisplayUnit.MODERN,
467+
onBack = {},
468+
onTextChanged = {},
469+
numericKeyboardVisible = true,
470+
onClickBalance = {},
471+
onInputChanged = {},
472+
onContinueGeneral = {},
473+
onContinueKeyboard = {},
474+
tags = listOf("Team", "Dinner", "Home"),
475+
onClickAddTag = {},
476+
onClickTag = {},
477+
isSoftKeyboardVisible = false,
478+
modifier = Modifier.sheetHeight(),
479+
)
480+
}
481+
}
482+
}

0 commit comments

Comments
 (0)