Commit 98167db3 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Merge branch 'fix/project_labels_color_crash' into 'develop'

Fix crash with parsing color for project labels.

See merge request !156
parents 4001fe5a 675af6f7
package ru.terrakok.gitlabclient.entity
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 04.01.19.
*/
data class Color(
val name: String,
val value: Int
)
\ No newline at end of file
......@@ -5,11 +5,10 @@ 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,
@SerializedName("color") val color: String,
@SerializedName("color") val color: Color,
@SerializedName("description") val description: String?,
@SerializedName("open_issues_count") val openIssuesCount: Int,
@SerializedName("closed_issues_count") val closedIssuesCount: Int,
......
package ru.terrakok.gitlabclient.model.data.server.deserializer
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import ru.terrakok.gitlabclient.entity.Color
import java.lang.reflect.Type
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 04.01.19.
*/
class ColorDeserializer : JsonDeserializer<Color> {
override fun deserialize(
json: JsonElement,
typeOfT: Type,
context: JsonDeserializationContext
): Color {
// 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.
val colorString = json.asJsonPrimitive.asString
val colorInt = try {
android.graphics.Color.parseColor(colorString)
} catch (e: IllegalArgumentException) {
android.graphics.Color.GREEN
}
return Color(colorString, colorInt)
}
}
\ No newline at end of file
......@@ -3,7 +3,9 @@ package ru.terrakok.gitlabclient.toothpick.provider
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.threeten.bp.LocalDateTime
import ru.terrakok.gitlabclient.entity.Color
import ru.terrakok.gitlabclient.entity.todo.Todo
import ru.terrakok.gitlabclient.model.data.server.deserializer.ColorDeserializer
import ru.terrakok.gitlabclient.model.data.server.deserializer.DateDeserializer
import ru.terrakok.gitlabclient.model.data.server.deserializer.TodoDeserializer
import javax.inject.Inject
......@@ -18,5 +20,6 @@ class GsonProvider @Inject constructor() : Provider<Gson> {
GsonBuilder()
.registerTypeAdapter(LocalDateTime::class.java, DateDeserializer())
.registerTypeAdapter(Todo::class.java, TodoDeserializer())
.registerTypeAdapter(Color::class.java, ColorDeserializer())
.create()
}
\ No newline at end of file
package ru.terrakok.gitlabclient.ui.project.labels
import android.annotation.SuppressLint
import android.graphics.Color
import android.support.v4.content.ContextCompat
import android.support.v4.graphics.ColorUtils
import android.support.v7.widget.RecyclerView
......@@ -10,6 +9,7 @@ import android.view.ViewGroup
import com.hannesdorfmann.adapterdelegates3.AdapterDelegate
import kotlinx.android.synthetic.main.item_label.view.*
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.entity.Color
import ru.terrakok.gitlabclient.entity.Label
import ru.terrakok.gitlabclient.extension.inflate
import ru.terrakok.gitlabclient.extension.setBackgroundTintByColor
......@@ -55,14 +55,13 @@ class ProjectLabelAdapterDelegate : AdapterDelegate<MutableList<Any>>() {
setLabelColor(item.color)
}
private fun setLabelColor(color: String) = with(itemView.labelTitleTextView) {
val labelColor = Color.parseColor(color)
private fun setLabelColor(color: Color) = with(itemView.labelTitleTextView) {
val textColor = when {
isColorDark(labelColor) -> ContextCompat.getColor(context, R.color.white)
isColorDark(color.value) -> ContextCompat.getColor(context, R.color.white)
else -> ContextCompat.getColor(context, R.color.primary_text)
}
setBackgroundTintByColor(labelColor)
setBackgroundTintByColor(color.value)
setTextColor(textColor)
}
......
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