Parse Post.repliesFrom on the background thread again

All access to Post.repliesFrom must be synchronized now
travisci
Floens 10 years ago
parent 78a84ea56a
commit 186f9aea8c
  1. 17
      Clover/app/src/main/java/org/floens/chan/chan/ChanLoader.java
  2. 52
      Clover/app/src/main/java/org/floens/chan/core/model/Post.java
  3. 22
      Clover/app/src/main/java/org/floens/chan/core/net/ChanReaderRequest.java
  4. 2
      Clover/app/src/main/java/org/floens/chan/core/presenter/ThreadPresenter.java
  5. 2
      Clover/app/src/main/java/org/floens/chan/core/watch/PinWatcher.java
  6. 18
      Clover/app/src/main/java/org/floens/chan/ui/cell/PostCell.java
  7. 1
      Clover/app/src/main/java/org/floens/chan/ui/controller/ThemeSettingsController.java

@ -267,23 +267,6 @@ public class ChanLoader implements Response.ErrorListener, Response.Listener<Cha
Logger.e(TAG, "Thread has no op!");
}
}
long start = Time.get();
List<Post> posts = thread.posts;
for (int i = 0; i < posts.size(); i++) {
Post sourcePost = posts.get(i);
for (int j = i; j < posts.size(); j++) {
Post replyToSource = posts.get(j);
if (replyToSource != sourcePost) {
if (replyToSource.repliesTo.contains(sourcePost.no)) {
sourcePost.repliesFrom.add(replyToSource.no);
}
}
}
}
Logger.test("processResponse took " + Time.get(start) + "ms");
}
private void setTimer(int postCount) {

@ -41,45 +41,85 @@ public class Post {
// *** These next members don't get changed after finish() is called. Effectively final. ***
public String board;
public int no = -1;
public int resto = -1;
public boolean isOP = false;
public String date;
public String name = "";
public CharSequence comment = "";
public String subject = "";
public long tim = -1;
public String ext;
public String filename;
public int imageWidth;
public int imageHeight;
public boolean hasImage = false;
public String thumbnailUrl;
public String imageUrl;
public String tripcode = "";
public String id = "";
public String capcode = "";
public String country = "";
public String countryName = "";
public long time = -1;
public int fileSize;
public String rawComment;
public String countryUrl;
public boolean spoiler = false;
public boolean isSavedReply = false;
public int filterHighlightedColor = 0;
public boolean filterStub = false;
public boolean filterRemove = false;
/**
* This post replies to the these ids. Is an unmodifiable list after finish().
*/
public List<Integer> repliesTo = new ArrayList<>();
public final ArrayList<PostLinkable> linkables = new ArrayList<>();
public boolean parsedSpans = false;
public SpannableString subjectSpan;
public SpannableString nameSpan;
public SpannableString tripcodeSpan;
public SpannableString idSpan;
public SpannableString capcodeSpan;
public CharSequence nameTripcodeIdCapcodeSpan;
// *** These next members may only change on the main thread after finish(). ***
@ -90,14 +130,16 @@ public class Post {
public int images = -1;
public int uniqueIps = 1;
public String title = "";
/**
* These ids replied to this post. Only modify this on the main thread.
*/
public List<Integer> repliesFrom = new ArrayList<>();
// *** Threadsafe members, may be read and modified on any thread. ***
public AtomicBoolean deleted = new AtomicBoolean(false);
public AtomicBoolean isSavedReply = new AtomicBoolean(false);
// *** Manual synchronization needed. ***
/**
* These ids replied to this post.<br>
* <b>synchronize on this when accessing.</b>
*/
public final List<Integer> repliesFrom = new ArrayList<>();
/**
* Finish up the data: parse the comment, check if the data is valid etc.

@ -24,6 +24,7 @@ import com.android.volley.Response.Listener;
import org.floens.chan.Chan;
import org.floens.chan.chan.ChanUrls;
import org.floens.chan.core.database.DatabaseManager;
import org.floens.chan.core.manager.FilterEngine;
import org.floens.chan.core.model.Filter;
import org.floens.chan.core.model.Loadable;
@ -40,10 +41,12 @@ public class ChanReaderRequest extends JsonReaderRequest<ChanReaderRequest.ChanR
private List<Post> cached;
private Post op;
private FilterEngine filterEngine;
private DatabaseManager databaseManager;
private ChanReaderRequest(String url, Listener<ChanReaderResponse> listener, ErrorListener errorListener) {
super(url, listener, errorListener);
filterEngine = FilterEngine.getInstance();
databaseManager = Chan.getDatabaseManager();
}
public static ChanReaderRequest newInstance(Loadable loadable, List<Post> cached, Listener<ChanReaderResponse> listener, ErrorListener errorListener) {
@ -131,8 +134,18 @@ public class ChanReaderRequest extends JsonReaderRequest<ChanReaderRequest.ChanR
response.posts.addAll(serverList);
}
for (Post post : response.posts) {
post.isSavedReply.set(Chan.getDatabaseManager().isSavedReply(post.board, post.no));
for (int i = 0; i < response.posts.size(); i++) {
Post sourcePost = response.posts.get(i);
synchronized (sourcePost.repliesFrom) {
sourcePost.repliesFrom.clear();
for (int j = i + 1; j < response.posts.size(); j++) {
Post replyToSource = response.posts.get(j);
if (replyToSource.repliesTo.contains(sourcePost.no)) {
sourcePost.repliesFrom.add(replyToSource.no);
}
}
}
}
return response;
@ -324,6 +337,7 @@ public class ChanReaderRequest extends JsonReaderRequest<ChanReaderRequest.ChanR
Logger.e(TAG, "Incorrect data about post received for post " + post.no);
return null;
} else {
processPostAfterFinish(post);
return post;
}
}
@ -357,6 +371,10 @@ public class ChanReaderRequest extends JsonReaderRequest<ChanReaderRequest.ChanR
}
}
private void processPostAfterFinish(Post post) {
post.isSavedReply = databaseManager.isSavedReply(post.board, post.no);
}
public static class ChanReaderResponse {
// Op Post that is created new each time.
// Used to later copy members like image count to the real op on the main thread.

@ -465,12 +465,14 @@ public class ThreadPresenter implements ChanLoader.ChanLoaderCallback, PostAdapt
@Override
public void onShowPostReplies(Post post) {
List<Post> posts = new ArrayList<>();
synchronized (post.repliesFrom) {
for (int no : post.repliesFrom) {
Post replyPost = findPostById(no);
if (replyPost != null) {
posts.add(replyPost);
}
}
}
if (posts.size() > 0) {
threadPresenterCallback.showPostsPopup(post, posts);
}

@ -147,7 +147,7 @@ public class PinWatcher implements ChanLoader.ChanLoaderCallback {
for (Post item : thread.posts) {
// saved.title = pin.loadable.title;
if (item.isSavedReply.get()) {
if (item.isSavedReply) {
savedReplies.add(item);
}
}

@ -169,7 +169,12 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin
@Override
public void onClick(View v) {
if (threadMode) {
if (post.repliesFrom.size() > 0) {
int repliesFromSize;
synchronized (post.repliesFrom) {
repliesFromSize = post.repliesFrom.size();
}
if (repliesFromSize > 0) {
callback.onShowPostReplies(post);
}
}
@ -276,7 +281,7 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin
if (highlighted) {
setBackgroundColor(theme.highlightedColor);
} else if (post.isSavedReply.get()) {
} else if (post.isSavedReply) {
setBackgroundColor(theme.savedReplyColor);
} else if (threadMode) {
setBackgroundResource(0);
@ -377,10 +382,15 @@ public class PostCell extends LinearLayout implements PostCellInterface, PostLin
}
}
if ((!threadMode && post.replies > 0) || (post.repliesFrom.size() > 0)) {
int repliesFromSize;
synchronized (post.repliesFrom) {
repliesFromSize = post.repliesFrom.size();
}
if ((!threadMode && post.replies > 0) || (repliesFromSize > 0)) {
replies.setVisibility(View.VISIBLE);
int replyCount = threadMode ? post.repliesFrom.size() : post.replies;
int replyCount = threadMode ? repliesFromSize : post.replies;
String text = getResources().getQuantityString(R.plurals.reply, replyCount, replyCount);
if (!threadMode && post.images > 0) {

@ -171,6 +171,7 @@ public class ThemeSettingsController extends Controller implements View.OnClickL
Post post = new Post();
post.no = 123456789;
post.time = (Time.get() - (30 * 60 * 1000)) / 1000;
// No synchronization needed, this is a dummy
post.repliesFrom.add(1);
post.repliesFrom.add(2);
post.repliesFrom.add(3);

Loading…
Cancel
Save