mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-16 02:05:06 +08:00
Create chips for incoming messages
This commit is contained in:
parent
fb2401d0b1
commit
07a59e63a6
@ -61,6 +61,7 @@ import im.vector.app.features.home.room.detail.timeline.item.RedactedMessageItem
|
||||
import im.vector.app.features.home.room.detail.timeline.item.RedactedMessageItem_
|
||||
import im.vector.app.features.home.room.detail.timeline.item.VerificationRequestItem
|
||||
import im.vector.app.features.home.room.detail.timeline.item.VerificationRequestItem_
|
||||
import im.vector.app.features.home.room.detail.timeline.render.EventTextRenderer
|
||||
import im.vector.app.features.home.room.detail.timeline.tools.createLinkMovementMethod
|
||||
import im.vector.app.features.home.room.detail.timeline.tools.linkify
|
||||
import im.vector.app.features.html.EventHtmlRenderer
|
||||
@ -112,6 +113,7 @@ class MessageItemFactory @Inject constructor(
|
||||
private val timelineMediaSizeProvider: TimelineMediaSizeProvider,
|
||||
private val htmlRenderer: Lazy<EventHtmlRenderer>,
|
||||
private val htmlCompressor: VectorHtmlCompressor,
|
||||
private val textRendererFactory: EventTextRenderer.Factory,
|
||||
private val stringProvider: StringProvider,
|
||||
private val imageContentRenderer: ImageContentRenderer,
|
||||
private val messageInformationDataFactory: MessageInformationDataFactory,
|
||||
@ -138,6 +140,10 @@ class MessageItemFactory @Inject constructor(
|
||||
pillsPostProcessorFactory.create(roomId)
|
||||
}
|
||||
|
||||
private val textRenderer by lazy {
|
||||
textRendererFactory.create(roomId)
|
||||
}
|
||||
|
||||
fun create(params: TimelineItemFactoryParams): VectorEpoxyModel<*>? {
|
||||
val event = params.event
|
||||
val highlight = params.isHighlighted
|
||||
@ -549,9 +555,9 @@ class MessageItemFactory @Inject constructor(
|
||||
highlight: Boolean,
|
||||
callback: TimelineEventController.Callback?,
|
||||
attributes: AbsMessageItem.Attributes): MessageTextItem? {
|
||||
// TODO process body to add pills for @room texts: create a dedicated renderer with PillsPostProcessor ?
|
||||
val bindingOptions = spanUtils.getBindingOptions(body)
|
||||
val linkifiedBody = body.linkify(callback)
|
||||
val renderedBody = textRenderer.render(body)
|
||||
val bindingOptions = spanUtils.getBindingOptions(renderedBody)
|
||||
val linkifiedBody = renderedBody.linkify(callback)
|
||||
|
||||
return MessageTextItem_()
|
||||
.message(
|
||||
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2022 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.app.features.home.room.detail.timeline.render
|
||||
|
||||
import android.content.Context
|
||||
import android.text.Spannable
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.Spanned
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import im.vector.app.core.di.ActiveSessionHolder
|
||||
import im.vector.app.core.glide.GlideApp
|
||||
import im.vector.app.features.home.AvatarRenderer
|
||||
import im.vector.app.features.html.PillImageSpan
|
||||
import org.matrix.android.sdk.api.session.room.model.RoomSummary
|
||||
import org.matrix.android.sdk.api.util.MatrixItem
|
||||
import timber.log.Timber
|
||||
|
||||
class EventTextRenderer @AssistedInject constructor(@Assisted private val roomId: String?,
|
||||
private val context: Context,
|
||||
private val avatarRenderer: AvatarRenderer,
|
||||
private val sessionHolder: ActiveSessionHolder) {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// PUBLIC API
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory {
|
||||
fun create(roomId: String?): EventTextRenderer
|
||||
}
|
||||
|
||||
/**
|
||||
* @param text the text you want to render
|
||||
*/
|
||||
fun render(text: CharSequence): CharSequence {
|
||||
return if (roomId != null && text.contains(MatrixItem.NOTIFY_EVERYONE)) {
|
||||
SpannableStringBuilder(text).apply {
|
||||
addNotifyEveryoneSpans(this, roomId)
|
||||
}
|
||||
} else {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// HELPER METHODS
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
private fun addNotifyEveryoneSpans(text: Spannable, roomId: String) {
|
||||
val room: RoomSummary? = sessionHolder.getSafeActiveSession()?.getRoomSummary(roomId)
|
||||
val matrixItem = MatrixItem.EveryoneInRoomItem(
|
||||
id = roomId,
|
||||
avatarUrl = room?.avatarUrl,
|
||||
roomDisplayName = room?.displayName
|
||||
)
|
||||
|
||||
// search for notify everyone text
|
||||
var foundIndex = text.indexOf(MatrixItem.NOTIFY_EVERYONE, 0)
|
||||
while (foundIndex >= 0) {
|
||||
val endSpan = foundIndex + MatrixItem.NOTIFY_EVERYONE.length
|
||||
//text.setSpan(ForegroundColorSpan(Color.RED), foundIndex, endSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
addPillSpan(text, createPillImageSpan(matrixItem), foundIndex, endSpan)
|
||||
Timber.e("set span for text $text from index $foundIndex to $endSpan")
|
||||
foundIndex = text.indexOf(MatrixItem.NOTIFY_EVERYONE, endSpan)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createPillImageSpan(matrixItem: MatrixItem) =
|
||||
PillImageSpan(GlideApp.with(context), avatarRenderer, context, matrixItem)
|
||||
|
||||
private fun addPillSpan(
|
||||
renderedText: Spannable,
|
||||
pillSpan: PillImageSpan,
|
||||
startSpan: Int,
|
||||
endSpan: Int
|
||||
) {
|
||||
renderedText.setSpan(pillSpan, startSpan, endSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
}
|
@ -57,7 +57,6 @@ class PillsPostProcessor @AssistedInject constructor(@Assisted private val roomI
|
||||
|
||||
private fun addPillSpans(renderedText: Spannable, roomId: String?) {
|
||||
addLinkSpans(renderedText, roomId)
|
||||
roomId?.let { id -> addRawTextSpans(renderedText, id) }
|
||||
}
|
||||
|
||||
private fun addPillSpan(
|
||||
@ -80,31 +79,6 @@ class PillsPostProcessor @AssistedInject constructor(@Assisted private val roomI
|
||||
}
|
||||
}
|
||||
|
||||
// TODO move this into another PostProcessor when there is no html
|
||||
private fun addRawTextSpans(renderedText: Spannable, roomId: String) {
|
||||
if (renderedText.contains(MatrixItem.NOTIFY_EVERYONE)) {
|
||||
addNotifyEveryoneSpans(renderedText, roomId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun addNotifyEveryoneSpans(renderedText: Spannable, roomId: String) {
|
||||
val room: RoomSummary? = sessionHolder.getSafeActiveSession()?.getRoomSummary(roomId)
|
||||
val matrixItem = MatrixItem.EveryoneInRoomItem(
|
||||
id = roomId,
|
||||
avatarUrl = room?.avatarUrl,
|
||||
roomDisplayName = room?.displayName
|
||||
)
|
||||
val pillSpan = createPillImageSpan(matrixItem)
|
||||
|
||||
// search for notify everyone text
|
||||
var foundIndex = renderedText.indexOf(MatrixItem.NOTIFY_EVERYONE, 0)
|
||||
while (foundIndex >= 0) {
|
||||
val endSpan = foundIndex + MatrixItem.NOTIFY_EVERYONE.length
|
||||
addPillSpan(renderedText, pillSpan, foundIndex, endSpan)
|
||||
foundIndex = renderedText.indexOf(MatrixItem.NOTIFY_EVERYONE, endSpan)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createPillImageSpan(matrixItem: MatrixItem) =
|
||||
PillImageSpan(GlideApp.with(context), avatarRenderer, context, matrixItem)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user