Commit 74736590 authored by Eugene Shapovalov's avatar Eugene Shapovalov 💬

Fix crash with parsing color for project labels.

parent 90663b1e
......@@ -5,10 +5,11 @@ import com.google.gson.annotations.SerializedName
/**
* @author Maxim Myalkin (MaxMyalkin) on 29.10.2018.
*/
data class Label(
@SerializedName("id") val id: Long,
@SerializedName("name") val name: String,
// The color of the label given in 6-digit hex notation with leading ‘#’ sign (e.g. #FFAABB)
// or one of the CSS color names. So according to CSS color names it can be named differently on Android.
@SerializedName("color") val color: String,
@SerializedName("description") val description: String?,
@SerializedName("open_issues_count") val openIssuesCount: Int,
......
......@@ -197,4 +197,12 @@ fun Any.objectScopeName() = "${javaClass.simpleName}_${hashCode()}"
fun View.setBackgroundTintByColor(@ColorInt color: Int) {
val wrappedDrawable = DrawableCompat.wrap(background)
DrawableCompat.setTint(wrappedDrawable.mutate(), color)
}
fun parseColorWithDefault(colorString: String, defaultColor: Int): Int {
return try {
Color.parseColor(colorString)
} catch (e: IllegalArgumentException) {
defaultColor
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import kotlinx.android.synthetic.main.item_label.view.*
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.entity.Label
import ru.terrakok.gitlabclient.extension.inflate
import ru.terrakok.gitlabclient.extension.parseColorWithDefault
import ru.terrakok.gitlabclient.extension.setBackgroundTintByColor
/**
......@@ -56,7 +57,7 @@ class ProjectLabelAdapterDelegate : AdapterDelegate<MutableList<Any>>() {
}
private fun setLabelColor(color: String) = with(itemView.labelTitleTextView) {
val labelColor = Color.parseColor(color)
val labelColor = parseColorWithDefault(color, Color.GREEN)
val textColor = when {
isColorDark(labelColor) -> ContextCompat.getColor(context, R.color.white)
else -> ContextCompat.getColor(context, R.color.primary_text)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment