Skip to content

Commit 24e0530

Browse files
author
burak.ozturk1
committed
Improved horizontal display of quantitypicker
1 parent 17d102d commit 24e0530

File tree

4 files changed

+131
-34
lines changed

4 files changed

+131
-34
lines changed

libraries/quantity-picker-compose/src/main/java/com/trendyol/uicomponents/quantitypicker/QuantityAddIcon.kt

+15-13
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package com.trendyol.uicomponents.quantitypicker
22

33
import androidx.compose.animation.Animatable
44
import androidx.compose.animation.core.tween
5-
import androidx.compose.foundation.background
65
import androidx.compose.foundation.clickable
76
import androidx.compose.foundation.interaction.MutableInteractionSource
7+
import androidx.compose.foundation.layout.Box
88
import androidx.compose.foundation.layout.padding
9-
import androidx.compose.foundation.shape.CircleShape
109
import androidx.compose.material.Icon
1110
import androidx.compose.runtime.Composable
1211
import androidx.compose.runtime.LaunchedEffect
@@ -16,7 +15,7 @@ import androidx.compose.runtime.remember
1615
import androidx.compose.runtime.rememberCoroutineScope
1716
import androidx.compose.runtime.setValue
1817
import androidx.compose.ui.Modifier
19-
import androidx.compose.ui.draw.clip
18+
import androidx.compose.ui.draw.drawBehind
2019
import androidx.compose.ui.graphics.Color
2120
import androidx.compose.ui.res.painterResource
2221
import androidx.compose.ui.unit.dp
@@ -64,15 +63,8 @@ internal fun QuantityAddIcon(
6463
setTargetBackgroundColor.invoke(quantityData.getBackgroundColor(icons))
6564
}
6665
}
67-
68-
Icon(
69-
painter = painterResource(id = icons.addIconResId),
70-
tint = iconTintColor,
71-
contentDescription = null,
66+
Box(
7267
modifier = Modifier
73-
.clip(CircleShape)
74-
.background(animatedBackgroundColor.value)
75-
.padding(8.dp)
7668
.clickable(
7769
indication = null,
7870
interactionSource = MutableInteractionSource(),
@@ -84,6 +76,16 @@ internal fun QuantityAddIcon(
8476
}
8577
onAddClick?.invoke()
8678
}
87-
)
88-
)
79+
)) {
80+
Icon(
81+
painter = painterResource(id = icons.addIconResId),
82+
tint = iconTintColor,
83+
contentDescription = null,
84+
modifier = Modifier
85+
.drawBehind {
86+
drawCircle(color = animatedBackgroundColor.value)
87+
}
88+
.padding(8.dp)
89+
)
90+
}
8991
}

libraries/quantity-picker-compose/src/main/java/com/trendyol/uicomponents/quantitypicker/QuantityPicker.kt

+19-19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.trendyol.uicomponents.quantitypicker
33
import androidx.compose.animation.AnimatedVisibility
44
import androidx.compose.foundation.background
55
import androidx.compose.foundation.border
6+
import androidx.compose.foundation.layout.Arrangement
67
import androidx.compose.foundation.layout.Column
78
import androidx.compose.foundation.layout.Row
89
import androidx.compose.foundation.layout.padding
@@ -139,28 +140,27 @@ internal fun HorizontalQuantityPicker(
139140
color = quantityShape.backgroundColor,
140141
shape = quantityShape.shape
141142
),
142-
verticalAlignment = Alignment.CenterVertically
143+
verticalAlignment = Alignment.CenterVertically,
144+
horizontalArrangement = Arrangement.SpaceEvenly
143145
) {
144146

145147
AnimatedVisibility(visible = quantityData.currentQuantity > 0 || showLoading) {
146-
Row(
147-
modifier = Modifier.padding(top = 2.dp),
148-
verticalAlignment = Alignment.CenterVertically
149-
) {
150-
QuantitySubtractIcon(
151-
icons = icons,
152-
quantityData = quantityData,
153-
onSubtractClick = onSubtractClick,
154-
currentQuantity = quantityData.currentQuantity
155-
)
156-
QuantityText(
157-
quantityData = quantityData,
158-
shape = quantityTextShape,
159-
style = textStyle,
160-
showLoading = showLoading,
161-
progressColor = progressColor,
162-
)
163-
}
148+
QuantitySubtractIcon(
149+
icons = icons,
150+
quantityData = quantityData,
151+
onSubtractClick = onSubtractClick,
152+
currentQuantity = quantityData.currentQuantity
153+
)
154+
}
155+
156+
AnimatedVisibility(visible = quantityData.currentQuantity > 0 || showLoading) {
157+
QuantityText(
158+
quantityData = quantityData,
159+
shape = quantityTextShape,
160+
style = textStyle,
161+
showLoading = showLoading,
162+
progressColor = progressColor,
163+
)
164164
}
165165
QuantityAddIcon(
166166
icons = icons,

sample-compose/src/main/java/com/trendyol/uicomponents/samplecompose/QuantityPickerScreen.kt

+93-1
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,27 @@ import androidx.compose.foundation.layout.Arrangement
44
import androidx.compose.foundation.layout.Column
55
import androidx.compose.foundation.layout.Row
66
import androidx.compose.foundation.layout.fillMaxWidth
7+
8+
import androidx.compose.foundation.layout.height
79
import androidx.compose.foundation.layout.padding
10+
import androidx.compose.foundation.layout.width
811
import androidx.compose.foundation.lazy.LazyRow
912
import androidx.compose.foundation.lazy.items
13+
import androidx.compose.foundation.rememberScrollState
14+
import androidx.compose.foundation.verticalScroll
15+
import androidx.compose.material.Card
16+
import androidx.compose.material.Divider
1017
import androidx.compose.material.Text
1118
import androidx.compose.runtime.Composable
1219
import androidx.compose.runtime.mutableStateListOf
1320
import androidx.compose.runtime.mutableStateOf
1421
import androidx.compose.runtime.remember
1522
import androidx.compose.runtime.rememberCoroutineScope
23+
import androidx.compose.ui.Alignment
1624
import androidx.compose.ui.Modifier
1725
import androidx.compose.ui.unit.dp
1826
import com.trendyol.uicomponents.quantitypicker.QuantityIcons
27+
import com.trendyol.uicomponents.quantitypicker.QuantityPicker
1928
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDefaults
2029
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDirection
2130
import com.trendyol.uicomponents.quantitypicker.QuantityPickerViewData
@@ -29,9 +38,13 @@ import kotlinx.coroutines.launch
2938
@Composable
3039
fun QuantityPickerScreen() {
3140
UIComponentsTheme {
32-
Column {
41+
Column(
42+
modifier = Modifier.verticalScroll(rememberScrollState())
43+
) {
3344
ProductSliderSection()
3445
SingleProductSection()
46+
ProductWeightOptionsSection()
47+
ProductDetailSection()
3548
}
3649
}
3750
}
@@ -167,3 +180,82 @@ private fun ProductSliderSection() {
167180
}
168181
}
169182
}
183+
184+
@Composable
185+
private fun ProductDetailSection() {
186+
Card(
187+
modifier = Modifier.padding(16.dp)
188+
) {
189+
Column(
190+
modifier = Modifier.padding(12.dp),
191+
verticalArrangement = Arrangement.spacedBy(8.dp),
192+
horizontalAlignment = Alignment.CenterHorizontally
193+
) {
194+
Text(text = "Product Detail")
195+
Divider()
196+
QuantityPicker(
197+
modifier = Modifier
198+
.height(42.dp)
199+
.width(150.dp),
200+
quantityData = QuantityPickerViewData(currentQuantity = 2),
201+
direction = QuantityPickerDirection.HORIZONTAL
202+
)
203+
}
204+
}
205+
}
206+
207+
@Composable
208+
private fun ProductWeightOptionsSection() {
209+
val coroutineScope = rememberCoroutineScope()
210+
val products = remember {
211+
mutableStateListOf(
212+
ProductCardData("0", "500 gr"),
213+
ProductCardData("1", "1 kg"),
214+
ProductCardData("2", "2 kg")
215+
)
216+
}
217+
218+
val onProductClick: (ProductCardData, Int) -> Unit = remember(products) {
219+
{ product: ProductCardData, quantity: Int ->
220+
val newQuantity = product.quantityData.currentQuantity + quantity
221+
coroutineScope.launch {
222+
val index = products.indexOf(product)
223+
products[index] = product.copy(isLoading = true)
224+
coroutineScope.launch {
225+
delay(500)
226+
products[index] = product.copy(
227+
isLoading = false,
228+
quantityData = product.quantityData.copy(
229+
currentQuantity = newQuantity
230+
)
231+
)
232+
}
233+
}
234+
}
235+
}
236+
237+
Card(modifier = Modifier.padding(16.dp)) {
238+
Column(
239+
verticalArrangement = Arrangement.spacedBy(12.dp)
240+
) {
241+
products.forEach { product ->
242+
Row(
243+
modifier = Modifier
244+
.fillMaxWidth()
245+
.padding(8.dp),
246+
horizontalArrangement = Arrangement.Absolute.SpaceBetween
247+
) {
248+
Text(text = product.title)
249+
QuantityPicker(
250+
icons = QuantityIcons.default.copy(addIconBackgroundColor = QuantityPickerDefaults.defaultColor),
251+
showLoading = product.isLoading,
252+
quantityData = product.quantityData,
253+
direction = QuantityPickerDirection.HORIZONTAL,
254+
onAddClick = { onProductClick(product, 1) },
255+
onSubtractClick = { onProductClick(product, -1) }
256+
)
257+
}
258+
}
259+
}
260+
}
261+
}

sample-compose/src/main/java/com/trendyol/uicomponents/samplecompose/ui/productcard/ProductCard.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.ui.res.painterResource
1515
import androidx.compose.ui.unit.dp
1616
import com.trendyol.uicomponents.quantitypicker.QuantityIcons
1717
import com.trendyol.uicomponents.quantitypicker.QuantityPicker
18+
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDefaults
1819
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDirection
1920
import com.trendyol.uicomponents.sample.compose.R
2021
import com.trendyol.uicomponents.samplecompose.ui.theme.Typography
@@ -54,8 +55,10 @@ fun ProductCard(
5455
val pickerAlignment = if (direction == QuantityPickerDirection.VERTICAL)
5556
Alignment.TopEnd else Alignment.TopStart
5657
QuantityPicker(
57-
icons = quantityPickerIcons,
5858
modifier = modifier.align(pickerAlignment),
59+
icons = quantityPickerIcons.copy(
60+
addIconBackgroundColor = QuantityPickerDefaults.defaultColor
61+
),
5962
direction = direction,
6063
quantityData = productData.quantityData,
6164
showLoading = productData.isLoading,

0 commit comments

Comments
 (0)