|
| 1 | +package co.yml.coreui.core.ui.ytag |
| 2 | + |
| 3 | +import android.util.Log |
| 4 | +import androidx.compose.foundation.background |
| 5 | +import androidx.compose.foundation.border |
| 6 | +import androidx.compose.foundation.clickable |
| 7 | +import androidx.compose.foundation.layout.* |
| 8 | +import androidx.compose.foundation.shape.CircleShape |
| 9 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 10 | +import androidx.compose.runtime.Composable |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.graphics.Color |
| 13 | +import androidx.compose.ui.layout.Layout |
| 14 | +import androidx.compose.ui.platform.testTag |
| 15 | +import androidx.compose.ui.tooling.preview.Preview |
| 16 | +import androidx.compose.ui.unit.Dp |
| 17 | +import androidx.compose.ui.unit.IntOffset |
| 18 | +import androidx.compose.ui.unit.dp |
| 19 | +import co.yml.coreui.core.ui.ytag.model.TagViewContainerModifiers |
| 20 | +import co.yml.coreui.core.ui.ytag.model.TagViewData |
| 21 | +import co.yml.coreui.core.ui.ytag.model.TagViewModifiers |
| 22 | + |
| 23 | +/** |
| 24 | + * [TagViewContainer] compose method used for hosting multiple chips |
| 25 | + * |
| 26 | + * @param tagViewData Defines the list of tag view data |
| 27 | + * @param tagViewContainerModifiers collection of modifier elements that decorate or add behavior to TagView elements |
| 28 | + */ |
| 29 | +@Composable |
| 30 | +fun TagViewContainer( |
| 31 | + tagViewData: List<TagViewData>, |
| 32 | + tagViewContainerModifiers: TagViewContainerModifiers |
| 33 | +) { |
| 34 | + val updatedTagViewData = tagViewData.toMutableList() |
| 35 | + val moreTagModifier = TagViewModifiers.Builder() |
| 36 | + .shape(CircleShape) |
| 37 | + .backgroundColor(Color.Black) |
| 38 | + .textColor(Color.White) |
| 39 | + .build() |
| 40 | + |
| 41 | + updatedTagViewData.add(TagViewData("More", moreTagModifier)) |
| 42 | + |
| 43 | + with(tagViewContainerModifiers) { |
| 44 | + val modifier = Modifier |
| 45 | + Box( |
| 46 | + modifier = modifier |
| 47 | + .width(width = width ?: Dp.Unspecified) |
| 48 | + .height(height = height) |
| 49 | + .defaultMinSize(minWidth = minWidth, minHeight = minHeight) |
| 50 | + .run { |
| 51 | + if (enableBorder) { |
| 52 | + border( |
| 53 | + width = borderWidth, |
| 54 | + color = borderColor, |
| 55 | + shape = shape |
| 56 | + ) |
| 57 | + } else { |
| 58 | + background(color = backgroundColor, shape = shape) |
| 59 | + } |
| 60 | + } |
| 61 | + .clickable { } |
| 62 | + .testTag("tag_view_container") |
| 63 | + .padding(containerPaddingValues) |
| 64 | + .background( |
| 65 | + color = backgroundColor, |
| 66 | + shape = shape |
| 67 | + ) |
| 68 | + ) { |
| 69 | + TagViewContainerLayout( |
| 70 | + tagViewContainerModifiers = tagViewContainerModifiers, |
| 71 | + content = { |
| 72 | + updatedTagViewData.forEach { |
| 73 | + with(it) { |
| 74 | + TagView( |
| 75 | + text = text, |
| 76 | + leadingIcon = leadingIcon, |
| 77 | + trailingIcon = trailingIcon, |
| 78 | + enabled = enabled, |
| 79 | + tagViewModifiers = tagViewModifiers |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * [TagViewContainerLayout] used for creating a custom layout to hosting y tag |
| 90 | + * @param tagViewContainerModifiers collection of modifier elements that decorate or add behavior to tag view container |
| 91 | + * @param content content of the container [Tag views] |
| 92 | + */ |
| 93 | +@Composable |
| 94 | +fun TagViewContainerLayout( |
| 95 | + tagViewContainerModifiers: TagViewContainerModifiers, |
| 96 | + content: @Composable () -> Unit |
| 97 | +) { |
| 98 | + Layout(content = content) { measurables, constraints -> |
| 99 | + val looseConstraints = constraints.copy( |
| 100 | + minWidth = 0, |
| 101 | + minHeight = 0 |
| 102 | + ) |
| 103 | + |
| 104 | + var currentRow = 0 |
| 105 | + var currentOffset = IntOffset.Zero |
| 106 | + |
| 107 | + //todo sree_ check whether the padding is correct |
| 108 | + |
| 109 | + val placeAbles = measurables.map { measurable -> |
| 110 | + val placeAble = measurable.measure(looseConstraints) |
| 111 | + |
| 112 | + //todo sree_ is horizontal and vertical space [surface] required |
| 113 | + if (currentOffset.x > 0f && currentOffset.x + placeAble.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx() |
| 114 | + .toInt() > constraints.maxWidth |
| 115 | + ) { |
| 116 | + Log.i( |
| 117 | + "check_measure", |
| 118 | + "c.XOffset: ${currentOffset.x} p.Width: ${placeAble.width} p.Height: ${placeAble.height} tagHSpace: ${ |
| 119 | + tagViewContainerModifiers.tagSpacingHorizontal.toPx().toInt() |
| 120 | + } tagVSpace: ${ |
| 121 | + tagViewContainerModifiers.tagSpacingVertical.toPx().toInt() |
| 122 | + } max w: ${constraints.maxWidth}" |
| 123 | + ) |
| 124 | + currentRow += 1 |
| 125 | + currentOffset = |
| 126 | + currentOffset.copy( |
| 127 | + x = 0, |
| 128 | + y = currentOffset.y + placeAble.height + tagViewContainerModifiers.tagSpacingVertical.toPx() |
| 129 | + .toInt() |
| 130 | + ) |
| 131 | + } |
| 132 | + Log.i("check_measure", "common: ${currentOffset}") |
| 133 | + |
| 134 | + placeAble to currentOffset.also { |
| 135 | + currentOffset = it.copy( |
| 136 | + x = it.x + placeAble.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx() |
| 137 | + .toInt() |
| 138 | + ) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + layout(width = constraints.maxWidth, |
| 143 | + height = placeAbles.lastOrNull()?.run { first.height } ?: 0) { |
| 144 | + Log.i("check_placeAble", "size: ${placeAbles.size}") |
| 145 | + placeAbles.forEachIndexed { index, placeable -> |
| 146 | + val (placeable, offset) = placeable |
| 147 | + //check whether current item has enough space |
| 148 | + val currentItemHorizontalSpace = |
| 149 | + placeable.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx() |
| 150 | + .toInt() |
| 151 | + val currentItemVerticalSpace = |
| 152 | + placeable.height + tagViewContainerModifiers.tagSpacingVertical.toPx() |
| 153 | + .toInt() |
| 154 | + |
| 155 | + if (offset.x + currentItemHorizontalSpace < constraints.maxWidth && offset.y + currentItemVerticalSpace < constraints.maxHeight) { |
| 156 | + //current item has space |
| 157 | + Log.i("check_placeable", "index: $index current item has space") |
| 158 | + val nextItemIndex = index + 1 |
| 159 | + if (nextItemIndex <= placeAbles.lastIndex) { |
| 160 | + val nextItemOffset = placeAbles[nextItemIndex].second |
| 161 | + |
| 162 | + val nextItemHorizontalSpace = |
| 163 | + placeAbles[nextItemIndex].first.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx() |
| 164 | + .toInt() |
| 165 | + val nextItemVerticalSpace = |
| 166 | + placeAbles[nextItemIndex].first.height + tagViewContainerModifiers.tagSpacingVertical.toPx() |
| 167 | + .toInt() |
| 168 | + |
| 169 | + if (nextItemOffset.x + nextItemHorizontalSpace < constraints.maxWidth && nextItemOffset.y + nextItemVerticalSpace < constraints.maxHeight) { |
| 170 | + //next item has space |
| 171 | + Log.i( |
| 172 | + "check_placeable", |
| 173 | + "next index: $nextItemIndex nextItemOffset : $nextItemOffset next item has space and placed" |
| 174 | + ) |
| 175 | + placeable.place(offset.x, offset.y) |
| 176 | + } else { |
| 177 | + //next item has no space |
| 178 | + //check space to accommodate more tag |
| 179 | + Log.i("check_placeable", "index: $index next item has no space") |
| 180 | + val moreTagPlaceAble = placeAbles.last() |
| 181 | + val moreTagHorizontalSpace = |
| 182 | + moreTagPlaceAble.first.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx() |
| 183 | + .toInt() |
| 184 | + val moreTagVerticalSpace = |
| 185 | + moreTagPlaceAble.first.height + tagViewContainerModifiers.tagSpacingVertical.toPx() |
| 186 | + .toInt() |
| 187 | + |
| 188 | + if (offset.x + moreTagHorizontalSpace < constraints.maxWidth && offset.y + moreTagVerticalSpace < constraints.maxHeight) { |
| 189 | + //place more tag |
| 190 | + Log.i("check_placeable", "index: $index more tag placed") |
| 191 | + moreTagPlaceAble.first.place(offset.x, offset.y) |
| 192 | + return@layout |
| 193 | + } else { |
| 194 | + Log.i("check_placeable", "index: $index more tag has no space") |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + } else { |
| 199 | + //current item has no space |
| 200 | + //check space to accommodate more tag |
| 201 | + Log.i("check_placeable", "index: $index current item has no space") |
| 202 | + val moreTagPlaceAble = placeAbles.last() |
| 203 | + val moreTagHorizontalSpace = |
| 204 | + moreTagPlaceAble.first.width + tagViewContainerModifiers.tagSpacingHorizontal.toPx() |
| 205 | + .toInt() |
| 206 | + val moreTagVerticalSpace = |
| 207 | + moreTagPlaceAble.first.height + tagViewContainerModifiers.tagSpacingVertical.toPx() |
| 208 | + .toInt() |
| 209 | + |
| 210 | + if (offset.x + moreTagHorizontalSpace < constraints.maxWidth && offset.y + moreTagVerticalSpace < constraints.maxHeight) { |
| 211 | + //place more tag |
| 212 | + Log.i("check_placeable", "index: $index more tag placed") |
| 213 | + placeable.place(offset.x, offset.y) |
| 214 | + return@layout |
| 215 | + } else { |
| 216 | + Log.i("check_placeable", "index: $index more tag has no space") |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +@Preview(name = "Default Tag container") |
| 225 | +@Composable |
| 226 | +fun DefaultTagContainer() { |
| 227 | + val tagViewModifiers = TagViewModifiers.Builder() |
| 228 | + .build() |
| 229 | + val tagViewData = listOf( |
| 230 | + TagViewData(text = "Tag 1", tagViewModifiers = tagViewModifiers), |
| 231 | + TagViewData(text = "Tag 2", tagViewModifiers = tagViewModifiers), |
| 232 | + TagViewData(text = "Tag 3", tagViewModifiers = tagViewModifiers), |
| 233 | + TagViewData(text = "Tag 4", tagViewModifiers = tagViewModifiers) |
| 234 | + ) |
| 235 | + |
| 236 | + val tagViewContainerModifiers = TagViewContainerModifiers.Builder() |
| 237 | + .shape(RoundedCornerShape(10.dp)) |
| 238 | + .backgroundColor(Color.Gray) |
| 239 | + .build() |
| 240 | + |
| 241 | + TagViewContainer( |
| 242 | + tagViewData = tagViewData, |
| 243 | + tagViewContainerModifiers = tagViewContainerModifiers |
| 244 | + ) |
| 245 | +} |
0 commit comments