Skip to content

Commit 262f94e

Browse files
authored
Merge pull request #67 from synonymdev/feat/keyboard-set-amount
Create Keyboard Component
2 parents 0e914e7 + 866a679 commit 262f94e

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
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+
}

0 commit comments

Comments
 (0)