Add optional color with default value for LabelChipView.kt
This commit is contained in:
parent
f92f6151a4
commit
40dd5a0350
|
@ -91,7 +91,8 @@ class MultiLineLabelChipGroupViewTest : ViewTest<MultiLineLabelChipGroupView>(::
|
|||
|
||||
// then
|
||||
for (label in labels) {
|
||||
onView(withText(label.name.s)).check(matches(withBackgroundColor(label.color)))
|
||||
val labelColor = checkNotNull(label.color)
|
||||
onView(withText(label.name.s)).check(matches(withBackgroundColor(labelColor)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -104,9 +104,12 @@ class SingleLineLabelChipGroupViewTest : ViewTest<SingleLineLabelChipGroupView>(
|
|||
chipGroupView.setLabels(labels)
|
||||
|
||||
// then
|
||||
val (expectedLabelName, expectedLabelColor) = with(labels.first()) {
|
||||
name.s to checkNotNull(color)
|
||||
}
|
||||
onLabelView()
|
||||
.check(matches(withText(testLabelsList.first().name.s)))
|
||||
.check(matches(withBackgroundColor(testLabelsList.first().color)))
|
||||
.check(matches(withText(expectedLabelName)))
|
||||
.check(matches(withBackgroundColor(expectedLabelColor)))
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -24,6 +24,7 @@ import android.content.res.ColorStateList
|
|||
import android.graphics.Color
|
||||
import android.text.TextUtils
|
||||
import android.util.AttributeSet
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.appcompat.widget.AppCompatTextView
|
||||
import androidx.core.content.withStyledAttributes
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
|
@ -44,6 +45,10 @@ class LabelChipView @JvmOverloads constructor(
|
|||
defStyleAttr: Int = android.R.attr.textViewStyle
|
||||
) : AppCompatTextView(context, attrs, defStyleAttr) {
|
||||
|
||||
@get:ColorInt
|
||||
private val defaultBackgroundTint: Int get() =
|
||||
context.getColor(R.color.shade_60)
|
||||
|
||||
init {
|
||||
// Padding
|
||||
val horizontalPadding = resources.getDimensionPixelSize(R.dimen.padding_m)
|
||||
|
@ -72,8 +77,8 @@ class LabelChipView @JvmOverloads constructor(
|
|||
.apply { fillColor = color }
|
||||
}
|
||||
|
||||
fun setLabelColor(color: Int) {
|
||||
setLabelColor(ColorStateList.valueOf(color))
|
||||
fun setLabelColor(@ColorInt color: Int?) {
|
||||
setLabelColor(ColorStateList.valueOf(color ?: defaultBackgroundTint))
|
||||
}
|
||||
|
||||
private fun buildBackgroundShape() = ShapeAppearanceModel
|
||||
|
@ -88,10 +93,16 @@ class LabelChipView @JvmOverloads constructor(
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ui Model for [LabelChipView]
|
||||
* @property color is the [ColorInt] that will be applied as background.
|
||||
* if `null` a default background will be used, for ensure readability of the text
|
||||
*/
|
||||
data class LabelChipUiModel(
|
||||
val id: Id,
|
||||
val name: Name,
|
||||
val color: Int
|
||||
@ColorInt
|
||||
val color: Int?
|
||||
) {
|
||||
|
||||
companion object {
|
||||
|
|
Loading…
Reference in New Issue