From b332389cb47ed476f2c9f9a32dbf0dcb1cb7c90e Mon Sep 17 00:00:00 2001 From: Floens Date: Sat, 14 Nov 2015 15:28:31 +0100 Subject: [PATCH] Made the post number clickable again Some fixes for cached span clicks --- .../org/floens/chan/ui/cell/PostCell.java | 57 +++++++++++++------ .../org/floens/chan/ui/text/FastTextView.java | 5 +- .../ui/text/FastTextViewMovementMethod.java | 4 +- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/ui/cell/PostCell.java b/Clover/app/src/main/java/org/floens/chan/ui/cell/PostCell.java index f59cbec8..05205637 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/cell/PostCell.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/cell/PostCell.java @@ -42,6 +42,7 @@ import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.UnderlineSpan; import android.util.AttributeSet; +import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; @@ -76,6 +77,7 @@ import static org.floens.chan.utils.AndroidUtils.setRoundItemBackground; import static org.floens.chan.utils.AndroidUtils.sp; public class PostCell extends LinearLayout implements PostCellInterface, PostLinkable.Callback { + private static final String TAG = "PostCell"; private static final int COMMENT_MAX_LENGTH_BOARD = 500; private ThumbnailView thumbnailView; @@ -106,11 +108,7 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin private OnClickListener selfClicked = new OnClickListener() { @Override public void onClick(View v) { -// if (ignoreNextOnClick) { -// ignoreNextOnClick = false; -// } else { callback.onPostClicked(post); -// } } }; @@ -344,11 +342,14 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin SpannableString date = new SpannableString(noText + " " + time); date.setSpan(new ForegroundColorSpan(theme.detailsColor), 0, date.length(), 0); date.setSpan(new AbsoluteSizeSpan(detailsSizePx), 0, date.length(), 0); - titleParts.add(date); - if (ChanSettings.tapNoReply.get()) { + + boolean noClickable = ChanSettings.tapNoReply.get(); + if (noClickable) { date.setSpan(new NoClickableSpan(), 0, noText.length(), 0); } + titleParts.add(date); + if (post.hasImage) { PostImage image = post.image; @@ -404,12 +405,15 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin PostViewMovementMethod movementMethod = new PostViewMovementMethod(); comment.setMovementMethod(movementMethod); comment.setOnClickListener(selfClicked); -// title.setMovementMethod(movementMethod); + + if (noClickable) { + title.setMovementMethod(movementMethod); + } } else { comment.setOnClickListener(null); comment.setClickable(false); comment.setMovementMethod(null); -// title.setMovementMethod(null); + title.setMovementMethod(null); } } @@ -491,7 +495,7 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin */ private class PostViewMovementMethod implements FastTextViewMovementMethod { @Override - public boolean onTouchEvent(@NonNull FastTextView widget, @NonNull Spannable buffer, @NonNull MotionEvent event) { + public boolean onTouchEvent(@NonNull FastTextView widget, @NonNull Spanned buffer, @NonNull Spanned cachedBuffer, @NonNull MotionEvent event) { int action = event.getActionMasked(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_DOWN) { @@ -513,26 +517,45 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(widget); - buffer.removeSpan(BACKGROUND_SPAN); - widget.invalidate(); + removeBackgroundSpan(widget, cachedBuffer); } else if (action == MotionEvent.ACTION_DOWN && link[0] instanceof PostLinkable) { - buffer.setSpan(BACKGROUND_SPAN, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), 0); - widget.invalidate(); + int spanStart = buffer.getSpanStart(link[0]); + int spanEnd = buffer.getSpanEnd(link[0]); + + // Set the buffer on the visible buffer, not on the functional buffer + // This is because the visible buffer may be a cached one or may be in use multiple times + if (spanStart <= spanEnd && spanStart >= 0 && spanEnd >= 0 && spanStart <= cachedBuffer.length() && spanEnd <= cachedBuffer.length()) { + showBackgroundSpan(widget, cachedBuffer, spanStart, spanEnd); + } else { + Log.e(TAG, "Could not add the background span because it was out of range!"); + } } else if (action == MotionEvent.ACTION_CANCEL) { - buffer.removeSpan(BACKGROUND_SPAN); - widget.invalidate(); + removeBackgroundSpan(widget, cachedBuffer); } return true; } else { - buffer.removeSpan(BACKGROUND_SPAN); - widget.invalidate(); + removeBackgroundSpan(widget, cachedBuffer); return false; } } return false; } + + private void showBackgroundSpan(FastTextView widget, Spanned buffer, int start, int end) { + if (buffer instanceof Spannable) { + ((Spannable) buffer).setSpan(BACKGROUND_SPAN, start, end, 0); + widget.invalidate(); + } + } + + private void removeBackgroundSpan(FastTextView widget, Spanned buffer) { + if (buffer instanceof Spannable) { + ((Spannable) buffer).removeSpan(BACKGROUND_SPAN); + widget.invalidate(); + } + } } private class NoClickableSpan extends ClickableSpan { diff --git a/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextView.java b/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextView.java index dea7f1de..8b127f00 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextView.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextView.java @@ -23,6 +23,7 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.text.Layout; import android.text.Spannable; +import android.text.Spanned; import android.text.StaticLayout; import android.text.TextPaint; import android.text.TextUtils; @@ -129,8 +130,8 @@ public class FastTextView extends View { public boolean onTouchEvent(MotionEvent event) { boolean handled = false; - if (movementMethod != null && text instanceof Spannable && layout != null && isEnabled()) { - handled |= movementMethod.onTouchEvent(this, (Spannable) layout.getText(), event); + if (movementMethod != null && text instanceof Spanned && layout != null && isEnabled()) { + handled |= movementMethod.onTouchEvent(this, (Spanned) text, (Spanned) layout.getText(), event); } return handled || super.onTouchEvent(event); diff --git a/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextViewMovementMethod.java b/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextViewMovementMethod.java index 49a648d5..705df8f8 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextViewMovementMethod.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/text/FastTextViewMovementMethod.java @@ -17,9 +17,9 @@ */ package org.floens.chan.ui.text; -import android.text.Spannable; +import android.text.Spanned; import android.view.MotionEvent; public interface FastTextViewMovementMethod { - boolean onTouchEvent(FastTextView widget, Spannable text, MotionEvent event); + boolean onTouchEvent(FastTextView widget, Spanned buffer, Spanned cachedBuffer, MotionEvent event); }