From 78496e080f362def8265221301ab36a79947dd34 Mon Sep 17 00:00:00 2001 From: Sergei Kozelko Date: Thu, 3 Nov 2022 08:47:10 +0700 Subject: 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 --- .../adapters/CommentsRecyclerViewAdapter.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 2046f5ed..34996b15 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -402,12 +402,16 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { if (authorName.equals(comment.getAuthor())) { + comment.setAuthorIconUrl(iconUrl); + } + + Comment currentComment = getCurrentComment(holder); + if (currentComment != null && authorName.equals(currentComment.getAuthor())) { mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) .into(((CommentViewHolder) holder).authorIconImageView); - comment.setAuthorIconUrl(iconUrl); } }); } else { @@ -529,12 +533,16 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { if (authorName.equals(comment.getAuthor())) { + comment.setAuthorIconUrl(iconUrl); + } + + Comment currentComment = getCurrentComment(holder); + if (currentComment != null && authorName.equals(currentComment.getAuthor())) { mGlide.load(iconUrl) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .error(mGlide.load(R.drawable.subreddit_default_icon) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) .into(((CommentFullyCollapsedViewHolder) holder).authorIconImageView); - comment.setAuthorIconUrl(iconUrl); } }); } else { -- cgit v1.2.3