Fixed bugs in CommentRecyclerViewAdapter. Minor layout tweaks.

This commit is contained in:
Alex Ning 2019-06-26 12:03:41 +08:00
parent c4690a6e38
commit a22b491a30
5 changed files with 88 additions and 65 deletions

Binary file not shown.

View File

@ -143,25 +143,24 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if(mPost.getSubredditIconUrl() == null) { if(mPost.getSubredditIconUrl() == null) {
if(mLoadSubredditIconAsyncTask != null) { if(mLoadSubredditIconAsyncTask != null) {
mLoadSubredditIconAsyncTask.cancel(true); mLoadSubredditIconAsyncTask.cancel(true);
} else {
mLoadSubredditIconAsyncTask = new LoadSubredditIconAsyncTask(
SubredditRoomDatabase.getDatabase(mActivity).subredditDao(), mPost.getSubredditNamePrefixed().substring(2),
iconImageUrl -> {
if(!iconImageUrl.equals("")) {
mGlide.load(iconImageUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((PostDetailViewHolder) holder).mSubredditIconGifImageView);
} else {
mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(((PostDetailViewHolder) holder).mSubredditIconGifImageView);
}
mPost.setSubredditIconUrl(iconImageUrl);
});
} }
mLoadSubredditIconAsyncTask = new LoadSubredditIconAsyncTask(
SubredditRoomDatabase.getDatabase(mActivity).subredditDao(), mPost.getSubredditNamePrefixed().substring(2),
iconImageUrl -> {
if(!iconImageUrl.equals("")) {
mGlide.load(iconImageUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((PostDetailViewHolder) holder).mSubredditIconGifImageView);
} else {
mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(((PostDetailViewHolder) holder).mSubredditIconGifImageView);
}
mPost.setSubredditIconUrl(iconImageUrl);
});
mLoadSubredditIconAsyncTask.execute(); mLoadSubredditIconAsyncTask.execute();
} else if(!mPost.getSubredditIconUrl().equals("")) { } else if(!mPost.getSubredditIconUrl().equals("")) {
@ -378,11 +377,8 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
} else if(holder instanceof LoadMoreCommentViewHolder) { } else if(holder instanceof LoadMoreCommentViewHolder) {
CommentData placeholder; CommentData placeholder;
if(isInitiallyLoading || isInitiallyLoadingFailed) { placeholder = mVisibleComments.get(holder.getAdapterPosition() - 1);
placeholder = mVisibleComments.get(holder.getAdapterPosition() - 2);
} else {
placeholder = mVisibleComments.get(holder.getAdapterPosition() - 1);
}
((LoadMoreCommentViewHolder) holder).verticalBlock.getLayoutParams().width = placeholder.getDepth() * 16; ((LoadMoreCommentViewHolder) holder).verticalBlock.getLayoutParams().width = placeholder.getDepth() * 16;
if(placeholder.isLoadingMoreChildren()) { if(placeholder.isLoadingMoreChildren()) {
((LoadMoreCommentViewHolder) holder).placeholderTextView.setText(R.string.loading); ((LoadMoreCommentViewHolder) holder).placeholderTextView.setText(R.string.loading);
@ -467,11 +463,14 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
comment.setExpanded(true); comment.setExpanded(true);
ArrayList<CommentData> children = comment.getChildren(); ArrayList<CommentData> children = comment.getChildren();
if(children != null && children.size() > 0) { if(children != null && children.size() > 0) {
mVisibleComments.addAll(position + 1, children); for(int i = 0; i < children.size(); i++) {
for(int i = position + 1; i <= position + children.size(); i++) { children.get(i).setExpanded(false);
mVisibleComments.get(i).setExpanded(false);
} }
notifyItemRangeInserted(position + 1, children.size()); mVisibleComments.addAll(position + 1, children);
/*for(int i = position + 1; i <= position + children.size(); i++) {
mVisibleComments.get(i).setExpanded(false);
}*/
notifyItemRangeInserted(position + 2, children.size());
} }
} }
} }
@ -489,7 +488,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
mVisibleComments.subList(position + 1, position + 1 + allChildrenSize).clear(); mVisibleComments.subList(position + 1, position + 1 + allChildrenSize).clear();
notifyItemRangeRemoved(position + 1, allChildrenSize); notifyItemRangeRemoved(position + 2, allChildrenSize);
} }
void addComments(ArrayList<CommentData> comments) { void addComments(ArrayList<CommentData> comments) {
@ -509,12 +508,21 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
void addComment(CommentData comment) { void addComment(CommentData comment) {
if(mVisibleComments.size() == 0 || isInitiallyLoadingFailed) {
notifyItemRemoved(1);
}
mVisibleComments.add(0, comment); mVisibleComments.add(0, comment);
notifyItemInserted(0);
if(isInitiallyLoading) {
notifyItemInserted(2);
} else {
notifyItemInserted(1);
}
} }
void addChildComment(CommentData comment, String parentFullname, int parentPosition) { void addChildComment(CommentData comment, String parentFullname, int parentPosition) {
if(parentFullname.equals(mVisibleComments.get(parentPosition).getFullName())) { if(!parentFullname.equals(mVisibleComments.get(parentPosition).getFullName())) {
for(int i = 0; i < mVisibleComments.size(); i++) { for(int i = 0; i < mVisibleComments.size(); i++) {
if(parentFullname.equals(mVisibleComments.get(i).getFullName())) { if(parentFullname.equals(mVisibleComments.get(i).getFullName())) {
parentPosition = i; parentPosition = i;
@ -527,10 +535,10 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
mVisibleComments.get(parentPosition).setHasReply(true); mVisibleComments.get(parentPosition).setHasReply(true);
if(!mVisibleComments.get(parentPosition).isExpanded()) { if(!mVisibleComments.get(parentPosition).isExpanded()) {
expandChildren(parentPosition); expandChildren(parentPosition);
notifyItemChanged(parentPosition); notifyItemChanged(parentPosition + 1);
} else { } else {
mVisibleComments.add(parentPosition + 1, comment); mVisibleComments.add(parentPosition + 1, comment);
notifyItemInserted(parentPosition + 1); notifyItemInserted(parentPosition + 2);
} }
} }
@ -539,7 +547,13 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
mLoadSubredditIconAsyncTask.cancel(true); mLoadSubredditIconAsyncTask.cancel(true);
} }
if(isInitiallyLoading || isInitiallyLoadingFailed) { if(mVisibleComments.size() != 0) {
int previousSize = mVisibleComments.size();
mVisibleComments.clear();
notifyItemRangeRemoved(1, previousSize);
}
if(isInitiallyLoading || isInitiallyLoadingFailed || mVisibleComments.size() == 0) {
isInitiallyLoading = true; isInitiallyLoading = true;
isInitiallyLoadingFailed = false; isInitiallyLoadingFailed = false;
notifyItemChanged(1); notifyItemChanged(1);
@ -548,8 +562,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
isInitiallyLoadingFailed = false; isInitiallyLoadingFailed = false;
notifyItemInserted(1); notifyItemInserted(1);
} }
clearComments();
} }
void initiallyLoadCommentsFailed() { void initiallyLoadCommentsFailed() {
@ -558,14 +570,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
notifyItemChanged(1); notifyItemChanged(1);
} }
private void clearComments() {
if(mVisibleComments.size() != 0) {
int previousSize = mVisibleComments.size();
mVisibleComments.clear();
notifyItemRangeRemoved(1, previousSize);
}
}
@Override @Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if (holder instanceof CommentViewHolder) { if (holder instanceof CommentViewHolder) {
@ -575,7 +579,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
@Override @Override
public int getItemCount() { public int getItemCount() {
if(isInitiallyLoading || isInitiallyLoadingFailed) { if(isInitiallyLoading || isInitiallyLoadingFailed || mVisibleComments.size() == 0) {
return 2; return 2;
} }
@ -767,10 +771,10 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
expandButton.setOnClickListener(view -> { expandButton.setOnClickListener(view -> {
if(mVisibleComments.get(getAdapterPosition() - 1).isExpanded()) { if(mVisibleComments.get(getAdapterPosition() - 1).isExpanded()) {
collapseChildren(getAdapterPosition()); collapseChildren(getAdapterPosition() - 1);
expandButton.setImageResource(R.drawable.ic_expand_more_black_20dp); expandButton.setImageResource(R.drawable.ic_expand_more_black_20dp);
} else { } else {
expandChildren(getAdapterPosition()); expandChildren(getAdapterPosition() - 1);
mVisibleComments.get(getAdapterPosition() - 1).setExpanded(true); mVisibleComments.get(getAdapterPosition() - 1).setExpanded(true);
expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp); expandButton.setImageResource(R.drawable.ic_expand_less_black_20dp);
} }
@ -782,7 +786,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mVisibleComments.get(getAdapterPosition() - 1).getCommentContent()); intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mVisibleComments.get(getAdapterPosition() - 1).getCommentContent());
intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mVisibleComments.get(getAdapterPosition() - 1).getFullName()); intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mVisibleComments.get(getAdapterPosition() - 1).getFullName());
intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true); intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, true);
intent.putExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, getAdapterPosition()); intent.putExtra(CommentActivity.EXTRA_PARENT_POSITION_KEY, getAdapterPosition() - 1);
mActivity.startActivityForResult(intent, CommentActivity.WRITE_COMMENT_REQUEST_CODE); mActivity.startActivityForResult(intent, CommentActivity.WRITE_COMMENT_REQUEST_CODE);
}); });
@ -1038,22 +1042,19 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
} }
class LoadCommentsViewHolder extends RecyclerView.ViewHolder { class LoadCommentsViewHolder extends RecyclerView.ViewHolder {
LoadCommentsViewHolder(@NonNull View itemView) {
public LoadCommentsViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
} }
} }
class LoadCommentsFailedViewHolder extends RecyclerView.ViewHolder { class LoadCommentsFailedViewHolder extends RecyclerView.ViewHolder {
LoadCommentsFailedViewHolder(@NonNull View itemView) {
public LoadCommentsFailedViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
} }
} }
class NoCommentViewHolder extends RecyclerView.ViewHolder { class NoCommentViewHolder extends RecyclerView.ViewHolder {
NoCommentViewHolder(@NonNull View itemView) {
public NoCommentViewHolder(@NonNull View itemView) {
super(itemView); super(itemView);
} }
} }

View File

@ -13,7 +13,6 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -94,7 +93,6 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mRecyclerView.setNestedScrollingEnabled(false); mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
if(savedInstanceState == null) { if(savedInstanceState == null) {
orientation = getResources().getConfiguration().orientation; orientation = getResources().getConfiguration().orientation;

View File

@ -1,8 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:gravity="center" android:layout_marginTop="36dp"
android:padding="36dp" android:orientation="vertical">
android:text="@string/post_load_comments_failed"
android:drawableTop="@drawable/load_post_error_indicator"/> <ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/load_post_error_indicator"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/post_load_comments_failed"/>
</LinearLayout>

View File

@ -1,8 +1,20 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/no_comments_yet" android:layout_marginTop="36dp"
android:gravity="center" android:orientation="vertical">
android:padding="36dp"
android:drawableTop="@drawable/no_comment_placeholder"/> <ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/no_comment_placeholder" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/no_comments_yet" />
</LinearLayout>