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

Create Color entity for Label logic.

parent 74736590
package ru.terrakok.gitlabclient.entity
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 04.01.19.
*/
data class Color(
val colorString: String,
val colorInt: Int
)
\ No newline at end of file
......@@ -10,7 +10,7 @@ data class Label(
@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("color") val color: Color,
@SerializedName("description") val description: String?,
@SerializedName("open_issues_count") val openIssuesCount: Int,
@SerializedName("closed_issues_count") val closedIssuesCount: Int,
......
......@@ -197,12 +197,4 @@ 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
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 {
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,9 +9,9 @@ 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.parseColorWithDefault
import ru.terrakok.gitlabclient.extension.setBackgroundTintByColor
/**
......@@ -56,14 +55,13 @@ class ProjectLabelAdapterDelegate : AdapterDelegate<MutableList<Any>>() {
setLabelColor(item.color)
}
private fun setLabelColor(color: String) = with(itemView.labelTitleTextView) {
val labelColor = parseColorWithDefault(color, Color.GREEN)
private fun setLabelColor(color: Color) = with(itemView.labelTitleTextView) {
val textColor = when {
isColorDark(labelColor) -> ContextCompat.getColor(context, R.color.white)
isColorDark(color.colorInt) -> ContextCompat.getColor(context, R.color.white)
else -> ContextCompat.getColor(context, R.color.primary_text)
}
setBackgroundTintByColor(labelColor)
setBackgroundTintByColor(color.colorInt)
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