Remove postlinkable callback trickery and handle it in the movementmethod

Should've done this from the start...
multisite
Floens 9 years ago
parent d04142cd1b
commit 26f8250c0b
  1. 70
      Clover/app/src/main/java/org/floens/chan/core/model/PostLinkable.java
  2. 40
      Clover/app/src/main/java/org/floens/chan/ui/cell/PostCell.java

@ -25,12 +25,8 @@ import android.view.View;
import org.floens.chan.ui.cell.PostCell; import org.floens.chan.ui.cell.PostCell;
import org.floens.chan.ui.theme.Theme; 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.<br> * A Clickable span that handles post clicks. These are created in ChanParser for post quotes, spoilers etc.<br>
* PostCells bind callbacks with addCallback and call removeCallback when done.<br>
* PostCell has a {@link PostCell.PostViewMovementMethod}, that searches spans at the location the TextView was tapped, * PostCell has a {@link PostCell.PostViewMovementMethod}, that searches spans at the location the TextView was tapped,
* and handled if it was a PostLinkable. * and handled if it was a PostLinkable.
*/ */
@ -45,11 +41,8 @@ public class PostLinkable extends ClickableSpan {
public final Object value; public final Object value;
public final Type type; public final Type type;
private List<Callback> callbacks = new ArrayList<>();
private boolean spoilerVisible = false; private boolean spoilerVisible = false;
private int markedNo = -1;
// private static boolean testingCallbacks = false;
// private static HashMap<PostLinkable, List<Callback>> callbacksTest = new HashMap<>();
public PostLinkable(Theme theme, Post post, String key, Object value, Type type) { public PostLinkable(Theme theme, Post post, String key, Object value, Type type) {
this.theme = theme; this.theme = theme;
@ -57,63 +50,22 @@ public class PostLinkable extends ClickableSpan {
this.key = key; this.key = key;
this.value = value; this.value = value;
this.type = type; 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<PostLinkable, List<Callback>> 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<Callback>());
}
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 @Override
public void onClick(View widget) { public void onClick(View widget) {
Callback top = topCallback();
if (top != null) {
top.onLinkableClick(this);
}
spoilerVisible = !spoilerVisible; spoilerVisible = !spoilerVisible;
} }
public void setMarkedNo(int markedNo) {
this.markedNo = markedNo;
}
@Override @Override
public void updateDrawState(@NonNull TextPaint ds) { public void updateDrawState(@NonNull TextPaint ds) {
if (type == Type.QUOTE || type == Type.LINK || type == Type.THREAD) { if (type == Type.QUOTE || type == Type.LINK || type == Type.THREAD) {
if (type == Type.QUOTE) { if (type == Type.QUOTE) {
Callback top = topCallback(); if (value instanceof Integer && ((int) value) == markedNo) {
if (value instanceof Integer && top != null && (Integer) value == top.getMarkedNo(this)) {
ds.setColor(theme.highlightQuoteColor); ds.setColor(theme.highlightQuoteColor);
} else { } else {
ds.setColor(theme.quoteColor); 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 static class ThreadLink {
public String board; public String board;
public int threadId; public int threadId;
@ -149,10 +97,4 @@ public class PostLinkable extends ClickableSpan {
this.postId = postId; this.postId = postId;
} }
} }
public interface Callback {
void onLinkableClick(PostLinkable postLinkable);
int getMarkedNo(PostLinkable postLinkable);
}
} }

@ -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.setRoundItemBackground;
import static org.floens.chan.utils.AndroidUtils.sp; 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 String TAG = "PostCell";
private static final int COMMENT_MAX_LENGTH_BOARD = 350; 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(); threadMode = callback.getLoadable().isThreadMode();
setPostLinkableListener(post, this); setPostLinkableListener(post, true);
replies.setClickable(threadMode); replies.setClickable(threadMode);
@ -468,26 +468,18 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin
icons.cancelCountryRequest(); 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) { if (post.comment instanceof Spanned) {
Spanned commentSpanned = (Spanned) post.comment; Spanned commentSpanned = (Spanned) post.comment;
PostLinkable[] linkables = commentSpanned.getSpans(0, commentSpanned.length(), PostLinkable.class); PostLinkable[] linkables = commentSpanned.getSpans(0, commentSpanned.length(), PostLinkable.class);
for (PostLinkable linkable : linkables) { for (PostLinkable linkable : linkables) {
if (callback == null) { linkable.setMarkedNo(bind ? markedNo : -1);
while (linkable.hasCallback(this)) {
linkable.removeCallback(this);
}
} else {
if (!linkable.hasCallback(this)) {
linkable.addCallback(callback);
}
}
} }
if (callback == null) { if (!bind) {
if (commentSpanned instanceof Spannable) { if (commentSpanned instanceof Spannable) {
Spannable commentSpannable = (Spannable) commentSpanned; Spannable commentSpannable = (Spannable) commentSpanned;
commentSpannable.removeSpan(BACKGROUND_SPAN); 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); 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); ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
if (link.length != 0) { if (link.length != 0) {
ClickableSpan clickableSpan = link[0];
if (action == MotionEvent.ACTION_UP) { if (action == MotionEvent.ACTION_UP) {
ignoreNextOnClick = true; ignoreNextOnClick = true;
link[0].onClick(widget); clickableSpan.onClick(widget);
if (clickableSpan instanceof PostLinkable) {
callback.onPostLinkableClicked((PostLinkable) clickableSpan);
}
buffer.removeSpan(BACKGROUND_SPAN); buffer.removeSpan(BACKGROUND_SPAN);
} else if (action == MotionEvent.ACTION_DOWN && link[0] instanceof PostLinkable) { } else if (action == MotionEvent.ACTION_DOWN && clickableSpan instanceof PostLinkable) {
buffer.setSpan(BACKGROUND_SPAN, buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), 0); buffer.setSpan(BACKGROUND_SPAN, buffer.getSpanStart(clickableSpan), buffer.getSpanEnd(clickableSpan), 0);
} else if (action == MotionEvent.ACTION_CANCEL) { } else if (action == MotionEvent.ACTION_CANCEL) {
buffer.removeSpan(BACKGROUND_SPAN); buffer.removeSpan(BACKGROUND_SPAN);
} }

Loading…
Cancel
Save