2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-05 17:31:03 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.detail.timeline.factory
|
2018-11-02 16:37:37 +00:00
|
|
|
|
2019-03-01 20:44:26 +00:00
|
|
|
import android.text.Spannable
|
2019-01-07 10:39:26 +00:00
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import im.vector.matrix.android.api.permalinks.MatrixLinkify
|
|
|
|
import im.vector.matrix.android.api.permalinks.MatrixPermalinkSpan
|
2019-01-16 09:41:32 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
2018-12-18 11:13:46 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.toModel
|
2019-03-12 07:29:49 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.model.message.*
|
2019-01-25 17:40:02 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
|
2019-02-20 10:39:25 +00:00
|
|
|
import im.vector.riotredesign.R
|
2019-04-05 08:40:59 +00:00
|
|
|
import im.vector.riotredesign.core.epoxy.VectorEpoxyModel
|
2019-01-16 09:41:32 +00:00
|
|
|
import im.vector.riotredesign.core.extensions.localDateTime
|
2019-04-09 15:33:47 +00:00
|
|
|
import im.vector.riotredesign.core.linkify.VectorLinkify
|
2019-02-20 10:39:25 +00:00
|
|
|
import im.vector.riotredesign.core.resources.ColorProvider
|
2019-03-05 17:31:03 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.TimelineEventController
|
2019-03-12 07:29:49 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineDateFormatter
|
2019-01-24 17:04:55 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.helper.TimelineMediaSizeProvider
|
2019-03-12 07:29:49 +00:00
|
|
|
import im.vector.riotredesign.features.home.room.detail.timeline.item.*
|
2019-02-26 18:35:47 +00:00
|
|
|
import im.vector.riotredesign.features.html.EventHtmlRenderer
|
2019-01-23 19:29:47 +00:00
|
|
|
import im.vector.riotredesign.features.media.MediaContentRenderer
|
2019-02-20 10:39:25 +00:00
|
|
|
import me.gujun.android.span.span
|
2018-11-02 16:37:37 +00:00
|
|
|
|
2019-02-20 10:39:25 +00:00
|
|
|
class MessageItemFactory(private val colorProvider: ColorProvider,
|
|
|
|
private val timelineMediaSizeProvider: TimelineMediaSizeProvider,
|
2019-02-21 18:21:08 +00:00
|
|
|
private val timelineDateFormatter: TimelineDateFormatter,
|
2019-02-25 14:18:36 +00:00
|
|
|
private val htmlRenderer: EventHtmlRenderer) {
|
2018-11-02 16:37:37 +00:00
|
|
|
|
2019-01-14 15:18:39 +00:00
|
|
|
fun create(event: TimelineEvent,
|
|
|
|
nextEvent: TimelineEvent?,
|
2018-12-19 10:50:44 +00:00
|
|
|
callback: TimelineEventController.Callback?
|
2019-04-05 08:40:59 +00:00
|
|
|
): VectorEpoxyModel<*>? {
|
2018-12-19 10:50:44 +00:00
|
|
|
|
2018-12-18 11:13:46 +00:00
|
|
|
val roomMember = event.roomMember
|
|
|
|
val nextRoomMember = nextEvent?.roomMember
|
2019-01-16 09:41:32 +00:00
|
|
|
|
|
|
|
val date = event.root.localDateTime()
|
|
|
|
val nextDate = nextEvent?.root?.localDateTime()
|
|
|
|
val addDaySeparator = date.toLocalDate() != nextDate?.toLocalDate()
|
|
|
|
val isNextMessageReceivedMoreThanOneHourAgo = nextDate?.isBefore(date.minusMinutes(60))
|
2019-02-19 14:47:50 +00:00
|
|
|
?: false
|
2019-01-16 09:41:32 +00:00
|
|
|
|
2019-03-21 19:21:45 +00:00
|
|
|
val showInformation = addDaySeparator
|
2019-02-19 14:47:50 +00:00
|
|
|
|| nextRoomMember != roomMember
|
|
|
|
|| nextEvent?.root?.type != EventType.MESSAGE
|
2019-03-21 19:21:45 +00:00
|
|
|
|| isNextMessageReceivedMoreThanOneHourAgo
|
2019-01-07 10:39:26 +00:00
|
|
|
|
2019-01-21 17:16:15 +00:00
|
|
|
val messageContent: MessageContent = event.root.content.toModel() ?: return null
|
|
|
|
val time = timelineDateFormatter.formatMessageHour(date)
|
|
|
|
val avatarUrl = roomMember?.avatarUrl
|
|
|
|
val memberName = roomMember?.displayName ?: event.root.sender
|
2019-01-22 17:43:15 +00:00
|
|
|
val informationData = MessageInformationData(time, avatarUrl, memberName, showInformation)
|
2019-01-21 17:16:15 +00:00
|
|
|
|
|
|
|
return when (messageContent) {
|
2019-04-09 15:33:47 +00:00
|
|
|
is MessageTextContent -> buildTextMessageItem(messageContent, informationData, callback)
|
|
|
|
is MessageImageContent -> buildImageMessageItem(messageContent, informationData, callback)
|
|
|
|
is MessageEmoteContent -> buildEmoteMessageItem(messageContent, informationData, callback)
|
2019-02-20 10:39:25 +00:00
|
|
|
is MessageNoticeContent -> buildNoticeMessageItem(messageContent, informationData, callback)
|
2019-04-09 15:33:47 +00:00
|
|
|
else -> buildNotHandledMessageItem(messageContent)
|
2019-01-21 17:16:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:47:50 +00:00
|
|
|
private fun buildNotHandledMessageItem(messageContent: MessageContent): DefaultItem? {
|
2019-02-11 12:47:47 +00:00
|
|
|
val text = "${messageContent.type} message events are not yet handled"
|
2019-02-19 14:47:50 +00:00
|
|
|
return DefaultItem_().text(text)
|
2019-02-11 12:47:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 19:29:47 +00:00
|
|
|
private fun buildImageMessageItem(messageContent: MessageImageContent,
|
2019-03-12 07:29:49 +00:00
|
|
|
informationData: MessageInformationData,
|
|
|
|
callback: TimelineEventController.Callback?): MessageImageItem? {
|
2019-01-24 17:04:55 +00:00
|
|
|
|
|
|
|
val (maxWidth, maxHeight) = timelineMediaSizeProvider.getMaxSize()
|
2019-01-23 19:29:47 +00:00
|
|
|
val data = MediaContentRenderer.Data(
|
2019-03-12 07:29:49 +00:00
|
|
|
messageContent.body,
|
2019-01-23 19:29:47 +00:00
|
|
|
url = messageContent.url,
|
2019-01-25 17:40:02 +00:00
|
|
|
height = messageContent.info?.height,
|
2019-01-24 17:04:55 +00:00
|
|
|
maxHeight = maxHeight,
|
2019-01-25 17:40:02 +00:00
|
|
|
width = messageContent.info?.width,
|
2019-01-24 17:04:55 +00:00
|
|
|
maxWidth = maxWidth,
|
2019-01-25 17:40:02 +00:00
|
|
|
rotation = messageContent.info?.rotation,
|
|
|
|
orientation = messageContent.info?.orientation
|
2019-01-23 19:29:47 +00:00
|
|
|
)
|
2019-02-19 10:57:17 +00:00
|
|
|
return MessageImageItem_()
|
|
|
|
.informationData(informationData)
|
|
|
|
.mediaData(data)
|
2019-03-12 15:54:33 +00:00
|
|
|
.clickListener { view -> callback?.onMediaClicked(data, view) }
|
2019-01-22 17:43:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 17:16:15 +00:00
|
|
|
private fun buildTextMessageItem(messageContent: MessageTextContent,
|
2019-01-22 17:43:15 +00:00
|
|
|
informationData: MessageInformationData,
|
2019-01-21 17:16:15 +00:00
|
|
|
callback: TimelineEventController.Callback?): MessageTextItem? {
|
|
|
|
|
2019-02-21 18:21:08 +00:00
|
|
|
val bodyToUse = messageContent.formattedBody
|
|
|
|
?.let {
|
|
|
|
htmlRenderer.render(it)
|
|
|
|
}
|
|
|
|
?: messageContent.body
|
|
|
|
|
|
|
|
val linkifiedBody = linkifyBody(bodyToUse, callback)
|
2019-02-19 18:12:58 +00:00
|
|
|
return MessageTextItem_()
|
2019-02-21 18:21:08 +00:00
|
|
|
.message(linkifiedBody)
|
2019-02-19 18:12:58 +00:00
|
|
|
.informationData(informationData)
|
|
|
|
}
|
|
|
|
|
2019-02-20 10:39:25 +00:00
|
|
|
private fun buildNoticeMessageItem(messageContent: MessageNoticeContent,
|
|
|
|
informationData: MessageInformationData,
|
|
|
|
callback: TimelineEventController.Callback?): MessageTextItem? {
|
|
|
|
|
|
|
|
val message = messageContent.body.let {
|
|
|
|
val formattedBody = span {
|
|
|
|
text = it
|
|
|
|
textColor = colorProvider.getColor(R.color.slate_grey)
|
|
|
|
textStyle = "italic"
|
|
|
|
}
|
|
|
|
linkifyBody(formattedBody, callback)
|
|
|
|
}
|
|
|
|
return MessageTextItem_()
|
|
|
|
.message(message)
|
|
|
|
.informationData(informationData)
|
|
|
|
}
|
|
|
|
|
2019-02-19 18:12:58 +00:00
|
|
|
private fun buildEmoteMessageItem(messageContent: MessageEmoteContent,
|
|
|
|
informationData: MessageInformationData,
|
|
|
|
callback: TimelineEventController.Callback?): MessageTextItem? {
|
|
|
|
|
2019-01-22 17:43:15 +00:00
|
|
|
val message = messageContent.body.let {
|
2019-02-19 18:12:58 +00:00
|
|
|
val formattedBody = "* ${informationData.memberName} $it"
|
|
|
|
linkifyBody(formattedBody, callback)
|
2019-01-07 10:39:26 +00:00
|
|
|
}
|
2019-02-19 10:57:17 +00:00
|
|
|
return MessageTextItem_()
|
|
|
|
.message(message)
|
|
|
|
.informationData(informationData)
|
2018-11-02 16:37:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 20:44:26 +00:00
|
|
|
private fun linkifyBody(body: CharSequence, callback: TimelineEventController.Callback?): Spannable {
|
2019-02-19 18:12:58 +00:00
|
|
|
val spannable = SpannableStringBuilder(body)
|
|
|
|
MatrixLinkify.addLinks(spannable, object : MatrixPermalinkSpan.Callback {
|
|
|
|
override fun onUrlClicked(url: String) {
|
|
|
|
callback?.onUrlClicked(url)
|
|
|
|
}
|
|
|
|
})
|
2019-04-09 15:33:47 +00:00
|
|
|
VectorLinkify.addLinks(spannable, true)
|
2019-02-19 18:12:58 +00:00
|
|
|
return spannable
|
|
|
|
}
|
2018-11-02 16:37:37 +00:00
|
|
|
|
|
|
|
}
|