2019-01-18 10:12:08 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* * Copyright 2019 New Vector Ltd
|
|
|
|
* *
|
|
|
|
* * 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
|
|
|
|
* *
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-11-02 16:37:37 +00:00
|
|
|
package im.vector.riotredesign.features.home.room.detail.timeline
|
|
|
|
|
2019-01-07 10:39:26 +00:00
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.text.util.Linkify
|
|
|
|
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
|
2019-01-14 15:18:39 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.TimelineEvent
|
2018-12-18 11:13:46 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.toModel
|
2018-11-02 16:37:37 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.model.MessageContent
|
2019-01-16 09:41:32 +00:00
|
|
|
import im.vector.riotredesign.core.extensions.localDateTime
|
2018-11-02 16:37:37 +00:00
|
|
|
|
|
|
|
class MessageItemFactory(private val timelineDateFormatter: TimelineDateFormatter) {
|
|
|
|
|
|
|
|
private val messagesDisplayedWithInformation = HashSet<String?>()
|
|
|
|
|
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?
|
|
|
|
): MessageItem? {
|
|
|
|
|
2018-12-18 11:13:46 +00:00
|
|
|
val messageContent: MessageContent? = event.root.content.toModel()
|
|
|
|
val roomMember = event.roomMember
|
2018-11-02 16:37:37 +00:00
|
|
|
if (messageContent == null || roomMember == null) {
|
|
|
|
return null
|
|
|
|
}
|
2018-12-18 11:13:46 +00:00
|
|
|
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))
|
|
|
|
?: false
|
|
|
|
|
|
|
|
if (addDaySeparator
|
|
|
|
|| nextRoomMember != roomMember
|
|
|
|
|| nextEvent.root.type != EventType.MESSAGE
|
|
|
|
|| isNextMessageReceivedMoreThanOneHourAgo) {
|
2018-11-02 16:37:37 +00:00
|
|
|
messagesDisplayedWithInformation.add(event.root.eventId)
|
|
|
|
}
|
2019-01-07 10:39:26 +00:00
|
|
|
|
|
|
|
val message = messageContent.body?.let {
|
|
|
|
val spannable = SpannableStringBuilder(it)
|
|
|
|
MatrixLinkify.addLinks(spannable, object : MatrixPermalinkSpan.Callback {
|
|
|
|
override fun onUrlClicked(url: String) {
|
|
|
|
callback?.onUrlClicked(url)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
Linkify.addLinks(spannable, Linkify.ALL)
|
|
|
|
spannable
|
|
|
|
}
|
2018-11-02 16:37:37 +00:00
|
|
|
val showInformation = messagesDisplayedWithInformation.contains(event.root.eventId)
|
|
|
|
return MessageItem(
|
2019-01-07 10:39:26 +00:00
|
|
|
message = message,
|
2018-11-02 16:37:37 +00:00
|
|
|
avatarUrl = roomMember.avatarUrl,
|
|
|
|
showInformation = showInformation,
|
|
|
|
time = timelineDateFormatter.formatMessageHour(date),
|
2019-01-07 10:39:26 +00:00
|
|
|
memberName = roomMember.displayName ?: event.root.sender
|
2018-11-02 16:37:37 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|