private List<Post> processPosts(List<Post> serverList) throws Exception { List<Post> totalList = new ArrayList<>(serverList.size()); if (cached.size() > 0) { totalList.addAll(cached); // If there's a cached post but it's not in the list received from the server, mark it as // deleted if (loadable.isThreadMode()) { boolean serverHas; for (Post cache : cached) { serverHas = false; for (Post b : serverList) { if (b.no == cache.no) { serverHas = true; break; } } cache.deleted = !serverHas; } } // If there's a post in the list from the server, that's not in the cached list, add it. boolean known; for (Post post : serverList) { known = false; for (Post cache : cached) { if (cache.no == post.no) { known = true; break; } } // serverPost is not in finalList if (!known) { totalList.add(post); } } // Sort if it got out of order due to posts disappearing/reappearing /*if (loadable.isThreadMode()) { Collections.sort(totalList, new Comparator<Post>() { @Override public int compare(Post lhs, Post rhs) { return lhs.time == rhs.time ? 0 : (lhs.time < rhs.time ? -1 : 1); } }); }*/ } else { totalList.addAll(serverList); } Set<Integer> invalidatedPosts = new HashSet<>(); for (Post post : totalList) { if (!post.deleted) { post.repliesFrom.clear(); for (Post other : totalList) { if (other.repliesTo.contains(post.no) && !other.deleted) { post.repliesFrom.add(other.no); } } } else { post.repliesTo.clear(); for (int no : post.repliesFrom) { invalidatedPosts.add(no); } post.repliesFrom.clear(); } } for (int no : invalidatedPosts) { for (Post post : totalList) { if (post.no == no) { if (!post.finish()) { throw new IOException("Incorrect data about post received."); } break; } } } for (Post post : totalList) { post.isSavedReply = ChanApplication.getDatabaseManager().isSavedReply(post.board, post.no); } return totalList; }
public void setPost(final Post post, final ThreadManager manager) { this.post = post; this.manager = manager; highlightQuotesNo = -1; boolean boardCatalogMode = manager.getLoadable().isBoardMode() || manager.getLoadable().isCatalogMode(); TypedArray ta = context.obtainStyledAttributes(null, R.styleable.PostView, R.attr.post_style, 0); if (!isBuild) { buildView(context, ta); isBuild = true; } int dateColor = ta.getColor(R.styleable.PostView_date_color, 0); int savedReplyColor = ta.getColor(R.styleable.PostView_saved_reply_color, 0); int highlightedColor = ta.getColor(R.styleable.PostView_highlighted_color, 0); int detailSize = ta.getDimensionPixelSize(R.styleable.PostView_detail_size, 0); ta.recycle(); if (post.hasImage) { imageView.setVisibility(View.VISIBLE); imageView.setImageUrl(post.thumbnailUrl, ChanApplication.getVolleyImageLoader()); } else { imageView.setVisibility(View.GONE); imageView.setImageUrl(null, null); } CharSequence total = new SpannableString(""); if (post.subjectSpan != null) { total = TextUtils.concat(total, post.subjectSpan); } if (isList()) { CharSequence relativeTime = DateUtils.getRelativeTimeSpanString( post.time * 1000L, Time.get(), DateUtils.SECOND_IN_MILLIS, 0); SpannableString date = new SpannableString("No." + post.no + " " + relativeTime); date.setSpan(new ForegroundColorSpan(dateColor), 0, date.length(), 0); date.setSpan(new AbsoluteSizeSpan(detailSize), 0, date.length(), 0); total = TextUtils.concat( total, post.subjectSpan == null ? "" : "\n", post.nameTripcodeIdCapcodeSpan, date, " "); } if (!TextUtils.isEmpty(total)) { titleView.setText(total); titleView.setVisibility(View.VISIBLE); } else { titleView.setVisibility(View.GONE); } commentView.setText(post.comment); if (manager.getLoadable().isThreadMode()) { post.setLinkableListener(this); commentView.setMovementMethod(new PostViewMovementMethod()); commentView.setOnClickListener(this); } else { post.setLinkableListener(null); commentView.setOnClickListener(null); commentView.setClickable(false); commentView.setMovementMethod(null); } if (isGrid() || ((post.isOP && boardCatalogMode && post.replies > 0) || (post.repliesFrom.size() > 0))) { repliesCountView.setVisibility(View.VISIBLE); String text = ""; int count = boardCatalogMode ? post.replies : post.repliesFrom.size(); if (count != 1) { text = count + " " + context.getString(R.string.multiple_replies); } else if (count == 1) { text = count + " " + context.getString(R.string.one_reply); } if (boardCatalogMode && post.images > 0) { if (post.images != 1) { text += ", " + post.images + " " + context.getString(R.string.multiple_images); } else { text += ", " + post.images + " " + context.getString(R.string.one_image); } } if (manager.getLoadable().isThreadMode()) { repliesCountView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { manager.showPostReplies(post); } }); } repliesCountView.setText(text); } else { repliesCountView.setVisibility(View.GONE); repliesCountView.setOnClickListener(null); } boolean showCountryFlag = isList() && !TextUtils.isEmpty(post.country) && !TextUtils.isEmpty(post.countryUrl); boolean showStickyIcon = isList() && post.sticky; boolean showDeletedIcon = isList() && post.deleted; boolean showArchivedIcon = isList() && post.archived; boolean showClosedIcon = isList() && post.closed && !showArchivedIcon; iconsView.setVisibility( (showCountryFlag || showStickyIcon || showClosedIcon || showDeletedIcon || showArchivedIcon) ? View.VISIBLE : View.GONE); stickyView.setVisibility(showStickyIcon ? View.VISIBLE : View.GONE); closedView.setVisibility(showClosedIcon ? View.VISIBLE : View.GONE); deletedView.setVisibility(showDeletedIcon ? View.VISIBLE : View.GONE); archivedView.setVisibility(showArchivedIcon ? View.VISIBLE : View.GONE); if (showCountryFlag) { countryView.setVisibility(View.VISIBLE); countryView.setImageUrl(post.countryUrl, ChanApplication.getVolleyImageLoader()); } else { countryView.setVisibility(View.GONE); countryView.setImageUrl(null, null); } if (post.isSavedReply) { full.setBackgroundColor(savedReplyColor); } else if (manager.isPostHightlighted(post)) { full.setBackgroundColor(highlightedColor); } else { full.setBackgroundColor(0x00000000); } if (manager.isPostLastSeen(post)) { lastSeen.setVisibility(View.VISIBLE); } else { lastSeen.setVisibility(View.GONE); } }