Display comment avatar only if it is the right user (#1191)

Condition in callback for loading avatar url is almost always true[1]. So it would load avatar even if the viewholder got bound to another comment.

Ideally the solution would be to update the comment just like now, then find current position of the comment and call onItemChanged. However you cannot call onItemChanged from onBindViewHolder and that is a problem because callback can be executed synchronously.

So instead we just check that viewholder is bound to some comment and that bound comment's author matches initially requested author.

[1] The only case I know when it is false is when that comment got deleted and its author got replaced with "[deleted]" before the callback got executed
This commit is contained in:
Sergei Kozelko 2022-11-03 08:47:10 +07:00 committed by GitHub
parent 191df66a3b
commit 78496e080f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -402,12 +402,16 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
if (comment.getAuthorIconUrl() == null) { if (comment.getAuthorIconUrl() == null) {
mFragment.loadIcon(comment.getAuthor(), (authorName, iconUrl) -> { mFragment.loadIcon(comment.getAuthor(), (authorName, iconUrl) -> {
if (authorName.equals(comment.getAuthor())) { if (authorName.equals(comment.getAuthor())) {
comment.setAuthorIconUrl(iconUrl);
}
Comment currentComment = getCurrentComment(holder);
if (currentComment != null && authorName.equals(currentComment.getAuthor())) {
mGlide.load(iconUrl) mGlide.load(iconUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon) .error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((CommentViewHolder) holder).authorIconImageView); .into(((CommentViewHolder) holder).authorIconImageView);
comment.setAuthorIconUrl(iconUrl);
} }
}); });
} else { } else {
@ -529,12 +533,16 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
if (comment.getAuthorIconUrl() == null) { if (comment.getAuthorIconUrl() == null) {
mFragment.loadIcon(comment.getAuthor(), (authorName, iconUrl) -> { mFragment.loadIcon(comment.getAuthor(), (authorName, iconUrl) -> {
if (authorName.equals(comment.getAuthor())) { if (authorName.equals(comment.getAuthor())) {
comment.setAuthorIconUrl(iconUrl);
}
Comment currentComment = getCurrentComment(holder);
if (currentComment != null && authorName.equals(currentComment.getAuthor())) {
mGlide.load(iconUrl) mGlide.load(iconUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(mGlide.load(R.drawable.subreddit_default_icon) .error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((CommentFullyCollapsedViewHolder) holder).authorIconImageView); .into(((CommentFullyCollapsedViewHolder) holder).authorIconImageView);
comment.setAuthorIconUrl(iconUrl);
} }
}); });
} else { } else {