Commit 7c2544e3 authored by Lucio Maciel's avatar Lucio Maciel

Use internal functions for getting attachments info

parent 009cade1
...@@ -96,13 +96,9 @@ class ViewModelMapper @Inject constructor(private val context: Context, ...@@ -96,13 +96,9 @@ class ViewModelMapper @Inject constructor(private val context: Context,
} }
private fun mapFileAttachment(message: Message, attachment: FileAttachment): BaseViewModel<*>? { private fun mapFileAttachment(message: Message, attachment: FileAttachment): BaseViewModel<*>? {
val attachmentUrl = if (attachment.url.startsWith("http")) { val attachmentUrl = attachmentUrl(attachment)
attachment.url val attachmentTitle = attachmentTitle(attachment)
} else { val id = attachmentId(message, attachment)
attachmentUrl("$baseUrl${attachment.url}")
}
val attachmentTitle = attachment.attachmentTitle
val id = "${message.id}_${attachment.url}".hashCode().toLong()
return when (attachment) { return when (attachment) {
is ImageAttachment -> ImageAttachmentViewModel(message, attachment, message.id, is ImageAttachment -> ImageAttachmentViewModel(message, attachment, message.id,
attachmentUrl, attachmentTitle, id, getReactions(message)) attachmentUrl, attachmentTitle, id, getReactions(message))
...@@ -114,17 +110,39 @@ class ViewModelMapper @Inject constructor(private val context: Context, ...@@ -114,17 +110,39 @@ class ViewModelMapper @Inject constructor(private val context: Context,
} }
} }
private fun attachmentUrl(url: String): String { private fun attachmentId(message: Message, attachment: FileAttachment): Long {
var response = url return "${message.id}_${attachment.url}".hashCode().toLong()
val httpUrl = HttpUrl.parse(url) }
httpUrl?.let {
response = it.newBuilder().apply { private fun attachmentTitle(attachment: FileAttachment): CharSequence {
addQueryParameter("rc_uid", token?.userId) return with(attachment) {
addQueryParameter("rc_token", token?.authToken) title?.let { return@with it }
}.build().toString()
val fileUrl = HttpUrl.parse(url)
fileUrl?.let {
return@with it.pathSegments().last()
}
return@with ""
} }
}
return response private fun attachmentUrl(attachment: FileAttachment): String {
return with(attachment) {
if (url.startsWith("http")) return@with url
val fullUrl = "$baseUrl$url"
val httpUrl = HttpUrl.parse(fullUrl)
httpUrl?.let {
return@with it.newBuilder().apply {
addQueryParameter("rc_uid", token?.userId)
addQueryParameter("rc_token", token?.authToken)
}.build().toString()
}
// Fallback to baseUrl + url
return@with fullUrl
}
} }
private suspend fun mapMessage(message: Message): MessageViewModel = withContext(CommonPool) { private suspend fun mapMessage(message: Message): MessageViewModel = withContext(CommonPool) {
...@@ -307,16 +325,4 @@ class ViewModelMapper @Inject constructor(private val context: Context, ...@@ -307,16 +325,4 @@ class ViewModelMapper @Inject constructor(private val context: Context,
}) })
} }
} }
} }
\ No newline at end of file
internal val FileAttachment.attachmentTitle: String
get() {
title?.let { return it }
val fileUrl = HttpUrl.parse(url)
fileUrl?.let {
return it.pathSegments().last()
}
return ""
}
\ 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