From c0c959e460f3e701b26c5190daf90c55f2dd2339 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 2 Dec 2021 14:38:38 +0000 Subject: [PATCH] avoiding duplicate long press events when long pressing urls in messages - infers a urls has been click by the presence of a selection --- .../java/im/vector/app/core/epoxy/Listener.kt | 21 +++++++++++++++++++ .../detail/timeline/item/MessageTextItem.kt | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/epoxy/Listener.kt b/vector/src/main/java/im/vector/app/core/epoxy/Listener.kt index e959d800b8..4d8f7b3a1c 100644 --- a/vector/src/main/java/im/vector/app/core/epoxy/Listener.kt +++ b/vector/src/main/java/im/vector/app/core/epoxy/Listener.kt @@ -17,6 +17,7 @@ package im.vector.app.core.epoxy import android.view.View +import android.widget.TextView import im.vector.app.core.utils.DebouncedClickListener /** @@ -32,6 +33,26 @@ fun View.onClick(listener: ClickListener?) { } } +fun TextView.onLongClickIgnoringLinks(listener: View.OnLongClickListener?) { + if (listener == null) { + setOnLongClickListener(null) + } else { + setOnLongClickListener(object : View.OnLongClickListener { + override fun onLongClick(v: View): Boolean { + if (hasLongPressedLink()) { + return false + } + return listener.onLongClick(v) + } + + /** + * Infer that a Clickable span has been click by the presence of a selection + */ + private fun hasLongPressedLink() = selectionStart != -1 || selectionEnd != -1 + }) + } +} + /** * Simple Text listener lambda */ diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageTextItem.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageTextItem.kt index c6dce5a9ce..747183bce6 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageTextItem.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/item/MessageTextItem.kt @@ -25,6 +25,7 @@ import com.airbnb.epoxy.EpoxyAttribute import com.airbnb.epoxy.EpoxyModelClass import im.vector.app.R import im.vector.app.core.epoxy.onClick +import im.vector.app.core.epoxy.onLongClickIgnoringLinks import im.vector.app.features.home.room.detail.timeline.TimelineEventController import im.vector.app.features.home.room.detail.timeline.tools.findPillsAndProcess import im.vector.app.features.home.room.detail.timeline.url.PreviewUrlRetriever @@ -94,10 +95,9 @@ abstract class MessageTextItem : AbsMessageItem() { } super.bind(holder) holder.messageView.movementMethod = movementMethod - renderSendState(holder.messageView, holder.messageView) holder.messageView.onClick(attributes.itemClickListener) - holder.messageView.setOnLongClickListener(attributes.itemLongClickListener) + holder.messageView.onLongClickIgnoringLinks(attributes.itemLongClickListener) if (canUseTextFuture) { holder.messageView.setTextFuture(textFuture) @@ -133,6 +133,7 @@ abstract class MessageTextItem : AbsMessageItem() { previewUrlView?.render(state, safeImageContentRenderer) } } + companion object { private const val STUB_ID = R.id.messageContentTextStub }