Skip to content

Commit

Permalink
Better fields support
Browse files Browse the repository at this point in the history
  • Loading branch information
luciofm committed Oct 3, 2018
1 parent 9047fc6 commit bdebcf2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import chat.rocket.core.model.attachment.AudioAttachment
import chat.rocket.core.model.attachment.AuthorAttachment
import chat.rocket.core.model.attachment.Color
import chat.rocket.core.model.attachment.ColorAttachment
import chat.rocket.core.model.attachment.DEFAULT_COLOR
import chat.rocket.core.model.attachment.Field
import chat.rocket.core.model.attachment.FileAttachment
import chat.rocket.core.model.attachment.GenericFileAttachment
Expand Down Expand Up @@ -160,14 +161,17 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
GenericFileAttachment(title, description, text, titleLink, titleLink, titleLinkDownload)
}
text != null && color != null && fallback != null -> {
ColorAttachment(color, text, fallback)
ColorAttachment(color, text, fallback, fields)
}
text != null -> {
MessageAttachment(authorName, authorIcon, text, thumbUrl, color, messageLink, attachments, timestamp)
}
authorLink != null -> {
AuthorAttachment(authorLink, authorIcon, authorName, fields)
}
fields != null -> {
ColorAttachment(color ?: DEFAULT_COLOR, text ?: "", fallback, fields)
}
actions != null -> {
ActionsAttachment(title, actions, buttonAlignment = buttonAlignment ?: "vertical")
}
Expand Down Expand Up @@ -274,6 +278,7 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
name("color").value(attachment.color.rawColor)
name("text").value(attachment.text)
name("fallback").value(attachment.fallback)
attachment.fields?.let { writeFields(writer, it) }
}
writer.endObject()
}
Expand Down Expand Up @@ -347,12 +352,12 @@ class AttachmentAdapter(moshi: Moshi, private val logger: Logger) : JsonAdapter<
name("author_link").value(attachment.url)
name("author_icon").value(attachment.authorIcon)
name("author_name").value(attachment.authorName)
attachment.fields?.let { writeAuthorFields(writer, it) }
attachment.fields?.let { writeFields(writer, it) }
}
writer.endObject()
}

private fun writeAuthorFields(writer: JsonWriter, fields: List<Field>) {
private fun writeFields(writer: JsonWriter, fields: List<Field>) {
if (fields.isNotEmpty()) {
writer.name("fields")
writer.beginArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,43 @@ import se.ansman.kotshi.JsonSerializable
data class ColorAttachment(
val color: Color,
val text: String,
val fallback: String? = null
val fallback: String? = null,
val fields: List<Field>? = null
) : Attachment {
override val url: String
get() = "#$color"
}

@FallbackSealedClass(name = "Custom", fieldName = "colorValue")
sealed class Color(val color: Int, val rawColor: String) {
@Json(name = "good") class Good : Color(0x35AC19, "good")
@Json(name = "warning") class Warning : Color(0xFCB316, "warning")
@Json(name = "danger") class Danger : Color(0xD30230, "danger")
@Json(name = "good") class Good : Color(0x35AC19, "0x35AC19")
@Json(name = "warning") class Warning : Color(0xFCB316, "0xFCB316")
@Json(name = "danger") class Danger : Color(0xD30230, "0xD30230")
class Custom(private val colorValue: String) : Color(parseColor(colorValue), colorValue)

override fun toString(): String {
return color.toString(16)
}
}

private const val DEFAULT_COLOR = 0xA0A0A0
private const val DEFAULT_COLOR_INT = 0xA0A0A0
private const val DEFAULT_COLOR_STR = "0xA0A0A0"
val DEFAULT_COLOR = Color.Custom(DEFAULT_COLOR_STR)

fun String?.asColorInt() = parseColor(this ?: DEFAULT_COLOR_STR)
fun String?.asColor() = Color.Custom(this ?: DEFAULT_COLOR_STR)

private fun parseColor(rawColor: String): Int {
return if (rawColor.startsWith('#')) {
var color = rawColor.substring(1).toLong(16)
if (rawColor.length == 7) {
// Set the alpha value
color = color or -0x1000000
} else if (rawColor.length != 9) {
color = DEFAULT_COLOR.toLong()
color = DEFAULT_COLOR_INT.toLong()
}
color.toInt()
} else {
DEFAULT_COLOR
DEFAULT_COLOR_INT
}
}

0 comments on commit bdebcf2

Please sign in to comment.