Browse Source

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

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
61851f2ef7
  1. 45
      app/src/main/java/awais/instagrabber/customviews/RamboTextView.java
  2. 2
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java

45
app/src/main/java/awais/instagrabber/customviews/RamboTextView.java

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

2
app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java

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

Loading…
Cancel
Save