Skip to content

Commit 7fb244a

Browse files
Merge pull request #2 from yml-org/CM-1226_YTag_Container
Tag view container Implementation
2 parents 4a0fc79 + 50b8935 commit 7fb244a

File tree

12 files changed

+1036
-149
lines changed

12 files changed

+1036
-149
lines changed
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3-
4-
<application>
5-
<activity
6-
android:name="co.yml.coreui.core.test.TestActivity"
7-
android:exported="true">
8-
<intent-filter>
9-
<action android:name="android.intent.action.MAIN" />
10-
11-
<category android:name="android.intent.category.LAUNCHER" />
12-
</intent-filter>
13-
</activity>
14-
</application>
153
</manifest>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package co.yml.coreui.ui.ytag
2+
3+
import androidx.compose.foundation.layout.PaddingValues
4+
import androidx.compose.foundation.shape.CircleShape
5+
import androidx.compose.foundation.shape.RoundedCornerShape
6+
import androidx.compose.ui.graphics.Color
7+
import androidx.compose.ui.test.*
8+
import androidx.compose.ui.test.junit4.createComposeRule
9+
import androidx.compose.ui.unit.dp
10+
import co.yml.coreui.core.ui.ytag.TagViewContainer
11+
import co.yml.coreui.core.ui.ytag.model.TagViewContainerModifiers
12+
import co.yml.coreui.core.ui.ytag.model.TagViewData
13+
import co.yml.coreui.core.ui.ytag.model.TagViewModifiers
14+
import org.junit.Rule
15+
import org.junit.Test
16+
17+
class TagViewContainerTesting {
18+
@get:Rule
19+
val composeTestRule = createComposeRule()
20+
21+
private fun launchTagViewContainer(
22+
tagViewContainerModifiers: TagViewContainerModifiers = TagViewContainerModifiers.Builder()
23+
.shape(RoundedCornerShape(10.dp))
24+
.width(200.dp)
25+
.height(120.dp)
26+
.build()
27+
) {
28+
val tagViewModifiers = TagViewModifiers.Builder()
29+
.shape(CircleShape)
30+
.backgroundColor(Color.Black)
31+
.textColor(Color.White)
32+
.build()
33+
34+
val tagViewData = listOf(
35+
TagViewData(text = "Tag 1", tagViewModifiers = tagViewModifiers),
36+
TagViewData(text = "Tag 2", tagViewModifiers = tagViewModifiers),
37+
TagViewData(text = "Tag 3", tagViewModifiers = tagViewModifiers),
38+
TagViewData(text = "Tag 4", tagViewModifiers = tagViewModifiers)
39+
)
40+
41+
composeTestRule.setContent {
42+
TagViewContainer(
43+
tagViewData = tagViewData,
44+
tagViewContainerModifiers = tagViewContainerModifiers
45+
)
46+
}
47+
}
48+
49+
@Test
50+
fun tagViewContainer_shown() {
51+
launchTagViewContainer()
52+
53+
println(
54+
"tag_view_container ${composeTestRule.onNodeWithTag("tag_view_container", useUnmergedTree = true).printToString()}"
55+
)
56+
57+
composeTestRule.onNodeWithTag("tag_view_container").assertIsDisplayed()
58+
}
59+
60+
@Test
61+
fun tagViewContainer_with_modifiers_are_executed() {
62+
val tagViewContainerModifiers = TagViewContainerModifiers.Builder()
63+
.minWidth(150.dp)
64+
.minHeight(150.dp)
65+
.width(200.dp)
66+
.height(150.dp)
67+
.enableBorder(true)
68+
.borderWidth(1.dp)
69+
.borderColor(Color.Red)
70+
.backgroundColor(Color.Gray)
71+
.shape(CircleShape)
72+
.containerPaddingValues(PaddingValues(8.dp))
73+
.tagSpacingHorizontal(8.dp)
74+
.tagSpacingVertical(8.dp)
75+
.moreTagConfiguration(TagViewData(text = "more"))
76+
.build()
77+
78+
launchTagViewContainer(tagViewContainerModifiers)
79+
80+
composeTestRule.onNodeWithTag("tag_view_container")
81+
.assertIsDisplayed()
82+
}
83+
84+
@Test
85+
fun tagViewContainer_tags_shown(){
86+
launchTagViewContainer()
87+
88+
composeTestRule.onNodeWithText("Tag 1").assertIsDisplayed()
89+
}
90+
91+
@Test
92+
fun tagViewContainer_with_less_space_more_tag_shown(){
93+
val tagViewContainerModifiers = TagViewContainerModifiers.Builder()
94+
.width(150.dp)
95+
.height(50.dp)
96+
.build()
97+
98+
launchTagViewContainer(tagViewContainerModifiers)
99+
100+
composeTestRule.onNodeWithText("more").assertIsDisplayed()
101+
}
102+
}

core/ui/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<application>
44
<activity android:name="androidx.activity.ComponentActivity" />
55
</application>
6-
</manifest>
6+
</manifest>

core/ui/src/main/java/co/yml/coreui/core/ui/ytag/TagView.kt

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import androidx.compose.foundation.clickable
77
import androidx.compose.foundation.layout.*
88
import androidx.compose.foundation.shape.CircleShape
99
import androidx.compose.foundation.shape.RoundedCornerShape
10-
import androidx.compose.material3.*
10+
import androidx.compose.material3.Icon
11+
import androidx.compose.material3.IconButton
12+
import androidx.compose.material3.Surface
13+
import androidx.compose.material3.Text
1114
import androidx.compose.runtime.Composable
12-
import androidx.compose.ui.Alignment
1315
import androidx.compose.ui.Modifier
1416
import androidx.compose.ui.graphics.Color
1517
import androidx.compose.ui.graphics.RectangleShape
@@ -21,6 +23,8 @@ import androidx.compose.ui.tooling.preview.Preview
2123
import androidx.compose.ui.unit.Dp
2224
import androidx.compose.ui.unit.dp
2325
import androidx.compose.ui.unit.sp
26+
import androidx.constraintlayout.compose.ConstraintLayout
27+
import androidx.constraintlayout.compose.Dimension
2428
import co.yml.coreui.core.ui.ytag.model.TagViewModifiers
2529

2630
/**
@@ -38,7 +42,9 @@ fun TagView(
3842
leadingIcon: @Composable ((enable: Boolean) -> Unit)? = null,
3943
trailingIcon: @Composable ((enable: Boolean) -> Unit)? = null,
4044
enabled: Boolean = true,
41-
tagViewModifiers: TagViewModifiers = TagViewModifiers.Builder().build()
45+
tagViewModifiers: TagViewModifiers = TagViewModifiers.Builder().build(),
46+
overFlowText: String = "",
47+
onClick: () -> Unit = {}
4248
) {
4349
with(tagViewModifiers) {
4450
Surface(
@@ -50,8 +56,10 @@ fun TagView(
5056
.width(width = width ?: Dp.Unspecified)
5157
.height(height = height)
5258
) {
53-
Row(
59+
ConstraintLayout(
5460
modifier = Modifier
61+
.width(width = width ?: Dp.Unspecified)
62+
.height(height)
5563
.run {
5664
if (enableBorder) {
5765
border(
@@ -66,33 +74,48 @@ fun TagView(
6674
.clickable {
6775
if (enabled) {
6876
onClick.invoke()
77+
tagViewModifiers.onClick.invoke()
6978
}
7079
}
7180
.defaultMinSize(minWidth = minWidth, minHeight = minHeight)
7281
.padding(containerPaddingValues)
7382
.background(
7483
color = backgroundColor,
7584
shape = shape
76-
),
77-
verticalAlignment = Alignment.CenterVertically
85+
)
7886
) {
79-
leadingIcon?.invoke(enabled)
87+
val (leading_icon, text_view, trailing_icon) = createRefs()
88+
89+
Box(modifier = Modifier.constrainAs(leading_icon) {
90+
start.linkTo(parent.start)
91+
top.linkTo(parent.top)
92+
bottom.linkTo(parent.bottom)
93+
}
94+
) {
95+
leadingIcon?.invoke(enabled)
96+
}
8097

8198
Text(
82-
text = text,
99+
text = overFlowText.ifEmpty { text },
83100
color = textColor,
84101
fontSize = fontSize,
85102
fontWeight = fontWeight,
86103
fontFamily = fontFamily,
87104
fontStyle = fontStyle,
88105
letterSpacing = letterSpacing,
89106
modifier = Modifier
107+
.constrainAs(text_view) {
108+
start.linkTo(leading_icon.end)
109+
end.linkTo(trailing_icon.start)
110+
top.linkTo(parent.top)
111+
bottom.linkTo(parent.bottom)
112+
width = Dimension.fillToConstraints
113+
}
90114
.padding(
91115
textPadding
92116
)
93-
.align(Alignment.CenterVertically)
94117
.semantics {
95-
this.contentDescription = text
118+
this.contentDescription = semantics
96119
},
97120
style = style,
98121
textDecoration = textDecoration,
@@ -103,7 +126,15 @@ fun TagView(
103126
maxLines = maxLines,
104127
onTextLayout = onTextLayout
105128
)
106-
trailingIcon?.invoke(enabled)
129+
Box(modifier = Modifier.constrainAs(trailing_icon) {
130+
end.linkTo(parent.end)
131+
top.linkTo(parent.top)
132+
bottom.linkTo(parent.bottom)
133+
}
134+
) {
135+
trailingIcon?.invoke(enabled)
136+
}
137+
107138
}
108139
}
109140
}

0 commit comments

Comments
 (0)