Calculate the comment placeholder position if the position was changed.

This commit is contained in:
Alex Ning 2019-06-23 16:28:08 +08:00
parent 9ddf15b8fb
commit e9cec91a5b
4 changed files with 93 additions and 49 deletions

View File

@ -50,7 +50,8 @@ class CommentData implements Parcelable {
isPlaceHolder = false; isPlaceHolder = false;
} }
CommentData(int depth) { CommentData(String parentFullName, int depth) {
this.fullName = parentFullName;
this.depth = depth; this.depth = depth;
isPlaceHolder = true; isPlaceHolder = true;
isLoadingMoreChildren = false; isLoadingMoreChildren = false;

View File

@ -9,7 +9,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -309,27 +308,54 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
if(mVisibleComments.get(parentPosition).isExpanded()) { if(mVisibleComments.get(parentPosition).isExpanded()) {
if(mVisibleComments.get(parentPosition).getChildren().size() > childrenStartingIndex) { if(mVisibleComments.get(parentPosition).getChildren().size() > childrenStartingIndex) {
mVisibleComments.get(position).setLoadingMoreChildren(false);
mVisibleComments.get(position).setLoadMoreChildrenFailed(false);
notifyItemChanged(position);
mVisibleComments.get(parentPosition).setMoreChildrenStartingIndex(childrenStartingIndex); mVisibleComments.get(parentPosition).setMoreChildrenStartingIndex(childrenStartingIndex);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadingMoreChildren(false); .setLoadingMoreChildren(false);
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false); .setLoadMoreChildrenFailed(false);
mVisibleComments.addAll(position, expandedComments); int placeholderPosition = position;
notifyItemRangeInserted(position, expandedComments.size()); if(mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
notifyItemChanged(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
} else { } else {
mVisibleComments.get(parentPosition).getChildren() mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1); .remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.remove(position);
notifyItemRemoved(position);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames(); mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
mVisibleComments.addAll(position, expandedComments); int placeholderPosition = position;
notifyItemRangeInserted(position, expandedComments.size()); if(mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) {
for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.remove(placeholderPosition);
notifyItemRemoved(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
}
} else {
if(mVisibleComments.get(parentPosition).hasReply() && mVisibleComments.get(parentPosition).getChildren().size() <= childrenStartingIndex) {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
} }
} }
@ -337,17 +363,24 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} else { } else {
for(int i = 0; i < mVisibleComments.size(); i++) { for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) { if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
mVisibleComments.get(i).setMoreChildrenStartingIndex(childrenStartingIndex);
if(mVisibleComments.get(i).isExpanded()) { if(mVisibleComments.get(i).isExpanded()) {
mVisibleComments.get(i + mVisibleComments.get(i).getChildren().size()).setLoadingMoreChildren(false); int placeholderPosition = i + mVisibleComments.get(i).getChildren().size();
mVisibleComments.get(i + mVisibleComments.get(i).getChildren().size()).setLoadMoreChildrenFailed(false);
notifyItemChanged(i + mVisibleComments.get(i).getChildren().size());
mVisibleComments.addAll(i + mVisibleComments.get(i).getChildren().size(), if(!mVisibleComments.get(i).getFullName()
expandedComments); .equals(mVisibleComments.get(placeholderPosition).getFullName())) {
notifyItemRangeInserted(i + mVisibleComments.get(i).getChildren().size(), for(int j = i + 1; j < mVisibleComments.size(); j++) {
expandedComments.size()); if(mVisibleComments.get(j).getFullName().equals(mVisibleComments.get(i).getFullName())) {
placeholderPosition = j;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(false);
notifyItemChanged(placeholderPosition);
mVisibleComments.addAll(placeholderPosition, expandedComments);
notifyItemRangeInserted(placeholderPosition, expandedComments.size());
} }
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1) mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
@ -367,9 +400,19 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if(parentPosition < mVisibleComments.size() if(parentPosition < mVisibleComments.size()
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
if(mVisibleComments.get(parentPosition).isExpanded()) { if(mVisibleComments.get(parentPosition).isExpanded()) {
mVisibleComments.get(position).setLoadingMoreChildren(false); int placeholderPosition = position;
mVisibleComments.get(position).setLoadMoreChildrenFailed(true); if(!mVisibleComments.get(position).getFullName().equals(parentComment.getFullName())) {
notifyItemChanged(position); for(int i = parentPosition + 1; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
placeholderPosition = i;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true);
notifyItemChanged(placeholderPosition);
} }
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
@ -380,9 +423,19 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
for(int i = 0; i < mVisibleComments.size(); i++) { for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) { if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
if(mVisibleComments.get(i).isExpanded()) { if(mVisibleComments.get(i).isExpanded()) {
mVisibleComments.get(i + mVisibleComments.get(i).getChildren().size()).setLoadingMoreChildren(false); int placeholderPosition = i + mVisibleComments.get(i).getChildren().size();
mVisibleComments.get(i + mVisibleComments.get(i).getChildren().size()).setLoadMoreChildrenFailed(true); if(!mVisibleComments.get(placeholderPosition).getFullName().equals(mVisibleComments.get(i).getFullName())) {
notifyItemChanged(i + mVisibleComments.get(i).getChildren().size()); for(int j = i + 1; j < mVisibleComments.size(); j++) {
if(mVisibleComments.get(j).getFullName().equals(mVisibleComments.get(i).getFullName())) {
placeholderPosition = j;
break;
}
}
}
mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false);
mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true);
notifyItemChanged(placeholderPosition);
} }
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadingMoreChildren(false); mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadingMoreChildren(false);
@ -478,7 +531,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) { if (holder instanceof CommentViewHolder) {
((CommentViewHolder) holder).expandButton.setVisibility(View.GONE); ((CommentViewHolder) holder).expandButton.setVisibility(View.GONE);
((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.GONE);
} }
} }
@ -487,14 +539,13 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
return mVisibleComments.size(); return mVisibleComments.size();
} }
class CommentViewHolder extends RecyclerView.ViewHolder { static class CommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.author_text_view_item_post_comment) TextView authorTextView; @BindView(R.id.author_text_view_item_post_comment) TextView authorTextView;
@BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView; @BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView;
@BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView; @BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView;
@BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton; @BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton;
@BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView; @BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView;
@BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton;
@BindView(R.id.load_more_comments_progress_bar) ProgressBar loadMoreCommentsProgressBar;
@BindView(R.id.expand_button_item_post_comment) ImageView expandButton; @BindView(R.id.expand_button_item_post_comment) ImageView expandButton;
@BindView(R.id.share_button_item_post_comment) ImageView shareButton; @BindView(R.id.share_button_item_post_comment) ImageView shareButton;
@BindView(R.id.reply_button_item_post_comment) ImageView replyButton; @BindView(R.id.reply_button_item_post_comment) ImageView replyButton;
@ -506,7 +557,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
} }
class LoadMoreCommentViewHolder extends RecyclerView.ViewHolder { static class LoadMoreCommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.vertical_block_item_load_more_comments) View verticalBlock; @BindView(R.id.vertical_block_item_load_more_comments) View verticalBlock;
@BindView(R.id.load_more_comments_text_view_item_load_more_comments) TextView textView; @BindView(R.id.load_more_comments_text_view_item_load_more_comments) TextView textView;
@ -516,7 +567,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
} }
class IsLoadingMoreCommentViewHolder extends RecyclerView.ViewHolder { static class IsLoadingMoreCommentViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.vertical_block_item_is_loading_more_comments) View verticalBlock; @BindView(R.id.vertical_block_item_is_loading_more_comments) View verticalBlock;
IsLoadingMoreCommentViewHolder(View itemView) { IsLoadingMoreCommentViewHolder(View itemView) {
@ -525,7 +576,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
} }
class LoadMoreCommentFailedViewHolder extends RecyclerView.ViewHolder { static class LoadMoreCommentFailedViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.vertical_block_item_load_more_comments_failed) View verticalBlock; @BindView(R.id.vertical_block_item_load_more_comments_failed) View verticalBlock;
@BindView(R.id.retry_text_view_item_load_more_comments_failed) TextView retryTextView; @BindView(R.id.retry_text_view_item_load_more_comments_failed) TextView retryTextView;

View File

@ -93,7 +93,7 @@ class ParseComment {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
try { try {
parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale); parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale);
makeChildrenVisible(newComments, expandedNewComments); expandChildren(newComments, expandedNewComments);
} catch (JSONException e) { } catch (JSONException e) {
parseFailed = true; parseFailed = true;
if(e.getMessage() != null) { if(e.getMessage() != null) {
@ -156,17 +156,18 @@ class ParseComment {
} }
} }
private static void makeChildrenVisible(ArrayList<CommentData> comments, ArrayList<CommentData> visibleComments) { private static void expandChildren(ArrayList<CommentData> comments, ArrayList<CommentData> visibleComments) {
for(CommentData c : comments) { for(CommentData c : comments) {
visibleComments.add(c); visibleComments.add(c);
if(c.hasReply()) { if(c.hasReply()) {
c.setExpanded(true); c.setExpanded(true);
makeChildrenVisible(c.getChildren(), visibleComments); expandChildren(c.getChildren(), visibleComments);
} }
if(c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) { if(c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) {
//Add a load more placeholder //Add a load more placeholder
visibleComments.add(new CommentData(c.getDepth() + 1)); CommentData placeholder = new CommentData(c.getFullName(), c.getDepth() + 1);
c.addChild(new CommentData(c.getDepth() + 1), c.getChildren().size()); visibleComments.add(placeholder);
c.addChild(placeholder, c.getChildren().size());
} }
} }
} }

View File

@ -9,7 +9,7 @@
<View <View
android:id="@+id/vertical_block_item_post_comment" android:id="@+id/vertical_block_item_post_comment"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/textColorPrimaryDark"/> android:background="@color/textColorPrimaryDark"/>
@ -36,9 +36,8 @@
<TextView <TextView
android:id="@+id/comment_time_text_view_item_post_comment" android:id="@+id/comment_time_text_view_item_post_comment"
android:layout_width="0dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end" /> android:gravity="end" />
</LinearLayout> </LinearLayout>
@ -92,14 +91,6 @@
android:clickable="true" android:clickable="true"
android:focusable="true"/> android:focusable="true"/>
<ProgressBar
android:id="@+id/load_more_comments_progress_bar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginEnd="16dp"
android:layout_toStartOf="@id/expand_button_item_post_comment"
android:visibility="gone"/>
<ImageView <ImageView
android:id="@+id/expand_button_item_post_comment" android:id="@+id/expand_button_item_post_comment"
android:layout_width="wrap_content" android:layout_width="wrap_content"