Skip to content

Commit

Permalink
add Font Support for QuantityPickerView
Browse files Browse the repository at this point in the history
  • Loading branch information
belfu.ogretir committed Sep 30, 2024
1 parent 9f021d5 commit 4f609aa
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 26 deletions.
26 changes: 13 additions & 13 deletions libraries/quantity-picker-view/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ To set programmatically, you can call `QuantityPickerView.setQuantityPickerViewS
| qpv_quantityBackgroundVerticalPadding | quantityBackgroundVerticalPadding | padding for quantity background vertically if `orientation` is `horizontal`, else horizontal padding. | `2dp` |
| qpv_add_contentDescription | text | Text for Add Image of Talkback | "" |
| qpv_remove_contentDescription | text | Text for Remove Image of Talkback | "" |
| qpv_fontFamily | fontFamily | Font family for texts. | |

# Public methods

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

## Listeners

To get updates on **QuantityPickerView** you need to set this listeners:

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

```xml

<com.trendyol.uicomponents.quantitypickerview.QuantityPickerView
android:id="@+id/quantity_picker_view"
android:layout_width="match_parent"
android:layout_height="36dp"
app:qpv_currentQuantity="1"
app:qpv_maxQuantity="10"
app:qpv_minQuantity="1"
app:qpv_quantityBackground="@drawable/qpv_shape_default_background"
app:qpv_quantityTextSize="14sp"
app:qpv_text="Add to Cart"
app:qpv_textSize="12sp" />
android:id="@+id/quantity_picker_view" android:layout_width="match_parent"
android:layout_height="36dp" app:qpv_currentQuantity="1" app:qpv_maxQuantity="10"
app:qpv_minQuantity="1" app:qpv_quantityBackground="@drawable/qpv_shape_default_background"
app:qpv_quantityTextSize="14sp" app:qpv_text="Add to Cart" app:qpv_textSize="12sp" />
```

To set *QuantityPickerViewState* programmatically:
Expand All @@ -112,16 +109,19 @@ val viewState = QuantityPickerViewState(
progressTintColor = themeColor(R.attr.colorAccent),
quantityTextColor = themeColor(R.attr.colorPrimary)
)

findViewById<QuantityPickerView>(R.id.quantity_picker_view).setQuantityPickerViewState(viewState)
```

## Contributors
This library is maintained mainly by Trendyol Android Team members but also other Android lovers contributes.

This library is maintained mainly by Trendyol Android Team members but also other Android lovers
contributes.

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

# License

Copyright 2022 Trendyol.com

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.trendyol.uicomponents.quantitypickerview
import android.graphics.Typeface
import android.util.TypedValue
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.res.ResourcesCompat


internal fun AppCompatTextView.setTextAppearance(
quantityPickerTextAppearance: QuantityPickerTextAppearance
Expand All @@ -16,4 +18,10 @@ internal fun AppCompatTextView.setTextAppearance(
setTypeface(typeface, style)
setTextSize(TypedValue.COMPLEX_UNIT_PX, quantityPickerTextAppearance.textSize.toFloat())
setTextColor(quantityPickerTextAppearance.textColor)

//or to support all versions use
if (quantityPickerTextAppearance.textFontFamily != -1) {
val typeface = ResourcesCompat.getFont(context, quantityPickerTextAppearance.textFontFamily)
setTypeface(typeface)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.trendyol.uicomponents.quantitypickerview

import androidx.annotation.ColorInt
import androidx.annotation.FontRes

data class QuantityPickerTextAppearance(
@ColorInt val textColor: Int,
val textSize: Int,
val textStyle: Int
val textStyle: Int,
@FontRes val textFontFamily: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class QuantityPickerView : ConstraintLayout {
fun setMinQuantity(minQuantity: Int) =
setQuantityPickerViewState(viewState.getWithMinQuantity(minQuantity))

fun setBackgroundImageDrawable(background: Drawable){
fun setBackgroundImageDrawable(background: Drawable) {
setQuantityPickerViewState(viewState.getWithBackgroundDrawable(background))
}

Expand Down Expand Up @@ -286,6 +286,8 @@ class QuantityPickerView : ConstraintLayout {
val removeContentDescription = it.getString(
R.styleable.QuantityPickerView_qpv_remove_contentDescription
) ?: ""
val textFontFamily =
it.getResourceId(R.styleable.QuantityPickerView_qpv_fontFamily, -1)

return QuantityPickerViewState(
text = text,
Expand Down Expand Up @@ -314,7 +316,8 @@ class QuantityPickerView : ConstraintLayout {
maxQuantity = maxQuantity,
minQuantity = minQuantity,
addContentDescription = addContentDescription,
removeContentDescription = removeContentDescription
removeContentDescription = removeContentDescription,
textFontFamily = textFontFamily
)
}
}
Expand Down Expand Up @@ -360,7 +363,12 @@ class QuantityPickerView : ConstraintLayout {
visibility = viewState.getProgressBarVisibility()
indeterminateTintList = ColorStateList.valueOf(viewState.progressTintColor)
val progressVerticalPadding = viewState.progressVerticalPadding
setPadding(paddingLeft, progressVerticalPadding, paddingRight, progressVerticalPadding)
setPadding(
paddingLeft,
progressVerticalPadding,
paddingRight,
progressVerticalPadding
)
}
with(imageAdd) {
contentDescription = viewState.addContentDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package com.trendyol.uicomponents.quantitypickerview

import android.content.Context
import android.graphics.drawable.Drawable
import android.graphics.fonts.FontFamily
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.FontRes
import org.intellij.lang.annotations.JdkConstants.FontStyle

data class QuantityPickerViewState(
private val text: String,
Expand Down Expand Up @@ -32,7 +35,8 @@ data class QuantityPickerViewState(
val disabledAddIconDrawable: Drawable? = addIconDrawable,
val disabledSubtractIconDrawable: Drawable? = subtractIconDrawable,
val addContentDescription: String,
val removeContentDescription: String
val removeContentDescription: String,
@FontRes val textFontFamily: Int
) {

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

fun getQuantityPickerTextAppearance(): QuantityPickerTextAppearance =
QuantityPickerTextAppearance(textColor, textSize, textStyle)
QuantityPickerTextAppearance(textColor, textSize, textStyle, textFontFamily)

fun getQuantityTextAppearance() =
QuantityPickerTextAppearance(quantityTextColor, quantityTextSize, quantityTextStyle)
QuantityPickerTextAppearance(quantityTextColor, quantityTextSize, quantityTextStyle, textFontFamily)

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

Expand Down Expand Up @@ -208,7 +212,8 @@ data class QuantityPickerViewState(
maxQuantity = 0,
minQuantity = 0,
addContentDescription = "",
removeContentDescription = ""
removeContentDescription = "",
textFontFamily = -1
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:includeFontPadding="false"
android:maxLines="1"
Expand All @@ -56,7 +55,6 @@
android:id="@+id/quantityText"
android:layout_width="0dp"
android:layout_height="0dp"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:includeFontPadding="false"
android:minEms="3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<enum name="bold" value="1" />
<enum name="italic" value="2" />
</attr>
<attr name="qpv_fontFamily" format="string"/>
<attr name="qpv_quantityTextColor" format="color" />
<attr name="qpv_quantityTextSize" format="dimension" />
<attr name="qpv_quantityTextStyle" format="enum">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class QuantityPickerViewActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(ActivityQuantityPickerViewBinding.inflate(layoutInflater).also { binding = it }.root)
setContentView(
ActivityQuantityPickerViewBinding.inflate(layoutInflater).also { binding = it }.root
)

val viewState = QuantityPickerViewState(
text = "Fresh Money",
text = "غداً",
textSize = asSP(12),
quantityTextSize = asSP(14),
backgroundDrawable = drawable(QuantityPickerViewR.drawable.qpv_shape_default_background),
Expand All @@ -34,7 +36,8 @@ class QuantityPickerViewActivity : AppCompatActivity() {
progressVerticalPadding = asDP(6),
quantityBackgroundVerticalPadding = asDP(6),
addContentDescription = "Add",
removeContentDescription = "Remove"
removeContentDescription = "Remove",
textFontFamily = -1
)

binding.quantityPickerView2.setQuantityPickerViewState(viewState)
Expand Down

0 comments on commit 4f609aa

Please sign in to comment.