Commit 0d819ab8 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Adapt MessageViewModel class to accept quotes/replies

parent 2f850b62
...@@ -8,40 +8,64 @@ import android.text.SpannableString ...@@ -8,40 +8,64 @@ import android.text.SpannableString
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
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.SITE_URL import chat.rocket.android.server.domain.SITE_URL
import chat.rocket.android.server.domain.USE_REALNAME import chat.rocket.android.server.domain.USE_REALNAME
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.Url
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.AudioAttachment
import chat.rocket.core.model.attachment.FileAttachment import chat.rocket.core.model.attachment.FileAttachment
import chat.rocket.core.model.attachment.ImageAttachment import chat.rocket.core.model.attachment.ImageAttachment
import chat.rocket.core.model.attachment.VideoAttachment import chat.rocket.core.model.attachment.VideoAttachment
import okhttp3.HttpUrl import okhttp3.HttpUrl
import timber.log.Timber
data class MessageViewModel(val context: Context, data class MessageViewModel(val context: Context,
private val token: Token?, private val token: Token?,
private val message: Message, private val message: Message,
private val settings: Map<String, Value<Any>>?) { private val settings: Map<String, Value<Any>>?,
private val parser: MessageParser,
private val messagesRepository: MessagesRepository) {
val id: String = message.id val id: String = message.id
val time: CharSequence val time: CharSequence
val sender: CharSequence val sender: CharSequence
val content: CharSequence val content: CharSequence
var quote: Message? = null
var urlsWithMeta = arrayListOf<Url>()
var attachmentUrl: String? = null var attachmentUrl: String? = null
var attachmentTitle: CharSequence? = null var attachmentTitle: CharSequence? = null
var attachmentType: AttachmentType? = null var attachmentType: AttachmentType? = null
init { init {
sender = getSenderName() sender = getSenderName()
content = getContent(context)
time = getTime() time = getTime()
val baseUrl = settings?.get(SITE_URL)
message.urls?.let {
// println(message.urls)
if (it.isEmpty()) return@let
for (url in it) {
if (url.meta != null) {
urlsWithMeta.add(url)
}
baseUrl?.let {
val quoteUrl = HttpUrl.parse(url.url)
val serverUrl = HttpUrl.parse(baseUrl.value.toString())
if (quoteUrl != null && serverUrl != null) {
makeQuote(quoteUrl, serverUrl)
}
}
}
}
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 val attachment = it[0] as FileAttachment
val baseUrl = settings?.get(SITE_URL)
baseUrl?.let { baseUrl?.let {
attachmentUrl = attachmentUrl("${baseUrl.value}${attachment.url}") attachmentUrl = attachmentUrl("${baseUrl.value}${attachment.url}")
attachmentTitle = attachment.title attachmentTitle = attachment.title
...@@ -54,6 +78,18 @@ data class MessageViewModel(val context: Context, ...@@ -54,6 +78,18 @@ data class MessageViewModel(val context: Context,
} }
} }
} }
content = getContent(context)
}
private fun makeQuote(quoteUrl: HttpUrl, serverUrl: HttpUrl) {
if (quoteUrl.host() == serverUrl.host()) {
val msgIdToQuote = quoteUrl.queryParameter("msg")
Timber.d("Will quote message Id: $msgIdToQuote")
if (msgIdToQuote != null) {
quote = messagesRepository.getById(msgIdToQuote)
}
}
} }
fun getAvatarUrl(serverUrl: String): String? { fun getAvatarUrl(serverUrl: String): String? {
...@@ -62,9 +98,11 @@ data class MessageViewModel(val context: Context, ...@@ -62,9 +98,11 @@ data class MessageViewModel(val context: Context,
} }
} }
fun getTime() = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(message.timestamp)) fun getOriginalMessage() = message.message
private fun getTime() = DateTimeHelper.getTime(DateTimeHelper.getLocalDateTime(message.timestamp))
fun getSenderName(): CharSequence { private fun getSenderName(): CharSequence {
val useRealName = settings?.get(USE_REALNAME)?.value as Boolean val useRealName = settings?.get(USE_REALNAME)?.value as Boolean
val username = message.sender?.username val username = message.sender?.username
val realName = message.sender?.name val realName = message.sender?.name
...@@ -72,7 +110,7 @@ data class MessageViewModel(val context: Context, ...@@ -72,7 +110,7 @@ data class MessageViewModel(val context: Context,
return senderName ?: username.toString() return senderName ?: username.toString()
} }
fun getContent(context: Context): CharSequence { private fun getContent(context: Context): CharSequence {
val contentMessage: CharSequence val contentMessage: CharSequence
when (message.type) { when (message.type) {
//TODO: Add implementation for Welcome type. //TODO: Add implementation for Welcome type.
...@@ -90,7 +128,14 @@ data class MessageViewModel(val context: Context, ...@@ -90,7 +128,14 @@ data class MessageViewModel(val context: Context,
return contentMessage return contentMessage
} }
private fun getNormalMessage() = message.message private fun getNormalMessage(): CharSequence {
var quoteViewModel: MessageViewModel? = null
if (quote != null) {
val quoteMessage: Message = quote!!
quoteViewModel = MessageViewModel(context, token, quoteMessage, settings, parser, messagesRepository)
}
return parser.renderMarkdown(message.message, quoteViewModel, urlsWithMeta)
}
private fun getSystemMessage(content: String): CharSequence { private fun getSystemMessage(content: String): CharSequence {
val spannableMsg = SpannableString(content) val spannableMsg = SpannableString(content)
......
package chat.rocket.android.chatroom.viewmodel package chat.rocket.android.chatroom.viewmodel
import android.content.Context import android.content.Context
import chat.rocket.android.helper.MessageParser
import chat.rocket.android.server.domain.MessagesRepository
import chat.rocket.core.TokenRepository import chat.rocket.core.TokenRepository
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
import chat.rocket.core.model.Value import chat.rocket.core.model.Value
import javax.inject.Inject import javax.inject.Inject
class MessageViewModelMapper @Inject constructor(private val context: Context, private val tokenRepository: TokenRepository) { class MessageViewModelMapper @Inject constructor(private val context: Context,
private val tokenRepository: TokenRepository,
private val messageParser: MessageParser,
private val messagesRepository: MessagesRepository) {
suspend fun mapToViewModel(message: Message, settings: Map<String, Value<Any>>?) = MessageViewModel(context, tokenRepository.get(), message, settings) suspend fun mapToViewModel(message: Message, settings: Map<String, Value<Any>>?) =
MessageViewModel(context, tokenRepository.get(), message, settings, messageParser, messagesRepository)
suspend fun mapToViewModelList(messageList: List<Message>, settings: Map<String, Value<Any>>?): List<MessageViewModel> { suspend fun mapToViewModelList(messageList: List<Message>, settings: Map<String, Value<Any>>?): List<MessageViewModel> {
return messageList.map { MessageViewModel(context, tokenRepository.get(), it, settings) } return messageList.map { MessageViewModel(context, tokenRepository.get(), it, settings, messageParser, messagesRepository) }
} }
} }
\ 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