Skip to content

Commit 4f609aa

Browse files
author
belfu.ogretir
committed
add Font Support for QuantityPickerView
1 parent 9f021d5 commit 4f609aa

File tree

8 files changed

+51
-26
lines changed

8 files changed

+51
-26
lines changed

libraries/quantity-picker-view/README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ To set programmatically, you can call `QuantityPickerView.setQuantityPickerViewS
5454
| qpv_quantityBackgroundVerticalPadding | quantityBackgroundVerticalPadding | padding for quantity background vertically if `orientation` is `horizontal`, else horizontal padding. | `2dp` |
5555
| qpv_add_contentDescription | text | Text for Add Image of Talkback | "" |
5656
| qpv_remove_contentDescription | text | Text for Remove Image of Talkback | "" |
57+
| qpv_fontFamily | fontFamily | Font family for texts. | |
5758

5859
# Public methods
5960

@@ -63,12 +64,13 @@ To set programmatically, you can call `QuantityPickerView.setQuantityPickerViewS
6364
| setQuantity | quantity: Int | To set quantity immediately. |
6465
| setMaxQuantity | maxQuantity: Int | To set maxQuantity immediately. |
6566
| setMinQuantity | minQuantity: Int | To set minQuantity immediately. |
66-
| setBackgroundImageDrawable | background: Drawable | To set backgroundImageDrawable immediately. |
67+
| setBackgroundImageDrawable | background: Drawable | To set backgroundImageDrawable immediately. |
6768
| stopLoading | | To stop current loading. |
6869
| reset | | To stop loading and set currentQuantity to 0. |
6970
| incrementQuantityBy | quantity | increments current total quantity by quantity parameter |
7071

7172
## Listeners
73+
7274
To get updates on **QuantityPickerView** you need to set this listeners:
7375

7476
| Listener | Data | Return | Information |
@@ -83,17 +85,12 @@ To get updates on **QuantityPickerView** you need to set this listeners:
8385
From XML, you can use attributes like below:
8486

8587
```xml
88+
8689
<com.trendyol.uicomponents.quantitypickerview.QuantityPickerView
87-
android:id="@+id/quantity_picker_view"
88-
android:layout_width="match_parent"
89-
android:layout_height="36dp"
90-
app:qpv_currentQuantity="1"
91-
app:qpv_maxQuantity="10"
92-
app:qpv_minQuantity="1"
93-
app:qpv_quantityBackground="@drawable/qpv_shape_default_background"
94-
app:qpv_quantityTextSize="14sp"
95-
app:qpv_text="Add to Cart"
96-
app:qpv_textSize="12sp" />
90+
android:id="@+id/quantity_picker_view" android:layout_width="match_parent"
91+
android:layout_height="36dp" app:qpv_currentQuantity="1" app:qpv_maxQuantity="10"
92+
app:qpv_minQuantity="1" app:qpv_quantityBackground="@drawable/qpv_shape_default_background"
93+
app:qpv_quantityTextSize="14sp" app:qpv_text="Add to Cart" app:qpv_textSize="12sp" />
9794
```
9895

9996
To set *QuantityPickerViewState* programmatically:
@@ -112,16 +109,19 @@ val viewState = QuantityPickerViewState(
112109
progressTintColor = themeColor(R.attr.colorAccent),
113110
quantityTextColor = themeColor(R.attr.colorPrimary)
114111
)
115-
112+
116113
findViewById<QuantityPickerView>(R.id.quantity_picker_view).setQuantityPickerViewState(viewState)
117114
```
118115

119116
## Contributors
120-
This library is maintained mainly by Trendyol Android Team members but also other Android lovers contributes.
117+
118+
This library is maintained mainly by Trendyol Android Team members but also other Android lovers
119+
contributes.
121120

122121
We developed this component for our needs, there is lots of improvements need to be implemented.
123122

124123
# License
124+
125125
Copyright 2022 Trendyol.com
126126

127127
Licensed under the Apache License, Version 2.0 (the "License");

libraries/quantity-picker-view/src/main/java/com/trendyol/uicomponents/quantitypickerview/BindingAdapters.kt

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.trendyol.uicomponents.quantitypickerview
33
import android.graphics.Typeface
44
import android.util.TypedValue
55
import androidx.appcompat.widget.AppCompatTextView
6+
import androidx.core.content.res.ResourcesCompat
7+
68

79
internal fun AppCompatTextView.setTextAppearance(
810
quantityPickerTextAppearance: QuantityPickerTextAppearance
@@ -16,4 +18,10 @@ internal fun AppCompatTextView.setTextAppearance(
1618
setTypeface(typeface, style)
1719
setTextSize(TypedValue.COMPLEX_UNIT_PX, quantityPickerTextAppearance.textSize.toFloat())
1820
setTextColor(quantityPickerTextAppearance.textColor)
21+
22+
//or to support all versions use
23+
if (quantityPickerTextAppearance.textFontFamily != -1) {
24+
val typeface = ResourcesCompat.getFont(context, quantityPickerTextAppearance.textFontFamily)
25+
setTypeface(typeface)
26+
}
1927
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.trendyol.uicomponents.quantitypickerview
22

33
import androidx.annotation.ColorInt
4+
import androidx.annotation.FontRes
45

56
data class QuantityPickerTextAppearance(
67
@ColorInt val textColor: Int,
78
val textSize: Int,
8-
val textStyle: Int
9+
val textStyle: Int,
10+
@FontRes val textFontFamily: Int
911
)

libraries/quantity-picker-view/src/main/java/com/trendyol/uicomponents/quantitypickerview/QuantityPickerView.kt

+11-3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class QuantityPickerView : ConstraintLayout {
153153
fun setMinQuantity(minQuantity: Int) =
154154
setQuantityPickerViewState(viewState.getWithMinQuantity(minQuantity))
155155

156-
fun setBackgroundImageDrawable(background: Drawable){
156+
fun setBackgroundImageDrawable(background: Drawable) {
157157
setQuantityPickerViewState(viewState.getWithBackgroundDrawable(background))
158158
}
159159

@@ -286,6 +286,8 @@ class QuantityPickerView : ConstraintLayout {
286286
val removeContentDescription = it.getString(
287287
R.styleable.QuantityPickerView_qpv_remove_contentDescription
288288
) ?: ""
289+
val textFontFamily =
290+
it.getResourceId(R.styleable.QuantityPickerView_qpv_fontFamily, -1)
289291

290292
return QuantityPickerViewState(
291293
text = text,
@@ -314,7 +316,8 @@ class QuantityPickerView : ConstraintLayout {
314316
maxQuantity = maxQuantity,
315317
minQuantity = minQuantity,
316318
addContentDescription = addContentDescription,
317-
removeContentDescription = removeContentDescription
319+
removeContentDescription = removeContentDescription,
320+
textFontFamily = textFontFamily
318321
)
319322
}
320323
}
@@ -360,7 +363,12 @@ class QuantityPickerView : ConstraintLayout {
360363
visibility = viewState.getProgressBarVisibility()
361364
indeterminateTintList = ColorStateList.valueOf(viewState.progressTintColor)
362365
val progressVerticalPadding = viewState.progressVerticalPadding
363-
setPadding(paddingLeft, progressVerticalPadding, paddingRight, progressVerticalPadding)
366+
setPadding(
367+
paddingLeft,
368+
progressVerticalPadding,
369+
paddingRight,
370+
progressVerticalPadding
371+
)
364372
}
365373
with(imageAdd) {
366374
contentDescription = viewState.addContentDescription

libraries/quantity-picker-view/src/main/java/com/trendyol/uicomponents/quantitypickerview/QuantityPickerViewState.kt

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package com.trendyol.uicomponents.quantitypickerview
22

33
import android.content.Context
44
import android.graphics.drawable.Drawable
5+
import android.graphics.fonts.FontFamily
56
import android.view.View
67
import androidx.annotation.ColorInt
8+
import androidx.annotation.FontRes
9+
import org.intellij.lang.annotations.JdkConstants.FontStyle
710

811
data class QuantityPickerViewState(
912
private val text: String,
@@ -32,7 +35,8 @@ data class QuantityPickerViewState(
3235
val disabledAddIconDrawable: Drawable? = addIconDrawable,
3336
val disabledSubtractIconDrawable: Drawable? = subtractIconDrawable,
3437
val addContentDescription: String,
35-
val removeContentDescription: String
38+
val removeContentDescription: String,
39+
@FontRes val textFontFamily: Int
3640
) {
3741

3842
internal fun isInQuantityMode(): Boolean = currentQuantity > 0
@@ -58,10 +62,10 @@ data class QuantityPickerViewState(
5862
}
5963

6064
fun getQuantityPickerTextAppearance(): QuantityPickerTextAppearance =
61-
QuantityPickerTextAppearance(textColor, textSize, textStyle)
65+
QuantityPickerTextAppearance(textColor, textSize, textStyle, textFontFamily)
6266

6367
fun getQuantityTextAppearance() =
64-
QuantityPickerTextAppearance(quantityTextColor, quantityTextSize, quantityTextStyle)
68+
QuantityPickerTextAppearance(quantityTextColor, quantityTextSize, quantityTextStyle, textFontFamily)
6569

6670
fun getQuantity() = currentQuantity.takeIf { it != 0 }?.toString()
6771

@@ -208,7 +212,8 @@ data class QuantityPickerViewState(
208212
maxQuantity = 0,
209213
minQuantity = 0,
210214
addContentDescription = "",
211-
removeContentDescription = ""
215+
removeContentDescription = "",
216+
textFontFamily = -1
212217
)
213218
}
214219
}

libraries/quantity-picker-view/src/main/res/layout/view_quantity_picker.xml

-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
android:id="@+id/text"
4040
android:layout_width="0dp"
4141
android:layout_height="0dp"
42-
android:fontFamily="sans-serif-medium"
4342
android:gravity="center"
4443
android:includeFontPadding="false"
4544
android:maxLines="1"
@@ -56,7 +55,6 @@
5655
android:id="@+id/quantityText"
5756
android:layout_width="0dp"
5857
android:layout_height="0dp"
59-
android:fontFamily="sans-serif-medium"
6058
android:gravity="center"
6159
android:includeFontPadding="false"
6260
android:minEms="3"

libraries/quantity-picker-view/src/main/res/values/attrs.xml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<enum name="bold" value="1" />
1111
<enum name="italic" value="2" />
1212
</attr>
13+
<attr name="qpv_fontFamily" format="string"/>
1314
<attr name="qpv_quantityTextColor" format="color" />
1415
<attr name="qpv_quantityTextSize" format="dimension" />
1516
<attr name="qpv_quantityTextStyle" format="enum">

sample/src/main/java/com/trendyol/uicomponents/QuantityPickerViewActivity.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ class QuantityPickerViewActivity : AppCompatActivity() {
1515

1616
override fun onCreate(savedInstanceState: Bundle?) {
1717
super.onCreate(savedInstanceState)
18-
setContentView(ActivityQuantityPickerViewBinding.inflate(layoutInflater).also { binding = it }.root)
18+
setContentView(
19+
ActivityQuantityPickerViewBinding.inflate(layoutInflater).also { binding = it }.root
20+
)
1921

2022
val viewState = QuantityPickerViewState(
21-
text = "Fresh Money",
23+
text = "غداً",
2224
textSize = asSP(12),
2325
quantityTextSize = asSP(14),
2426
backgroundDrawable = drawable(QuantityPickerViewR.drawable.qpv_shape_default_background),
@@ -34,7 +36,8 @@ class QuantityPickerViewActivity : AppCompatActivity() {
3436
progressVerticalPadding = asDP(6),
3537
quantityBackgroundVerticalPadding = asDP(6),
3638
addContentDescription = "Add",
37-
removeContentDescription = "Remove"
39+
removeContentDescription = "Remove",
40+
textFontFamily = -1
3841
)
3942

4043
binding.quantityPickerView2.setQuantityPickerViewState(viewState)

0 commit comments

Comments
 (0)