From 71de8fdad3cfeedec0c74ace7b4ca2769338aaf8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 3 Dec 2019 15:05:41 +0100 Subject: [PATCH] Display pills Avatar in the message preview --- .../BottomSheetItemMessagePreview.kt | 2 ++ .../detail/timeline/item/MessageTextItem.kt | 20 ++--------- .../timeline/tools/EventRenderingTools.kt | 34 +++++++++++++++++++ 3 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt diff --git a/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemMessagePreview.kt b/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemMessagePreview.kt index 999068b289..3e3f1d3cf2 100644 --- a/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemMessagePreview.kt +++ b/vector/src/main/java/im/vector/riotx/core/epoxy/bottomsheet/BottomSheetItemMessagePreview.kt @@ -25,6 +25,7 @@ import im.vector.riotx.core.epoxy.VectorEpoxyHolder import im.vector.riotx.core.epoxy.VectorEpoxyModel import im.vector.riotx.core.extensions.setTextOrHide import im.vector.riotx.features.home.AvatarRenderer +import im.vector.riotx.features.home.room.detail.timeline.tools.findPillsAndProcess /** * A message preview for bottom sheet. @@ -49,6 +50,7 @@ abstract class BottomSheetItemMessagePreview : VectorEpoxyModel() { holder.messageView.setOnClickListener(attributes.itemClickListener) holder.messageView.setOnLongClickListener(attributes.itemLongClickListener) if (searchForPills) { - findPillsAndProcess { it.bind(holder.messageView) } + message?.findPillsAndProcess { it.bind(holder.messageView) } } val textFuture = PrecomputedTextCompat.getTextFuture( message ?: "", @@ -85,17 +80,6 @@ abstract class MessageTextItem : AbsMessageItem() { holder.messageView.setTextFuture(textFuture) } - private fun findPillsAndProcess(processBlock: (span: PillImageSpan) -> Unit) { - GlobalScope.launch(Dispatchers.Main) { - val pillImageSpans: Array? = withContext(Dispatchers.IO) { - message?.toSpannable()?.let { spannable -> - spannable.getSpans(0, spannable.length, PillImageSpan::class.java) - } - } - pillImageSpans?.forEach { processBlock(it) } - } - } - override fun getViewType() = STUB_ID class Holder : AbsMessageItem.Holder(STUB_ID) { diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt new file mode 100644 index 0000000000..685799cd32 --- /dev/null +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/tools/EventRenderingTools.kt @@ -0,0 +1,34 @@ +/* + * 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.tools + +import androidx.core.text.toSpannable +import im.vector.riotx.features.html.PillImageSpan +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + +fun CharSequence.findPillsAndProcess(processBlock: (PillImageSpan) -> Unit) { + GlobalScope.launch(Dispatchers.Main) { + withContext(Dispatchers.IO) { + toSpannable().let { spannable -> + spannable.getSpans(0, spannable.length, PillImageSpan::class.java) + } + }.forEach { processBlock(it) } + } +}