Fix / Verification Msg show fallback text in room summary

This commit is contained in:
Valere 2019-12-13 17:55:37 +01:00
parent 3953022258
commit ff5305ee66
3 changed files with 112 additions and 28 deletions

View File

@ -0,0 +1,104 @@
/*
* 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.
*/
package im.vector.riotx.features.home.room.detail.timeline.format
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.model.message.MessageType
import im.vector.matrix.android.api.session.room.model.message.isReply
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
import im.vector.matrix.android.api.session.room.timeline.getLastMessageContent
import im.vector.matrix.android.api.session.room.timeline.getTextEditableContent
import im.vector.riotx.R
import im.vector.riotx.core.resources.ColorProvider
import im.vector.riotx.core.resources.StringProvider
import me.gujun.android.span.span
import javax.inject.Inject
class DisplayableEventFormatter @Inject constructor(
// private val sessionHolder: ActiveSessionHolder,
private val stringProvider: StringProvider,
private val colorProvider: ColorProvider,
private val noticeEventFormatter: NoticeEventFormatter
) {
fun format(timelineEvent: TimelineEvent, appendAuthor: Boolean): CharSequence {
if (timelineEvent.root.isEncrypted()
&& timelineEvent.root.mxDecryptionResult == null) {
return stringProvider.getString(R.string.encrypted_message)
}
val senderName = timelineEvent.getDisambiguatedDisplayName()
when (timelineEvent.root.getClearType()) {
EventType.MESSAGE -> {
timelineEvent.getLastMessageContent()?.let { messageContent ->
when (messageContent.type) {
MessageType.MSGTYPE_VERIFICATION_REQUEST -> {
return simpleFormat(senderName, stringProvider.getString(R.string.verification_request), appendAuthor)
}
MessageType.MSGTYPE_IMAGE -> {
return simpleFormat(senderName, stringProvider.getString(R.string.sent_an_image), appendAuthor)
}
MessageType.MSGTYPE_AUDIO -> {
return simpleFormat(senderName, stringProvider.getString(R.string.sent_an_audio_file), appendAuthor)
}
MessageType.MSGTYPE_VIDEO -> {
return simpleFormat(senderName, stringProvider.getString(R.string.sent_a_video), appendAuthor)
}
MessageType.MSGTYPE_FILE -> {
return simpleFormat(senderName, stringProvider.getString(R.string.sent_a_file), appendAuthor)
}
MessageType.MSGTYPE_TEXT -> {
if (messageContent.isReply()) {
// Skip reply prefix, and show important
// TODO add a reply image span ?
return simpleFormat(senderName, timelineEvent.getTextEditableContent()
?: messageContent.body, appendAuthor)
} else {
return simpleFormat(senderName, messageContent.body, appendAuthor)
}
}
else -> {
return simpleFormat(senderName, messageContent.body, appendAuthor)
}
}
}
}
else -> {
return span {
text = noticeEventFormatter.format(timelineEvent) ?: ""
textStyle = "italic"
}
}
}
return span { }
}
private fun simpleFormat(senderName: String, body: CharSequence, appendAuthor: Boolean): CharSequence {
return if (appendAuthor) {
span {
text = senderName
textColor = colorProvider.getColorFromAttribute(R.attr.riotx_text_primary)
}
.append(": ")
.append(body)
} else {
body
}
}
}

View File

@ -17,10 +17,8 @@
package im.vector.riotx.features.home.room.list
import android.view.View
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.room.model.Membership
import im.vector.matrix.android.api.session.room.model.RoomSummary
import im.vector.matrix.android.api.session.room.timeline.getLastMessageContent
import im.vector.riotx.R
import im.vector.riotx.core.date.VectorDateFormatter
import im.vector.riotx.core.epoxy.VectorEpoxyModel
@ -30,11 +28,10 @@ import im.vector.riotx.core.resources.DateProvider
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.core.utils.DebouncedClickListener
import im.vector.riotx.features.home.AvatarRenderer
import im.vector.riotx.features.home.room.detail.timeline.format.NoticeEventFormatter
import me.gujun.android.span.span
import im.vector.riotx.features.home.room.detail.timeline.format.DisplayableEventFormatter
import javax.inject.Inject
class RoomSummaryItemFactory @Inject constructor(private val noticeEventFormatter: NoticeEventFormatter,
class RoomSummaryItemFactory @Inject constructor(private val displayableEventFormatter: DisplayableEventFormatter,
private val dateFormatter: VectorDateFormatter,
private val colorProvider: ColorProvider,
private val stringProvider: StringProvider,
@ -93,29 +90,7 @@ class RoomSummaryItemFactory @Inject constructor(private val noticeEventFormatte
val date = latestEvent.root.localDateTime()
val currentDate = DateProvider.currentLocalDateTime()
val isSameDay = date.toLocalDate() == currentDate.toLocalDate()
latestFormattedEvent = if (latestEvent.root.isEncrypted()
&& latestEvent.root.mxDecryptionResult == null) {
stringProvider.getString(R.string.encrypted_message)
} else if (latestEvent.root.getClearType() == EventType.MESSAGE || latestEvent.root.getClearType() == EventType.STICKER) {
val senderName = latestEvent.getDisambiguatedDisplayName()
val content = latestEvent.getLastMessageContent()
val message = content?.body ?: ""
if (roomSummary.isDirect.not()) {
span {
text = senderName
textColor = colorProvider.getColorFromAttribute(R.attr.riotx_text_primary)
}
.append(" - ")
.append(message)
} else {
message
}
} else {
span {
text = noticeEventFormatter.format(latestEvent) ?: ""
textStyle = "italic"
}
}
latestFormattedEvent = displayableEventFormatter.format(latestEvent, roomSummary.isDirect.not())
latestEventTime = if (isSameDay) {
dateFormatter.formatMessageHour(date)
} else {

View File

@ -153,4 +153,9 @@
<string name="verification_conclusion_warning">Untrusted sign in</string>
<string name="sent_a_video">Video.</string>
<string name="sent_an_image">Image.</string>
<string name="sent_an_audio_file">Audio</string>
<string name="sent_a_file">File</string>
</resources>