Skip to content

Commit 17d102d

Browse files
authored
Merge pull request #133 from Trendyol/feature/quantityPickerComposeAddIconColor
Feature/quantity picker compose add icon color
2 parents 33add82 + f7e4b84 commit 17d102d

File tree

7 files changed

+117
-10
lines changed

7 files changed

+117
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,89 @@
11
package com.trendyol.uicomponents.quantitypicker
22

3+
import androidx.compose.animation.Animatable
4+
import androidx.compose.animation.core.tween
5+
import androidx.compose.foundation.background
36
import androidx.compose.foundation.clickable
47
import androidx.compose.foundation.interaction.MutableInteractionSource
58
import androidx.compose.foundation.layout.padding
9+
import androidx.compose.foundation.shape.CircleShape
610
import androidx.compose.material.Icon
711
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.LaunchedEffect
13+
import androidx.compose.runtime.getValue
14+
import androidx.compose.runtime.mutableStateOf
15+
import androidx.compose.runtime.remember
16+
import androidx.compose.runtime.rememberCoroutineScope
17+
import androidx.compose.runtime.setValue
818
import androidx.compose.ui.Modifier
19+
import androidx.compose.ui.draw.clip
20+
import androidx.compose.ui.graphics.Color
921
import androidx.compose.ui.res.painterResource
1022
import androidx.compose.ui.unit.dp
23+
import kotlinx.coroutines.launch
1124

1225
@Composable
1326
internal fun QuantityAddIcon(
1427
icons: QuantityIcons,
1528
quantityData: QuantityPickerViewData,
1629
onAddClick: (() -> Unit)?
1730
) {
31+
val coroutineScope = rememberCoroutineScope()
32+
var targetBackgroundColor by remember {
33+
mutableStateOf(quantityData.getBackgroundColor(icons))
34+
}
35+
var iconTintColor by remember {
36+
mutableStateOf(
37+
quantityData.getAddIconColor(
38+
icons,
39+
quantityData.currentQuantity
40+
)
41+
)
42+
}
43+
val animatedBackgroundColor = remember { Animatable(targetBackgroundColor) }
44+
val lastQuantityCount = remember { mutableStateOf(quantityData.currentQuantity) }
45+
46+
val setTargetBackgroundColor: (color: Color) -> Unit = remember {
47+
{ color ->
48+
if (targetBackgroundColor != color) {
49+
targetBackgroundColor = color
50+
coroutineScope.launch {
51+
animatedBackgroundColor.animateTo(
52+
targetValue = targetBackgroundColor,
53+
animationSpec = tween(500)
54+
)
55+
}
56+
}
57+
}
58+
}
59+
60+
LaunchedEffect(key1 = quantityData.currentQuantity) {
61+
if (lastQuantityCount.value != quantityData.currentQuantity) {
62+
lastQuantityCount.value = quantityData.currentQuantity
63+
iconTintColor = quantityData.getAddIconColor(icons, quantityData.currentQuantity)
64+
setTargetBackgroundColor.invoke(quantityData.getBackgroundColor(icons))
65+
}
66+
}
67+
1868
Icon(
1969
painter = painterResource(id = icons.addIconResId),
20-
tint = quantityData.getAddIconColor(icons.iconColor, icons.disabledColor),
70+
tint = iconTintColor,
2171
contentDescription = null,
2272
modifier = Modifier
73+
.clip(CircleShape)
74+
.background(animatedBackgroundColor.value)
75+
.padding(8.dp)
2376
.clickable(
24-
enabled = quantityData.isAddButtonEnabled(),
25-
onClick = { onAddClick?.invoke() },
26-
interactionSource = MutableInteractionSource(),
2777
indication = null,
78+
interactionSource = MutableInteractionSource(),
79+
enabled = quantityData.isAddButtonEnabled(),
80+
onClick = {
81+
if (quantityData.currentQuantity == 0) {
82+
setTargetBackgroundColor.invoke(Color.White)
83+
iconTintColor = icons.iconColor
84+
}
85+
onAddClick?.invoke()
86+
}
2887
)
29-
.padding(8.dp)
3088
)
3189
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ data class QuantityIcons(
1010
@DrawableRes val removeIconResId: Int? = R.drawable.ic_trash,
1111
val iconColor: Color,
1212
val disabledColor: Color,
13+
val addIconBackgroundColor: Color? = null,
1314
) {
1415
companion object {
1516
val default = QuantityIcons(

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import androidx.compose.foundation.background
55
import androidx.compose.foundation.border
66
import androidx.compose.foundation.layout.Column
77
import androidx.compose.foundation.layout.Row
8+
import androidx.compose.foundation.layout.padding
89
import androidx.compose.runtime.Composable
910
import androidx.compose.ui.Alignment
1011
import androidx.compose.ui.Modifier
1112
import androidx.compose.ui.graphics.Color
1213
import androidx.compose.ui.text.TextStyle
1314
import androidx.compose.ui.tooling.preview.Preview
15+
import androidx.compose.ui.unit.dp
1416

1517
@Composable
1618
fun QuantityPicker(
@@ -90,7 +92,10 @@ internal fun VerticalQuantityPicker(
9092
)
9193

9294
AnimatedVisibility(visible = quantityData.currentQuantity > 0 || showLoading) {
93-
Column(horizontalAlignment = Alignment.CenterHorizontally) {
95+
Column(
96+
modifier = Modifier.padding(top = 2.dp),
97+
horizontalAlignment = Alignment.CenterHorizontally
98+
) {
9499
QuantityText(
95100
quantityData = quantityData,
96101
shape = quantityTextShape,
@@ -138,7 +143,10 @@ internal fun HorizontalQuantityPicker(
138143
) {
139144

140145
AnimatedVisibility(visible = quantityData.currentQuantity > 0 || showLoading) {
141-
Row(verticalAlignment = Alignment.CenterVertically) {
146+
Row(
147+
modifier = Modifier.padding(top = 2.dp),
148+
verticalAlignment = Alignment.CenterVertically
149+
) {
142150
QuantitySubtractIcon(
143151
icons = icons,
144152
quantityData = quantityData,
@@ -157,7 +165,7 @@ internal fun HorizontalQuantityPicker(
157165
QuantityAddIcon(
158166
icons = icons,
159167
quantityData = quantityData,
160-
onAddClick = onAddClick,
168+
onAddClick = onAddClick
161169
)
162170
}
163171
}
@@ -201,4 +209,13 @@ private fun PreviewSubtractButtonDisabledQuantityPicker() {
201209
QuantityPicker(
202210
quantityData = QuantityPickerViewData(currentQuantity = 2, minQuantity = 3),
203211
)
212+
}
213+
214+
@Preview
215+
@Composable
216+
private fun PreviewAddButtonVisibleQuantityPicker() {
217+
QuantityPicker(
218+
icons = QuantityIcons.default.copy(addIconBackgroundColor = QuantityPickerDefaults.defaultColor),
219+
quantityData = QuantityPickerViewData(currentQuantity = 0),
220+
)
204221
}

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@ data class QuantityPickerViewData(
1515
return maxQuantity != -1
1616
}
1717

18-
fun getAddIconColor(iconTintColor: Color, disableTintColor: Color): Color {
19-
return if (isAddButtonEnabled()) iconTintColor else disableTintColor
18+
fun getAddIconColor(
19+
icons: QuantityIcons,
20+
currentQuantity: Int
21+
): Color {
22+
val iconColor = if (currentQuantity == 0 && icons.addIconBackgroundColor != null) {
23+
Color.White
24+
} else {
25+
icons.iconColor
26+
}
27+
return if (isAddButtonEnabled()) iconColor else icons.disabledColor
2028
}
2129

30+
fun getBackgroundColor(
31+
icons: QuantityIcons
32+
): Color = if (currentQuantity == 0) {
33+
icons.addIconBackgroundColor ?: Color.White
34+
} else Color.White
35+
36+
2237
fun isSubtractButtonEnabled(): Boolean {
2338
return (currentQuantity > minQuantity)
2439
}

libraries/timeline-view-compose/build.gradle.kts

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ android {
1616
vectorDrawables.useSupportLibrary = true
1717
}
1818

19+
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
20+
compilerOptions.freeCompilerArgs.addAll(
21+
"-P",
22+
"plugin:androidx.compose.compiler.plugins.kotlin:experimentalStrongSkipping=true",
23+
)
24+
}
25+
26+
1927
buildTypes {
2028
getByName<com.android.build.gradle.internal.dsl.BuildType>("release") {
2129
isMinifyEnabled = false

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

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import androidx.compose.runtime.remember
1515
import androidx.compose.runtime.rememberCoroutineScope
1616
import androidx.compose.ui.Modifier
1717
import androidx.compose.ui.unit.dp
18+
import com.trendyol.uicomponents.quantitypicker.QuantityIcons
19+
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDefaults
1820
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDirection
1921
import com.trendyol.uicomponents.quantitypicker.QuantityPickerViewData
2022
import com.trendyol.uicomponents.samplecompose.ui.productcard.ProductCard
@@ -150,6 +152,9 @@ private fun ProductSliderSection() {
150152
) {
151153
items(products) { product ->
152154
ProductCard(
155+
quantityPickerIcons = QuantityIcons.default.copy(
156+
addIconBackgroundColor = QuantityPickerDefaults.defaultColor
157+
),
153158
productData = product,
154159
modifier = Modifier.padding(
155160
end = 14.dp,

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

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.compose.ui.Alignment
1313
import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.res.painterResource
1515
import androidx.compose.ui.unit.dp
16+
import com.trendyol.uicomponents.quantitypicker.QuantityIcons
1617
import com.trendyol.uicomponents.quantitypicker.QuantityPicker
1718
import com.trendyol.uicomponents.quantitypicker.QuantityPickerDirection
1819
import com.trendyol.uicomponents.sample.compose.R
@@ -22,6 +23,7 @@ import com.trendyol.uicomponents.samplecompose.ui.theme.Typography
2223
fun ProductCard(
2324
modifier: Modifier = Modifier,
2425
productData: ProductCardData,
26+
quantityPickerIcons: QuantityIcons = QuantityIcons.default,
2527
direction: QuantityPickerDirection = QuantityPickerDirection.VERTICAL,
2628
onAddClick: ((ProductCardData) -> Unit),
2729
onSubtractClick: ((ProductCardData) -> Unit)
@@ -52,6 +54,7 @@ fun ProductCard(
5254
val pickerAlignment = if (direction == QuantityPickerDirection.VERTICAL)
5355
Alignment.TopEnd else Alignment.TopStart
5456
QuantityPicker(
57+
icons = quantityPickerIcons,
5558
modifier = modifier.align(pickerAlignment),
5659
direction = direction,
5760
quantityData = productData.quantityData,

0 commit comments

Comments
 (0)