1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57:29 +00:00

Fix long click in feed view and url click in profile view

This commit is contained in:
Ammar Githam 2020-09-09 00:37:40 +09:00
parent 84ff0a23e1
commit 61851f2ef7
2 changed files with 36 additions and 11 deletions

View File

@ -3,6 +3,7 @@ package awais.instagrabber.customviews;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.graphics.RectF; import android.graphics.RectF;
import android.os.Handler;
import android.text.Layout; import android.text.Layout;
import android.text.Selection; import android.text.Selection;
import android.text.Spannable; import android.text.Spannable;
@ -14,6 +15,7 @@ import android.text.style.ClickableSpan;
import android.text.style.URLSpan; import android.text.style.URLSpan;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -26,11 +28,20 @@ import awais.instagrabber.models.FeedModel;
import awais.instagrabber.utils.Utils; import awais.instagrabber.utils.Utils;
public final class RamboTextView extends AppCompatTextView { public final class RamboTextView extends AppCompatTextView {
private static final String TAG = "RamboTextView";
private static final int highlightBackgroundSpanKey = R.id.tvComment; private static final int highlightBackgroundSpanKey = R.id.tvComment;
private static final RectF touchedLineBounds = new RectF(); private static final RectF touchedLineBounds = new RectF();
private ClickableSpan clickableSpanUnderTouchOnActionDown; private ClickableSpan clickableSpanUnderTouchOnActionDown;
private MentionClickListener mentionClickListener; private MentionClickListener mentionClickListener;
private boolean isUrlHighlighted, isExpandable, isExpanded; private boolean isUrlHighlighted;
private boolean isExpandable;
private boolean isExpanded;
private OnLongClickListener longClickListener;
private final Handler handler = new Handler();
private final Runnable longPressRunnable = () -> {
longClickListener.onLongClick(this);
};
public RamboTextView(final Context context) { public RamboTextView(final Context context) {
super(context); super(context);
@ -56,6 +67,12 @@ public final class RamboTextView extends AppCompatTextView {
this.isExpanded = isExpanded; this.isExpanded = isExpanded;
} }
@Override
public void setOnLongClickListener(@Nullable final OnLongClickListener l) {
if (l == null) return;
this.longClickListener = l;
}
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@Override @Override
public boolean onTouchEvent(final MotionEvent event) { public boolean onTouchEvent(final MotionEvent event) {
@ -71,33 +88,37 @@ public final class RamboTextView extends AppCompatTextView {
final boolean isURLSpan = clickableSpanUnderTouch instanceof URLSpan; final boolean isURLSpan = clickableSpanUnderTouch instanceof URLSpan;
// feed view caption hacks // feed view caption hacks
if (isExpandable && !touchStartedOverAClickableSpan) // if (isExpandable && !touchStartedOverAClickableSpan)
return !isExpanded | super.onTouchEvent(event); // short operator, because we want two shits to work // return !isExpanded | super.onTouchEvent(event); // short operator, because we want two shits to work
final Object tag = getTag(); final Object tag = getTag();
final FeedModel feedModel = tag instanceof FeedModel ? (FeedModel) tag : null; final FeedModel feedModel = tag instanceof FeedModel ? (FeedModel) tag : null;
switch (action) { switch (action) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
final int longPressTimeout = ViewConfiguration.getLongPressTimeout();
handler.postDelayed(longPressRunnable, longPressTimeout);
if (feedModel != null) feedModel.setMentionClicked(false); if (feedModel != null) feedModel.setMentionClicked(false);
if (clickableSpanUnderTouch != null) highlightUrl(clickableSpanUnderTouch, spanText); if (clickableSpanUnderTouch != null) {
return isURLSpan ? super.onTouchEvent(event) : touchStartedOverAClickableSpan; highlightUrl(clickableSpanUnderTouch, spanText);
}
return super.onTouchEvent(event);
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
handler.removeCallbacks(longPressRunnable);
if (touchStartedOverAClickableSpan && clickableSpanUnderTouch == clickableSpanUnderTouchOnActionDown) { if (touchStartedOverAClickableSpan && clickableSpanUnderTouch == clickableSpanUnderTouchOnActionDown) {
dispatchUrlClick(spanText, clickableSpanUnderTouch); dispatchUrlClick(spanText, clickableSpanUnderTouch);
if (feedModel != null) feedModel.setMentionClicked(true); if (feedModel != null) feedModel.setMentionClicked(true);
} }
cleanupOnTouchUp(spanText); cleanupOnTouchUp(spanText);
return isURLSpan ? super.onTouchEvent(event) : touchStartedOverAClickableSpan; return super.onTouchEvent(event);
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
// handler.removeCallbacks(longPressRunnable);
if (feedModel != null) feedModel.setMentionClicked(false); if (feedModel != null) feedModel.setMentionClicked(false);
if (clickableSpanUnderTouch != null) highlightUrl(clickableSpanUnderTouch, spanText); if (clickableSpanUnderTouch != null) highlightUrl(clickableSpanUnderTouch, spanText);
else removeUrlHighlightColor(spanText); else removeUrlHighlightColor(spanText);
return isURLSpan ? super.onTouchEvent(event) : touchStartedOverAClickableSpan; return super.onTouchEvent(event);
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
handler.removeCallbacks(longPressRunnable);
if (feedModel != null) feedModel.setMentionClicked(false); if (feedModel != null) feedModel.setMentionClicked(false);
cleanupOnTouchUp(spanText); cleanupOnTouchUp(spanText);
return super.onTouchEvent(event); return super.onTouchEvent(event);
@ -155,7 +176,9 @@ public final class RamboTextView extends AppCompatTextView {
} }
@Nullable @Nullable
private static ClickableSpan findClickableSpanUnderTouch(@NonNull final TextView textView, final Spannable text, @NonNull final MotionEvent event) { private static ClickableSpan findClickableSpanUnderTouch(@NonNull final TextView textView,
final Spanned text,
@NonNull final MotionEvent event) {
final int touchX = (int) (event.getX() - textView.getTotalPaddingLeft() + textView.getScrollX()); final int touchX = (int) (event.getX() - textView.getTotalPaddingLeft() + textView.getScrollX());
final int touchY = (int) (event.getY() - textView.getTotalPaddingTop() + textView.getScrollY()); final int touchY = (int) (event.getY() - textView.getTotalPaddingTop() + textView.getScrollY());

View File

@ -8,6 +8,7 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.RelativeSizeSpan; import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.util.Log; import android.util.Log;
@ -487,6 +488,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
} else { } else {
binding.mainUrl.setVisibility(View.VISIBLE); binding.mainUrl.setVisibility(View.VISIBLE);
binding.mainUrl.setText(Utils.getSpannableUrl(url)); binding.mainUrl.setText(Utils.getSpannableUrl(url));
binding.mainUrl.setMovementMethod(LinkMovementMethod.getInstance());
} }
binding.mainFullName.setSelected(true); binding.mainFullName.setSelected(true);