Skip to content

Commit 920b222

Browse files
committed
Merge branch 'master' into feat/external-node
2 parents c14acad + b37ef78 commit 920b222

File tree

4 files changed

+215
-10
lines changed

4 files changed

+215
-10
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package to.bitkit.ui.components
2+
3+
import androidx.compose.ui.test.*
4+
import androidx.compose.ui.test.junit4.createComposeRule
5+
import dagger.hilt.android.testing.HiltAndroidRule
6+
import dagger.hilt.android.testing.HiltAndroidTest
7+
import org.junit.Before
8+
import org.junit.Rule
9+
import org.junit.Test
10+
11+
@HiltAndroidTest
12+
class KeyboardTest {
13+
14+
@get:Rule
15+
val composeTestRule = createComposeRule()
16+
17+
@get:Rule
18+
val hiltRule = HiltAndroidRule(this)
19+
20+
@Before
21+
fun setup() {
22+
hiltRule.inject()
23+
}
24+
25+
@Test
26+
fun keyboard_displaysAllButtons() {
27+
composeTestRule.setContent {
28+
Keyboard(onClick = {})
29+
}
30+
31+
composeTestRule.onNodeWithTag("KeyboardButton_1").assertIsDisplayed()
32+
composeTestRule.onNodeWithTag("KeyboardButton_2").assertIsDisplayed()
33+
composeTestRule.onNodeWithTag("KeyboardButton_3").assertIsDisplayed()
34+
composeTestRule.onNodeWithTag("KeyboardButton_4").assertIsDisplayed()
35+
composeTestRule.onNodeWithTag("KeyboardButton_5").assertIsDisplayed()
36+
composeTestRule.onNodeWithTag("KeyboardButton_6").assertIsDisplayed()
37+
composeTestRule.onNodeWithTag("KeyboardButton_7").assertIsDisplayed()
38+
composeTestRule.onNodeWithTag("KeyboardButton_8").assertIsDisplayed()
39+
composeTestRule.onNodeWithTag("KeyboardButton_9").assertIsDisplayed()
40+
composeTestRule.onNodeWithTag("KeyboardButton_.").assertIsDisplayed()
41+
composeTestRule.onNodeWithTag("KeyboardButton_0").assertIsDisplayed()
42+
composeTestRule.onNodeWithTag("KeyboardButton_").assertIsDisplayed()
43+
}
44+
45+
@Test
46+
fun keyboard_tripleZero_when_not_decimal() {
47+
composeTestRule.setContent {
48+
Keyboard(onClick = {}, isDecimal = false)
49+
}
50+
composeTestRule.onNodeWithTag("KeyboardButton_000").assertIsDisplayed()
51+
}
52+
53+
@Test
54+
fun keyboard_decimal_when_decimal() {
55+
composeTestRule.setContent {
56+
Keyboard(onClick = {}, isDecimal = true)
57+
}
58+
composeTestRule.onNodeWithTag("KeyboardButton_.").assertIsDisplayed()
59+
}
60+
61+
@Test
62+
fun keyboard_button_click_triggers_callback() {
63+
var clickedValue = ""
64+
composeTestRule.setContent {
65+
Keyboard(onClick = { clickedValue = it })
66+
}
67+
68+
composeTestRule.onNodeWithTag("KeyboardButton_5").performClick()
69+
assert(clickedValue == "5")
70+
71+
composeTestRule.onNodeWithTag("KeyboardButton_.").performClick()
72+
assert(clickedValue == ".")
73+
74+
composeTestRule.onNodeWithTag("KeyboardButton_0").performClick()
75+
assert(clickedValue == "0")
76+
77+
}
78+
79+
@Test
80+
fun keyboard_button_click_tripleZero() {
81+
var clickedValue = ""
82+
composeTestRule.setContent {
83+
Keyboard(onClick = { clickedValue = it }, isDecimal = false)
84+
}
85+
86+
composeTestRule.onNodeWithTag("KeyboardButton_000").performClick()
87+
assert(clickedValue == "000")
88+
}
89+
90+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package to.bitkit.ui.components
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.Arrangement
5+
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.foundation.layout.fillMaxWidth
8+
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.foundation.lazy.grid.GridCells
10+
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
11+
import androidx.compose.material3.Text
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.ui.Modifier
14+
import androidx.compose.ui.platform.testTag
15+
import androidx.compose.ui.text.TextStyle
16+
import androidx.compose.ui.text.font.FontWeight
17+
import androidx.compose.ui.text.style.TextAlign
18+
import androidx.compose.ui.tooling.preview.Devices
19+
import androidx.compose.ui.tooling.preview.Preview
20+
import androidx.compose.ui.unit.dp
21+
import androidx.compose.ui.unit.sp
22+
import to.bitkit.ui.theme.AppThemeSurface
23+
import to.bitkit.ui.theme.Colors
24+
import to.bitkit.ui.theme.InterFontFamily
25+
26+
@Composable
27+
fun Keyboard(
28+
onClick: (String) -> Unit,
29+
isDecimal: Boolean = true,
30+
modifier: Modifier = Modifier
31+
) {
32+
LazyVerticalGrid(
33+
verticalArrangement = Arrangement.spacedBy(34.dp),
34+
columns = GridCells.Fixed(3),
35+
modifier = modifier
36+
) {
37+
item { KeyboardButton(text = "1", onClick = onClick) }
38+
item { KeyboardButton(text = "2", onClick = onClick) }
39+
item { KeyboardButton(text = "3", onClick = onClick) }
40+
item { KeyboardButton(text = "4", onClick = onClick) }
41+
item { KeyboardButton(text = "5", onClick = onClick) }
42+
item { KeyboardButton(text = "6", onClick = onClick) }
43+
item { KeyboardButton(text = "7", onClick = onClick) }
44+
item { KeyboardButton(text = "8", onClick = onClick) }
45+
item { KeyboardButton(text = "9", onClick = onClick) }
46+
item { KeyboardButton(text = if (isDecimal) "." else "000", onClick = onClick) }
47+
item { KeyboardButton(text = "0", onClick = onClick) }
48+
item { KeyboardButton(text = "", onClick = onClick) }
49+
}
50+
}
51+
52+
@Composable
53+
private fun KeyboardButton(
54+
text: String,
55+
onClick: (String) -> Unit
56+
) {
57+
Text(
58+
text = text,
59+
style = TextStyle(
60+
fontFamily = InterFontFamily,
61+
fontWeight = FontWeight.Normal,
62+
fontSize = 24.sp,
63+
lineHeight = 44.sp,
64+
letterSpacing = (-0.1).sp,
65+
textAlign = TextAlign.Center,
66+
color = Colors.White,
67+
),
68+
modifier = Modifier.clickable(
69+
onClick = {
70+
onClick(text)
71+
},
72+
onClickLabel = text
73+
).testTag("KeyboardButton_$text"),
74+
)
75+
}
76+
77+
@Preview(showBackground = true)
78+
@Composable
79+
private fun Preview() {
80+
AppThemeSurface {
81+
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
82+
Keyboard(modifier = Modifier.fillMaxWidth().padding(41.dp), onClick = {})
83+
}
84+
}
85+
}
86+
87+
@Preview(showBackground = true)
88+
@Composable
89+
private fun Preview2() {
90+
AppThemeSurface {
91+
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
92+
Keyboard(isDecimal = false, modifier = Modifier.fillMaxWidth().padding(41.dp), onClick = {})
93+
}
94+
}
95+
}
96+
97+
@Preview(showBackground = true, device = Devices.PIXEL_TABLET)
98+
@Composable
99+
private fun Preview3() {
100+
AppThemeSurface {
101+
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
102+
Keyboard(isDecimal = false, modifier = Modifier.fillMaxWidth().padding(41.dp), onClick = {})
103+
}
104+
}
105+
}

app/src/main/java/to/bitkit/ui/screens/wallets/send/SendAmountScreen.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.ui.text.input.KeyboardType
2424
import androidx.compose.ui.unit.dp
2525
import to.bitkit.R
2626
import to.bitkit.ui.LocalBalances
27+
import to.bitkit.ui.components.Keyboard
2728
import to.bitkit.ui.components.OutlinedColorButton
2829
import to.bitkit.ui.components.PrimaryButton
2930
import to.bitkit.ui.components.Text13Up
@@ -56,14 +57,14 @@ fun SendAmountScreen(
5657
val focusRequester = remember { FocusRequester() }
5758
LaunchedEffect(Unit) { focusRequester.requestFocus() }
5859

59-
TextField(
60+
TextField( //TODO UPDATE IN OTHER PR
6061
placeholder = { Text(stringResource(R.string.amount_placeholder)) },
62+
readOnly = true,
6163
value = uiState.amountInput,
62-
onValueChange = { onEvent(SendEvent.AmountChange(it)) },
64+
onValueChange = { },
6365
colors = AppTextFieldDefaults.noIndicatorColors,
6466
shape = MaterialTheme.shapes.small,
6567
singleLine = true,
66-
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
6768
modifier = Modifier
6869
.fillMaxWidth()
6970
.focusRequester(focusRequester)
@@ -104,15 +105,25 @@ fun SendAmountScreen(
104105
)
105106
}
106107
}
107-
HorizontalDivider()
108108

109109
Spacer(modifier = Modifier.weight(1f))
110110

111+
HorizontalDivider(modifier = Modifier.padding(vertical = 32.dp))
112+
113+
Keyboard(
114+
onClick = { number -> onEvent(SendEvent.AmountChange(number)) },
115+
isDecimal = false, //TODO UPDATE IN OTHER PR
116+
modifier = Modifier.fillMaxWidth(),
117+
)
118+
119+
Spacer(modifier = Modifier.height(41.dp))
120+
111121
PrimaryButton(
112122
text = stringResource(R.string.continue_button),
113123
enabled = uiState.isAmountInputValid,
114124
onClick = { onEvent(SendEvent.AmountContinue(uiState.amountInput)) },
115125
)
126+
116127
Spacer(modifier = Modifier.height(16.dp))
117128
}
118129
}

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,12 @@ class AppViewModel @Inject constructor(
269269
}
270270

271271
private fun onAmountChange(value: String) {
272-
val isAmountValid = validateAmount(value)
273-
_sendUiState.update {
274-
it.copy(
275-
amountInput = value,
276-
isAmountInputValid = isAmountValid,
277-
)
272+
val newInput = if (_sendUiState.value.amountInput == "0") value else _sendUiState.value.amountInput + value
273+
val isAmountValid = validateAmount(newInput)
274+
if (isAmountValid) {
275+
_sendUiState.update { it.copy( amountInput = newInput,) }
278276
}
277+
_sendUiState.update { it.copy(isAmountInputValid = isAmountValid,) }
279278
}
280279

281280
private fun onPaymentMethodSwitch() {

0 commit comments

Comments
 (0)