Commit cb560bb4 authored by Lucio Maciel's avatar Lucio Maciel

Fixes on color attachments

parent 07c5c0bf
This diff is collapsed.
package chat.rocket.android.chatroom.adapter
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import android.text.method.LinkMovementMethod
import android.view.View
import androidx.core.view.isVisible
import androidx.core.widget.ImageViewCompat
import chat.rocket.android.R
import chat.rocket.android.chatroom.uimodel.ColorAttachmentUiModel
import chat.rocket.android.emoji.EmojiReactionListener
......@@ -15,8 +18,7 @@ class ColorAttachmentViewHolder(itemView: View,
reactionListener: EmojiReactionListener? = null)
: BaseViewHolder<ColorAttachmentUiModel>(itemView, listener, reactionListener) {
val drawable: Drawable? = ContextCompat.getDrawable(itemView.context,
R.drawable.quote_vertical_gray_bar)
val drawable: Drawable = ColorDrawable(ContextCompat.getColor(itemView.context, R.color.quoteBar))
init {
with(itemView) {
......@@ -27,9 +29,19 @@ class ColorAttachmentViewHolder(itemView: View,
override fun bindViews(data: ColorAttachmentUiModel) {
with(itemView) {
drawable?.let {
quote_bar.background = drawable.mutate().apply { setTint(data.color) }
quote_bar.setColorFilter(data.color)
if (data.text.isNotEmpty()) {
attachment_text.isVisible = true
attachment_text.text = data.text
} else {
attachment_text.isVisible = false
}
if (data.fields.isNullOrEmpty()) {
text_fields.isVisible = false
} else {
text_fields.isVisible = true
text_fields.text = data.fields
}
}
}
......
......@@ -9,6 +9,7 @@ data class ColorAttachmentUiModel(
val id: Long,
val color: Int,
val text: CharSequence,
val fields: CharSequence? = null,
override val message: Message,
override val rawData: ColorAttachment,
override val messageId: String,
......
......@@ -42,6 +42,7 @@ import chat.rocket.core.model.attachment.Attachment
import chat.rocket.core.model.attachment.AudioAttachment
import chat.rocket.core.model.attachment.AuthorAttachment
import chat.rocket.core.model.attachment.ColorAttachment
import chat.rocket.core.model.attachment.Field
import chat.rocket.core.model.attachment.FileAttachment
import chat.rocket.core.model.attachment.GenericFileAttachment
import chat.rocket.core.model.attachment.ImageAttachment
......@@ -129,14 +130,14 @@ class UiModelMapper @Inject constructor(
withContext(CommonPool) {
val list = ArrayList<BaseUiModel<*>>()
message.urls?.forEach {
val url = mapUrl(message, it)
url?.let { list.add(url) }
message.urls?.forEach { url ->
mapUrl(message, url)?.let { list.add(it) }
}
message.attachments?.forEach {
val attachment = mapAttachment(message, it)
attachment?.let { list.add(attachment) }
message.attachments?.mapNotNull { attachment ->
mapAttachment(message, attachment)
}?.asReversed()?.let {
list.addAll(it)
}
mapMessage(message).let {
......@@ -334,20 +335,18 @@ class UiModelMapper @Inject constructor(
val localDateTime = DateTimeHelper.getLocalDateTime(message.timestamp)
val dayMarkerText = DateTimeHelper.getFormattedDateForMessages(localDateTime, context)
val fieldsText = mapFields(fields)
ColorAttachmentUiModel(attachmentUrl = url, id = id, color = color.color,
text = text, message = message, rawData = attachment,
text = text, fields = fieldsText, message = message, rawData = attachment,
messageId = message.id, reactions = getReactions(message),
preview = message.copy(message = content.message), unread = message.unread,
showDayMarker = false, currentDayMarkerText = dayMarkerText)
}
}
private fun mapAuthorAttachment(message: Message, attachment: AuthorAttachment): AuthorAttachmentUiModel {
return with(attachment) {
val content = stripMessageQuotes(message)
val fieldsText = fields?.let {
private fun mapFields(fields: List<Field>?): CharSequence? {
return fields?.let {
buildSpannedString {
it.forEachIndexed { index, field ->
bold { append(field.title) }
......@@ -362,6 +361,13 @@ class UiModelMapper @Inject constructor(
}
}
}
}
private fun mapAuthorAttachment(message: Message, attachment: AuthorAttachment): AuthorAttachmentUiModel {
return with(attachment) {
val content = stripMessageQuotes(message)
val fieldsText = mapFields(fields)
val id = attachmentId(message, attachment)
val localDateTime = DateTimeHelper.getLocalDateTime(message.timestamp)
......
......@@ -56,8 +56,8 @@ class DatabaseManager(val context: Application,
fun userDao(): UserDao = database.userDao()
fun messageDao(): MessageDao = database.messageDao()
fun clearUsersStatus() {
launch(dbContext) {
suspend fun clearUsersStatus() {
withContext(dbContext) {
userDao().clearStatus()
}
}
......
......@@ -23,7 +23,7 @@ import chat.rocket.android.db.model.UserEntity
AttachmentFieldEntity::class, AttachmentActionEntity::class, UrlEntity::class,
ReactionEntity::class, MessagesSync::class
],
version = 8,
version = 9,
exportSchema = true
)
abstract class RCDatabase : RoomDatabase() {
......
......@@ -69,6 +69,8 @@ data class AttachmentEntity(
val timestamp: Long? = null,
@ColumnInfo(name = "has_actions")
val hasActions: Boolean = false,
@ColumnInfo(name = "has_fields")
val hasFields: Boolean = false,
@ColumnInfo(name = "button_alignment")
val buttonAlignment: String? = null
) : BaseMessageEntity
......@@ -119,7 +121,7 @@ fun Attachment.asEntity(msgId: String): List<BaseMessageEntity> {
is VideoAttachment -> listOf(asEntity(msgId))
is AudioAttachment -> listOf(asEntity(msgId))
is AuthorAttachment -> asEntity(msgId)
is ColorAttachment -> listOf(asEntity(msgId))
is ColorAttachment -> asEntity(msgId)
is MessageAttachment -> listOf(asEntity(msgId))
is GenericFileAttachment -> listOf(asEntity(msgId))
is ActionsAttachment -> asEntity(msgId)
......@@ -179,7 +181,8 @@ fun AuthorAttachment.asEntity(msgId: String): List<BaseMessageEntity> {
messageId = msgId,
authorLink = url,
authorIcon = authorIcon,
authorName = authorName
authorName = authorName,
hasFields = fields?.isNotEmpty() == true
)
list.add(attachment)
......@@ -195,13 +198,28 @@ fun AuthorAttachment.asEntity(msgId: String): List<BaseMessageEntity> {
return list
}
fun ColorAttachment.asEntity(msgId: String): AttachmentEntity =
AttachmentEntity(
fun ColorAttachment.asEntity(msgId: String): List<BaseMessageEntity> {
val list = mutableListOf<BaseMessageEntity>()
val attachment = AttachmentEntity(
_id = "${msgId}_${hashCode()}",
messageId = msgId,
color = color.rawColor,
fallback = fallback
fallback = fallback,
hasFields = fields?.isNotEmpty() == true
)
list.add(attachment)
fields?.forEach { field ->
val entity = AttachmentFieldEntity(
attachmentId = attachment._id,
title = field.title,
value = field.value
)
list.add(entity)
}
return list
}
// TODO - how to model An message attachment with attachments???
fun MessageAttachment.asEntity(msgId: String): AttachmentEntity =
......
......@@ -16,6 +16,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_STR
import chat.rocket.core.model.attachment.Field
import chat.rocket.core.model.attachment.GenericFileAttachment
import chat.rocket.core.model.attachment.ImageAttachment
......@@ -61,7 +62,7 @@ class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
}
val urls = this.urls?.let { mapUrl(it) }
val reactions = this.reactions?.let { mapReactions(it) }
val attachments = this.attachments?.let { mapAttachments(it) }
val attachments = this.attachments?.let { mapAttachments(it).asReversed() }
val messageType = messageTypeOf(this.message.type)
list.add(Message(
......@@ -164,6 +165,9 @@ class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
authorLink != null -> {
mapAuthorAttachment(this)
}
hasFields -> {
mapColorAttachmentWithFields(this)
}
hasActions -> {
mapActionAttachment(this)
}
......@@ -174,6 +178,19 @@ class DatabaseMessageMapper(private val dbManager: DatabaseManager) {
return list
}
private suspend fun mapColorAttachmentWithFields(entity: AttachmentEntity): ColorAttachment {
val fields = withContext(CommonPool) {
dbManager.messageDao().getAttachmentFields(entity._id)
}.map { Field(it.title, it.value) }
return with(entity) {
ColorAttachment(
color = Color.Custom(color ?: DEFAULT_COLOR_STR),
text = text ?: "",
fallback = fallback,
fields = fields)
}
}
private suspend fun mapActionAttachment(attachment: AttachmentEntity): ActionsAttachment {
val actions = withContext(CommonPool) {
dbManager.messageDao().getAttachmentActions(attachment._id)
......
......@@ -13,12 +13,13 @@
android:paddingEnd="@dimen/screen_edge_left_and_right_padding"
android:paddingBottom="@dimen/message_item_top_and_bottom_padding">
<View
<ImageView
android:id="@+id/quote_bar"
android:layout_width="4dp"
android:layout_height="0dp"
android:layout_marginStart="56dp"
android:background="@drawable/quote_vertical_gray_bar"
android:src="@drawable/quote_vertical_gray_bar"
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="@id/recycler_view_reactions"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
......@@ -35,11 +36,24 @@
app:layout_constraintTop_toTopOf="parent"
tools:text="#5571 - User profile from SSO must not have password change option" />
<TextView
android:id="@+id/text_fields"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
app:layout_constraintTop_toBottomOf="@id/attachment_text"
app:layout_constraintStart_toEndOf="@id/quote_bar"
app:layout_constraintEnd_toEndOf="parent"
tools:visibility="visible"
tools:text="line1\nline2\n\nline3\nline4"/>
<include
layout="@layout/layout_reactions"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/quote_bar"
app:layout_constraintTop_toBottomOf="@id/attachment_text" />
app:layout_constraintTop_toBottomOf="@id/text_fields" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
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