From 26f8250c0b2788866f751ca1cf40ee8c766573cf Mon Sep 17 00:00:00 2001 From: Floens Date: Sun, 10 Apr 2016 22:38:50 +0200 Subject: [PATCH] Remove postlinkable callback trickery and handle it in the movementmethod Should've done this from the start... --- .../floens/chan/core/model/PostLinkable.java | 70 ++----------------- .../org/floens/chan/ui/cell/PostCell.java | 40 ++++------- 2 files changed, 19 insertions(+), 91 deletions(-) diff --git a/Clover/app/src/main/java/org/floens/chan/core/model/PostLinkable.java b/Clover/app/src/main/java/org/floens/chan/core/model/PostLinkable.java index cfffa090..ad91e966 100644 --- a/Clover/app/src/main/java/org/floens/chan/core/model/PostLinkable.java +++ b/Clover/app/src/main/java/org/floens/chan/core/model/PostLinkable.java @@ -25,12 +25,8 @@ import android.view.View; import org.floens.chan.ui.cell.PostCell; import org.floens.chan.ui.theme.Theme; -import java.util.ArrayList; -import java.util.List; - /** * A Clickable span that handles post clicks. These are created in ChanParser for post quotes, spoilers etc.
- * PostCells bind callbacks with addCallback and call removeCallback when done.
* PostCell has a {@link PostCell.PostViewMovementMethod}, that searches spans at the location the TextView was tapped, * and handled if it was a PostLinkable. */ @@ -45,11 +41,8 @@ public class PostLinkable extends ClickableSpan { public final Object value; public final Type type; - private List callbacks = new ArrayList<>(); private boolean spoilerVisible = false; - -// private static boolean testingCallbacks = false; -// private static HashMap> callbacksTest = new HashMap<>(); + private int markedNo = -1; public PostLinkable(Theme theme, Post post, String key, Object value, Type type) { this.theme = theme; @@ -57,63 +50,22 @@ public class PostLinkable extends ClickableSpan { this.key = key; this.value = value; this.type = type; - - /*if (!testingCallbacks) { - testingCallbacks = true; - - AndroidUtils.runOnUiThread(testCallbacksRunnable, 1000); - }*/ - } - - /*private static Runnable testCallbacksRunnable = new Runnable() { - @Override - public void run() { - AndroidUtils.runOnUiThread(testCallbacksRunnable, 1000); - - Logger.test("Callbacks:"); - for (Map.Entry> entry : callbacksTest.entrySet()) { - if (entry.getValue().size() > 0) { - Logger.test(entry.getKey().key + " still has " + entry.getValue().size() + " bounded callbacks"); - } - } - } - };*/ - - public void addCallback(Callback callback) { - callbacks.add(callback); - - /*if (!callbacksTest.containsKey(this)) { - callbacksTest.put(this, new ArrayList()); - } - - callbacksTest.get(this).add(callback);*/ - } - - public void removeCallback(Callback callback) { - callbacks.remove(callback); - - /*callbacksTest.get(this).remove(callback);*/ - } - - public boolean hasCallback(Callback callback) { - return callbacks.contains(callback); } @Override public void onClick(View widget) { - Callback top = topCallback(); - if (top != null) { - top.onLinkableClick(this); - } spoilerVisible = !spoilerVisible; } + public void setMarkedNo(int markedNo) { + this.markedNo = markedNo; + } + @Override public void updateDrawState(@NonNull TextPaint ds) { if (type == Type.QUOTE || type == Type.LINK || type == Type.THREAD) { if (type == Type.QUOTE) { - Callback top = topCallback(); - if (value instanceof Integer && top != null && (Integer) value == top.getMarkedNo(this)) { + if (value instanceof Integer && ((int) value) == markedNo) { ds.setColor(theme.highlightQuoteColor); } else { ds.setColor(theme.quoteColor); @@ -134,10 +86,6 @@ public class PostLinkable extends ClickableSpan { } } - private Callback topCallback() { - return callbacks.size() > 0 ? callbacks.get(callbacks.size() - 1) : null; - } - public static class ThreadLink { public String board; public int threadId; @@ -149,10 +97,4 @@ public class PostLinkable extends ClickableSpan { this.postId = postId; } } - - public interface Callback { - void onLinkableClick(PostLinkable postLinkable); - - int getMarkedNo(PostLinkable postLinkable); - } } 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 52931f17..af995cc9 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 @@ -83,7 +83,7 @@ import static org.floens.chan.utils.AndroidUtils.getString; 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 { +public class PostCell extends LinearLayout implements PostCellInterface { private static final String TAG = "PostCell"; private static final int COMMENT_MAX_LENGTH_BOARD = 350; @@ -297,7 +297,7 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin threadMode = callback.getLoadable().isThreadMode(); - setPostLinkableListener(post, this); + setPostLinkableListener(post, true); replies.setClickable(threadMode); @@ -468,26 +468,18 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin icons.cancelCountryRequest(); - setPostLinkableListener(post, null); + setPostLinkableListener(post, false); } - private void setPostLinkableListener(Post post, PostLinkable.Callback callback) { + private void setPostLinkableListener(Post post, boolean bind) { if (post.comment instanceof Spanned) { Spanned commentSpanned = (Spanned) post.comment; PostLinkable[] linkables = commentSpanned.getSpans(0, commentSpanned.length(), PostLinkable.class); for (PostLinkable linkable : linkables) { - if (callback == null) { - while (linkable.hasCallback(this)) { - linkable.removeCallback(this); - } - } else { - if (!linkable.hasCallback(this)) { - linkable.addCallback(callback); - } - } + linkable.setMarkedNo(bind ? markedNo : -1); } - if (callback == null) { + if (!bind) { if (commentSpanned instanceof Spannable) { Spannable commentSpannable = (Spannable) commentSpanned; commentSpannable.removeSpan(BACKGROUND_SPAN); @@ -496,16 +488,6 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin } } - @Override - public void onLinkableClick(PostLinkable postLinkable) { - callback.onPostLinkableClicked(postLinkable); - } - - @Override - public int getMarkedNo(PostLinkable postLinkable) { - return markedNo; - } - private static BackgroundColorSpan BACKGROUND_SPAN = new BackgroundColorSpan(0x6633B5E5); /** @@ -534,12 +516,16 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { + ClickableSpan clickableSpan = link[0]; if (action == MotionEvent.ACTION_UP) { ignoreNextOnClick = true; - link[0].onClick(widget); + clickableSpan.onClick(widget); + if (clickableSpan instanceof PostLinkable) { + callback.onPostLinkableClicked((PostLinkable) clickableSpan); + } buffer.removeSpan(BACKGROUND_SPAN); - } else if (action == MotionEvent.ACTION_DOWN && link[0] instanceof PostLinkable) { - buffer.setSpan(BACKGROUND_SPAN, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), 0); + } else if (action == MotionEvent.ACTION_DOWN && clickableSpan instanceof PostLinkable) { + buffer.setSpan(BACKGROUND_SPAN, buffer.getSpanStart(clickableSpan), buffer.getSpanEnd(clickableSpan), 0); } else if (action == MotionEvent.ACTION_CANCEL) { buffer.removeSpan(BACKGROUND_SPAN); }