Commit 80923948 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Proper formatting pinned messages

parent 4e21753d
...@@ -5,6 +5,8 @@ import android.content.Context ...@@ -5,6 +5,8 @@ import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.graphics.Typeface import android.graphics.Typeface
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan import android.text.style.StyleSpan
import chat.rocket.android.R import chat.rocket.android.R
...@@ -12,16 +14,12 @@ import chat.rocket.android.helper.MessageParser ...@@ -12,16 +14,12 @@ import chat.rocket.android.helper.MessageParser
import chat.rocket.android.helper.UrlHelper import chat.rocket.android.helper.UrlHelper
import chat.rocket.android.server.domain.MessagesRepository import chat.rocket.android.server.domain.MessagesRepository
import chat.rocket.android.server.domain.SITE_URL import chat.rocket.android.server.domain.SITE_URL
import chat.rocket.android.server.domain.USE_REALNAME
import chat.rocket.android.server.domain.useRealName import chat.rocket.android.server.domain.useRealName
import chat.rocket.common.model.Token import chat.rocket.common.model.Token
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
import chat.rocket.core.model.MessageType.* import chat.rocket.core.model.MessageType.*
import chat.rocket.core.model.Value import chat.rocket.core.model.Value
import chat.rocket.core.model.attachment.AudioAttachment import chat.rocket.core.model.attachment.*
import chat.rocket.core.model.attachment.FileAttachment
import chat.rocket.core.model.attachment.ImageAttachment
import chat.rocket.core.model.attachment.VideoAttachment
import chat.rocket.core.model.url.Url import chat.rocket.core.model.url.Url
import okhttp3.HttpUrl import okhttp3.HttpUrl
import timber.log.Timber import timber.log.Timber
...@@ -42,11 +40,15 @@ data class MessageViewModel(val context: Context, ...@@ -42,11 +40,15 @@ data class MessageViewModel(val context: Context,
var attachmentUrl: String? = null var attachmentUrl: String? = null
var attachmentTitle: CharSequence? = null var attachmentTitle: CharSequence? = null
var attachmentType: AttachmentType? = null var attachmentType: AttachmentType? = null
var attachmentMessageText: String? = null
var attachmentMessageAuthor: String? = null
var attachmentMessageIcon: String? = null
var attachmentTimestamp: Long? = null
var systemMessage: Boolean = false var systemMessage: Boolean = false
init { init {
sender = getSenderName() sender = getSenderName()
time = getTime() time = getTime(message.timestamp)
val baseUrl = settings.get(SITE_URL) val baseUrl = settings.get(SITE_URL)
message.urls?.let { message.urls?.let {
...@@ -67,17 +69,27 @@ data class MessageViewModel(val context: Context, ...@@ -67,17 +69,27 @@ data class MessageViewModel(val context: Context,
message.attachments?.let { message.attachments?.let {
if (it.isEmpty() || it[0] == null) return@let if (it.isEmpty() || it[0] == null) return@let
val attachment = it[0] as FileAttachment
baseUrl?.let { if (it[0] is FileAttachment) {
attachmentUrl = attachmentUrl("${baseUrl.value}${attachment.url}") val attachment = it[0] as FileAttachment
attachmentTitle = attachment.title baseUrl?.let {
attachmentUrl = attachmentUrl("${baseUrl.value}${attachment.url}")
attachmentType = when (attachment) { attachmentTitle = attachment.title
is ImageAttachment -> AttachmentType.Image
is VideoAttachment -> AttachmentType.Video attachmentType = when (attachment) {
is AudioAttachment -> AttachmentType.Audio is ImageAttachment -> AttachmentType.Image
else -> null is VideoAttachment -> AttachmentType.Video
is AudioAttachment -> AttachmentType.Audio
else -> null
}
} }
} else if (it[0] is MessageAttachment) {
val attachment = it[0] as MessageAttachment
attachmentType = AttachmentType.Message
attachmentMessageText = attachment.text ?: ""
attachmentMessageAuthor = attachment.author ?: ""
attachmentMessageIcon = attachment.icon
attachmentTimestamp = attachment.timestamp
} }
} }
...@@ -102,7 +114,7 @@ data class MessageViewModel(val context: Context, ...@@ -102,7 +114,7 @@ data class MessageViewModel(val context: Context,
fun getOriginalMessage() = message.message fun getOriginalMessage() = message.message
private fun getTime() = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(message.timestamp)) private fun getTime(timestamp: Long) = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(timestamp))
private fun getSenderName(): CharSequence { private fun getSenderName(): CharSequence {
val username = message.sender?.username val username = message.sender?.username
...@@ -124,6 +136,8 @@ data class MessageViewModel(val context: Context, ...@@ -124,6 +136,8 @@ data class MessageViewModel(val context: Context,
context.getString(R.string.message_room_name_changed, message.message, message.sender?.username)) context.getString(R.string.message_room_name_changed, message.message, message.sender?.username))
is UserRemoved -> contentMessage = getSystemMessage( is UserRemoved -> contentMessage = getSystemMessage(
context.getString(R.string.message_user_removed_by, message.message, message.sender?.username)) context.getString(R.string.message_user_removed_by, message.message, message.sender?.username))
is MessagePinned -> contentMessage = getSystemMessage(
context.getString(R.string.message_pinned))
else -> contentMessage = getNormalMessage() else -> contentMessage = getNormalMessage()
} }
return contentMessage return contentMessage
...@@ -140,28 +154,44 @@ data class MessageViewModel(val context: Context, ...@@ -140,28 +154,44 @@ data class MessageViewModel(val context: Context,
private fun getSystemMessage(content: String): CharSequence { private fun getSystemMessage(content: String): CharSequence {
systemMessage = true systemMessage = true
val spannableMsg = SpannableString(content) val spannableMsg = SpannableStringBuilder(content)
spannableMsg.setSpan(StyleSpan(Typeface.ITALIC), 0, spannableMsg.length, spannableMsg.setSpan(StyleSpan(Typeface.ITALIC), 0, spannableMsg.length,
0) 0)
spannableMsg.setSpan(ForegroundColorSpan(Color.GRAY), 0, spannableMsg.length, spannableMsg.setSpan(ForegroundColorSpan(Color.GRAY), 0, spannableMsg.length,
0) 0)
val username = message.sender?.username if (attachmentType == null) {
val message = message.message val username = message.sender?.username
val message = message.message
val usernameTextStartIndex = if (username != null) content.indexOf(username) else -1 val usernameTextStartIndex = if (username != null) content.indexOf(username) else -1
val usernameTextEndIndex = if (username != null) usernameTextStartIndex + username.length else -1 val usernameTextEndIndex = if (username != null) usernameTextStartIndex + username.length else -1
val messageTextStartIndex = if (message.isNotEmpty()) content.indexOf(message) else -1 val messageTextStartIndex = if (message.isNotEmpty()) content.indexOf(message) else -1
val messageTextEndIndex = messageTextStartIndex + message.length val messageTextEndIndex = messageTextStartIndex + message.length
if (usernameTextStartIndex > -1) { if (usernameTextStartIndex > -1) {
spannableMsg.setSpan(StyleSpan(Typeface.BOLD_ITALIC), usernameTextStartIndex, usernameTextEndIndex, spannableMsg.setSpan(StyleSpan(Typeface.BOLD_ITALIC), usernameTextStartIndex, usernameTextEndIndex,
0) 0)
} }
if (messageTextStartIndex > -1) { if (messageTextStartIndex > -1) {
spannableMsg.setSpan(StyleSpan(Typeface.BOLD_ITALIC), messageTextStartIndex, messageTextEndIndex, spannableMsg.setSpan(StyleSpan(Typeface.BOLD_ITALIC), messageTextStartIndex, messageTextEndIndex,
0) 0)
}
} else {
spannableMsg.apply {
var header = "\n$attachmentMessageAuthor ${getTime(attachmentTimestamp!!)}\n"
append(SpannableString(header).apply {
setSpan(StyleSpan(Typeface.BOLD), 1, attachmentMessageAuthor!!.length + 1, 0)
setSpan(MessageParser.QuoteMarginSpan(context.getDrawable(R.drawable.quote), 10), 1, length, 0)
setSpan(AbsoluteSizeSpan(context.resources.getDimensionPixelSize(R.dimen.message_time_text_size)),
attachmentMessageAuthor!!.length + 1, length, 0)
})
append(SpannableString(parser.renderMarkdown(attachmentMessageText!!)).apply {
setSpan(MessageParser.QuoteMarginSpan(context.getDrawable(R.drawable.quote), 10), 0, length, 0)
})
}
} }
return spannableMsg return spannableMsg
...@@ -185,4 +215,5 @@ sealed class AttachmentType { ...@@ -185,4 +215,5 @@ sealed class AttachmentType {
object Image : AttachmentType() object Image : AttachmentType()
object Video : AttachmentType() object Video : AttachmentType()
object Audio : AttachmentType() object Audio : AttachmentType()
object Message : AttachmentType()
} }
\ No newline at end of file
...@@ -40,13 +40,15 @@ class MessageParser @Inject constructor(val context: Application, private val co ...@@ -40,13 +40,15 @@ class MessageParser @Inject constructor(val context: Application, private val co
* *
* @return A Spannable with the parsed markdown. * @return A Spannable with the parsed markdown.
*/ */
fun renderMarkdown(text: String, quote: MessageViewModel?, urls: List<Url>): CharSequence { fun renderMarkdown(text: String, quote: MessageViewModel? = null, urls: List<Url>? = null): CharSequence {
val builder = SpannableBuilder() val builder = SpannableBuilder()
var content: String = text var content: String = text
// Replace all url links to markdown url syntax. // Replace all url links to markdown url syntax.
for (url in urls) { urls?.let {
content = content.replace(url.url, "[${url.url}](${url.url})") for (url in urls) {
content = content.replace(url.url, "[${url.url}](${url.url})")
}
} }
val parentNode = parser.parse(toLenientMarkdown(content)) val parentNode = parser.parse(toLenientMarkdown(content))
......
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