Skip to content

Commit aa1ac76

Browse files
1. Added semantics property in TagViewModifiers.kt and TagViewContainerModifiers.kt.
2. Code refactoring
1 parent 713e7f2 commit aa1ac76

File tree

6 files changed

+43
-53
lines changed

6 files changed

+43
-53
lines changed

core/ui/src/androidTest/java/co/yml/coreui/ui/ytag/TagViewContainerTesting.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class TagViewContainerTesting {
8585
fun tagViewContainer_tags_shown(){
8686
launchTagViewContainer()
8787

88-
composeTestRule.onNodeWithContentDescription("Tag 1").assertIsDisplayed()
88+
composeTestRule.onNodeWithText("Tag 1").assertIsDisplayed()
8989
}
9090

9191
@Test
@@ -97,6 +97,6 @@ class TagViewContainerTesting {
9797

9898
launchTagViewContainer(tagViewContainerModifiers)
9999

100-
composeTestRule.onNodeWithContentDescription("more").assertIsDisplayed()
100+
composeTestRule.onNodeWithText("more").assertIsDisplayed()
101101
}
102102
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fun TagView(
9696
)
9797
.align(Alignment.CenterVertically)
9898
.semantics {
99-
this.contentDescription = text
99+
this.contentDescription = semantics
100100
},
101101
style = style,
102102
textDecoration = textDecoration,

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

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package co.yml.coreui.core.ui.ytag
22

3-
import android.util.Log
43
import androidx.compose.foundation.background
54
import androidx.compose.foundation.border
65
import androidx.compose.foundation.clickable
@@ -34,7 +33,7 @@ fun TagViewContainer(
3433
tagViewData: List<TagViewData>,
3534
tagViewContainerModifiers: TagViewContainerModifiers
3635
) {
37-
//add more tag into the list
36+
//add overflow details tag into the list
3837
val updatedTagViewData = tagViewData.toMutableList()
3938
val moreTag = tagViewContainerModifiers.moreTagConfiguration
4039
updatedTagViewData.add(moreTag)
@@ -69,7 +68,7 @@ fun TagViewContainer(
6968
.clickable { }
7069
.semantics {
7170
this.contentDescription =
72-
context.getString(co.yml.coreui.ui.R.string.tag_view_container_accessibility_title)
71+
tagViewContainerModifiers.semantics.ifEmpty { context.getString(co.yml.coreui.ui.R.string.tag_view_container_accessibility_title) }
7372
}
7473
.testTag("tag_view_container")
7574
.background(
@@ -127,20 +126,14 @@ fun TagViewContainerLayout(
127126
var currentRow = 0
128127
var currentOffset = IntOffset.Zero
129128

129+
//Measurement phase
130130
val placeAbles = measurables.map { measurable ->
131131
val placeAble: Placeable = measurable.measure(looseConstraints)
132132

133+
//calculate the offsets to place the tags in layout phase
133134
if (currentOffset.x > 0f && currentOffset.x + placeAble.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx()
134135
.toInt() > constraints.maxWidth
135136
) {
136-
Log.i(
137-
"check_measure",
138-
"c.XOffset: ${currentOffset.x} p.Width: ${placeAble.width} p.Height: ${placeAble.height} tagHSpace: ${
139-
tagViewContainerModifiers.tagSpacingHorizontal.toPx().toInt()
140-
} tagVSpace: ${
141-
tagViewContainerModifiers.tagSpacingVertical.toPx().toInt()
142-
} max w: ${constraints.maxWidth}"
143-
)
144137
currentRow += 1
145138
currentOffset =
146139
currentOffset.copy(
@@ -161,30 +154,21 @@ fun TagViewContainerLayout(
161154
width = constraints.maxWidth,
162155
height = constraints.maxHeight
163156
) {
164-
Log.i("check_placeAble", "size: ${placeAbles.size}")
165-
placeAbles.forEachIndexed { index, placeable ->
166-
val (placeable, offset) = placeable
167-
//check whether current item has enough space
157+
placeAbles.forEachIndexed { index, tagPlaceable ->
158+
val (placeable, offset) = tagPlaceable
159+
//check whether container has enough space to place the current tag
168160
if (offset.x + placeable.width < constraints.maxWidth && offset.y + placeable.height < constraints.maxHeight) {
169-
//current item has space
170-
Log.i("check_placeable", "index: $index current item has space")
161+
//space available for current tag
171162
val nextItemIndex = index + 1
163+
//check whether container has enough space to place the next tag
172164
if (nextItemIndex <= placeAbles.lastIndex) {
173165
val nextItemOffset = placeAbles[nextItemIndex].second
174-
Log.i(
175-
"check_placeable",
176-
"next index: $nextItemIndex nextItemOffset : $nextItemOffset Before checking"
177-
)
178166
if (nextItemOffset.x + placeAbles[nextItemIndex].first.width < constraints.maxWidth && nextItemOffset.y + placeAbles[nextItemIndex].first.height < constraints.maxHeight) {
179-
//next item has space
180-
Log.i(
181-
"check_placeable",
182-
"next index: $nextItemIndex nextItemOffset : $nextItemOffset next item has space and placed"
183-
)
167+
//space available for next tag
184168
placeable.place(offset.x, offset.y)
185169
} else {
186-
//next item has no space
187-
//check space to accommodate more tag
170+
//space not available for next tag
171+
//place the over flow tag
188172
val overflow = showOverFlow(
189173
index,
190174
placeAbles,
@@ -199,8 +183,8 @@ fun TagViewContainerLayout(
199183
}
200184
}
201185
} else {
202-
//current item has no space
203-
//check space to accommodate more tag
186+
//space available for current tag
187+
//place the over flow tag
204188
val overflow = showOverFlow(
205189
index,
206190
placeAbles,
@@ -237,18 +221,12 @@ fun showOverFlow(
237221
val placeable = placeAbles[index]
238222

239223
if (tagViewContainerModifiers.moreTagConfiguration.showOverFlow) {
240-
Log.i("check_placeable", "index: $index next item has no space")
241224
val moreTagPlaceAble = placeAbles.last()
242225
val remainingTags = placeAbles.lastIndex - 1 - index
243226

244227
if (offset.x + moreTagPlaceAble.first.width < constraints.maxWidth && offset.y + moreTagPlaceAble.first.height < constraints.maxHeight) {
245228
//place more tag
246-
Log.i(
247-
"check_placeable",
248-
"index: $index more tag placed offset: $offset"
249-
)
250-
251-
//check whether more tag can be placed in between current and previous offset
229+
//check whether space available for over flow tag to place in between current [which replace over flow tag] and previous tags
252230
val previousIndex = index - 1
253231
if (previousIndex >= 0) {
254232
val previousOffset = placeAbles[previousIndex].second
@@ -260,18 +238,11 @@ fun showOverFlow(
260238
val moreTagYOffset = previousOffset.y
261239

262240
if (moreTagXOffset + moreTagPlaceAble.first.width < constraints.maxWidth && moreTagYOffset + moreTagPlaceAble.first.height < constraints.maxHeight) {
263-
Log.i(
264-
"check_placeable",
265-
" prev index: $previousIndex more tag placed on in btw offset: $previousOffset"
266-
)
267-
268241
return Pair(moreTagPlaceAble.first, IntOffset(moreTagXOffset, moreTagYOffset))
269242
}
270243
}
271244

272245
return moreTagPlaceAble
273-
} else {
274-
Log.i("check_placeable", "index: $index more tag has no space")
275246
}
276247
} else {
277248
return placeable

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ data class TagViewContainerModifiers(
3939
val tagSpacingHorizontal: Dp,
4040
val tagSpacingVertical: Dp,
4141
val moreTagConfiguration: TagViewData,
42-
val onClick: (TagViewData) -> Unit
42+
val onClick: (TagViewData) -> Unit,
43+
val semantics: String
4344
) {
4445
//todo sree_ check min and max default size
4546
class Builder {
@@ -65,6 +66,7 @@ data class TagViewContainerModifiers(
6566
.build()
6667
)
6768
private var onClick: (TagViewData) -> Unit = {}
69+
private var semantics: String = ""
6870

6971
fun minWidth(minWidth: Dp) = apply { this.minWidth = minWidth }
7072

@@ -97,6 +99,8 @@ data class TagViewContainerModifiers(
9799

98100
fun onCLick(onClick: (TagViewData) -> Unit) = apply { this.onClick = onClick }
99101

102+
fun semantics(semantics: String) = apply { this.semantics = semantics }
103+
100104
fun build() = TagViewContainerModifiers(
101105
minWidth,
102106
minHeight,
@@ -111,7 +115,8 @@ data class TagViewContainerModifiers(
111115
tagSpacingHorizontal,
112116
tagSpacingVertical,
113117
moreTagConfiguration,
114-
onClick
118+
onClick,
119+
semantics
115120
)
116121
}
117122
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ data class TagViewModifiers(
7979
val tonalElevation: Dp,
8080
val shadowElevation: Dp,
8181
val containerPaddingValues: PaddingValues,
82-
val onClick: () -> Unit
82+
val onClick: () -> Unit,
83+
val semantics: String
8384
) {
8485
class Builder {
8586
private var minWidth: Dp = 52.dp
@@ -111,6 +112,7 @@ data class TagViewModifiers(
111112
private var shadowElevation: Dp = 0.dp
112113
private var containerPaddingValues: PaddingValues = PaddingValues(horizontal = 4.dp)
113114
private var onClick: () -> Unit = {}
115+
private var semantics: String = text
114116

115117
fun minWidth(minWidth: Dp) = apply { this.minWidth = minWidth }
116118

@@ -166,6 +168,8 @@ data class TagViewModifiers(
166168
apply { this.containerPaddingValues = paddingValues }
167169

168170
fun onCLick(onClick: () -> Unit) = apply { this.onClick = onClick }
171+
172+
fun semantics(semantics: String) = apply { this.semantics = semantics }
169173
fun build() = TagViewModifiers(
170174
minWidth,
171175
minHeight,
@@ -194,7 +198,8 @@ data class TagViewModifiers(
194198
tonalElevation,
195199
shadowElevation,
196200
containerPaddingValues,
197-
onClick
201+
onClick,
202+
semantics
198203
)
199204
}
200205
}

feature/ytag/src/main/java/co/yml/coreui/feature/ytag/ui/YTagActivity.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,17 @@ fun DefaultTagViewContainer() {
306306

307307
val tagViewData = listOf(
308308
TagViewData("Tag view 1", tagViewModifiers),
309-
TagViewData("Tag view 2", tagViewModifiers),
310-
TagViewData("Tag view 3", tagViewModifiers),
309+
TagViewData("Tag 2", TagViewModifiers.Builder()
310+
.shape(CircleShape)
311+
.backgroundColor(Color.Black)
312+
.textColor(Color.White)
313+
.style(textStyle)
314+
.semantics("second tag")
315+
.onCLick {
316+
Log.i("check_click", "tag view clicked")
317+
}
318+
.build()),
319+
TagViewData("Tag 3", tagViewModifiers),
311320
TagViewData("Tag view 4", tagViewModifiers),
312321
TagViewData("Tag view 5", tagViewModifiers),
313322
TagViewData("Tag view 6", tagViewModifiers),

0 commit comments

Comments
 (0)