avoiding duplicate long press events when long pressing urls in messages

- infers a urls has been click by the presence of a selection
This commit is contained in:
Adam Brown 2021-12-02 14:38:38 +00:00
parent 7c4460b812
commit c0c959e460
2 changed files with 24 additions and 2 deletions

View file

@ -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
*/

View file

@ -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<MessageTextItem.Holder>() {
}
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<MessageTextItem.Holder>() {
previewUrlView?.render(state, safeImageContentRenderer)
}
}
companion object {
private const val STUB_ID = R.id.messageContentTextStub
}