From 46139340fed514498eb9fc97c88f61a836dc1d99 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 15 May 2024 06:51:52 +0530 Subject: [PATCH] Convert comment touch listener to a lambda --- .../holder/CommentInfoItemHolder.java | 26 +++++++++++- .../util/text/CommentTextOnTouchListener.java | 42 ------------------- 2 files changed, 24 insertions(+), 44 deletions(-) delete mode 100644 app/src/main/java/org/schabi/newpipe/util/text/CommentTextOnTouchListener.java diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java index a3f0384ad..839aa1813 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentInfoItemHolder.java @@ -1,9 +1,13 @@ package org.schabi.newpipe.info_list.holder; import static org.schabi.newpipe.util.ServiceHelper.getServiceById; +import static org.schabi.newpipe.util.text.TouchUtils.getOffsetForHorizontalLine; +import android.text.Spanned; import android.text.method.LinkMovementMethod; +import android.text.style.ClickableSpan; import android.text.style.URLSpan; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; @@ -25,7 +29,6 @@ import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.external_communication.ShareUtils; import org.schabi.newpipe.util.image.ImageStrategy; import org.schabi.newpipe.util.image.PicassoHelper; -import org.schabi.newpipe.util.text.CommentTextOnTouchListener; import org.schabi.newpipe.util.text.TextEllipsizer; public class CommentInfoItemHolder extends InfoItemHolder { @@ -128,7 +131,26 @@ public class CommentInfoItemHolder extends InfoItemHolder { textEllipsizer.ellipsize(); //noinspection ClickableViewAccessibility - itemContentView.setOnTouchListener(CommentTextOnTouchListener.INSTANCE); + itemContentView.setOnTouchListener((v, event) -> { + final CharSequence text = itemContentView.getText(); + if (text instanceof Spanned buffer) { + final int action = event.getAction(); + + if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { + final int offset = getOffsetForHorizontalLine(itemContentView, event); + final var links = buffer.getSpans(offset, offset, ClickableSpan.class); + + if (links.length != 0) { + if (action == MotionEvent.ACTION_UP) { + links[0].onClick(itemContentView); + } + // we handle events that intersect links, so return true + return true; + } + } + } + return false; + }); itemView.setOnClickListener(view -> { textEllipsizer.toggle(); diff --git a/app/src/main/java/org/schabi/newpipe/util/text/CommentTextOnTouchListener.java b/app/src/main/java/org/schabi/newpipe/util/text/CommentTextOnTouchListener.java deleted file mode 100644 index 5018a6120..000000000 --- a/app/src/main/java/org/schabi/newpipe/util/text/CommentTextOnTouchListener.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.schabi.newpipe.util.text; - -import static org.schabi.newpipe.util.text.TouchUtils.getOffsetForHorizontalLine; - -import android.annotation.SuppressLint; -import android.text.Spanned; -import android.text.style.ClickableSpan; -import android.view.MotionEvent; -import android.view.View; -import android.widget.TextView; - -public class CommentTextOnTouchListener implements View.OnTouchListener { - public static final CommentTextOnTouchListener INSTANCE = new CommentTextOnTouchListener(); - - @SuppressLint("ClickableViewAccessibility") - @Override - public boolean onTouch(final View v, final MotionEvent event) { - if (!(v instanceof TextView)) { - return false; - } - final TextView widget = (TextView) v; - final CharSequence text = widget.getText(); - if (text instanceof Spanned) { - final Spanned buffer = (Spanned) text; - final int action = event.getAction(); - - if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { - final int offset = getOffsetForHorizontalLine(widget, event); - final ClickableSpan[] links = buffer.getSpans(offset, offset, ClickableSpan.class); - - if (links.length != 0) { - if (action == MotionEvent.ACTION_UP) { - links[0].onClick(widget); - } - // we handle events that intersect links, so return true - return true; - } - } - } - return false; - } -}