diff options
author | Sergei Kozelko <KozelkoS@yandex.ru> | 2022-12-09 07:40:21 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-09 07:40:21 +0000 |
commit | 56ac5ad6d635aac3e6dfc378c23474924626d32c (patch) | |
tree | f39d41eb26bc070d1fa0112242819444837b3783 /app/src/main/java | |
parent | 282817c1922ac36f9bc25fe82de66606f696579b (diff) | |
download | infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.tar infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.tar.gz infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.tar.bz2 infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.tar.lz infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.tar.xz infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.tar.zst infinity-for-reddit-56ac5ad6d635aac3e6dfc378c23474924626d32c.zip |
Refactor onFetchMoreCommentFailed callback (#1263)
Unified branches, extracted repeatedly used expressions.
Fortunately both branches had the same logic, except for placeholder position hint calculation.
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java | 70 |
1 files changed, 33 insertions, 37 deletions
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 a9a67f10..75ddc811 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -736,44 +736,28 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi @Override public void onFetchMoreCommentFailed() { - if (parentPosition < mVisibleComments.size() - && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { - if (mVisibleComments.get(parentPosition).isExpanded()) { - int commentPosition = mIsSingleCommentThreadMode ? holder.getBindingAdapterPosition() - 1 : holder.getBindingAdapterPosition(); - int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), commentPosition); - - if (placeholderPosition != -1) { - mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false); - mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true); - } - ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.comment_load_more_comments_failed); - } - - mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) - .setLoadingMoreChildren(false); - mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1) - .setLoadMoreChildrenFailed(true); - } else { - for (int i = 0; i < mVisibleComments.size(); i++) { - if (mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) { - if (mVisibleComments.get(i).isExpanded()) { - int placeholderPositionHint = i + mVisibleComments.get(i).getChildren().size(); - int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), placeholderPositionHint); - - if (placeholderPosition != -1) { - mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false); - mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true); - } - ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.comment_load_more_comments_failed); - } + int currentParentPosition = findCommentPosition(parentComment.getFullName(), parentPosition); + if (currentParentPosition == -1) { + // note: returning here is probably a mistake, because + // parent is just not visible, but it can still exist in the comments tree. + return; + } + Comment currentParentComment = mVisibleComments.get(currentParentPosition); - mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadingMoreChildren(false); - mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1).setLoadMoreChildrenFailed(true); + if (currentParentComment.isExpanded()) { + int placeholderPositionHint = currentParentPosition + currentParentComment.getChildren().size(); + int placeholderPosition = findLoadMoreCommentsPlaceholderPosition(parentComment.getFullName(), placeholderPositionHint); - break; - } + if (placeholderPosition != -1) { + mVisibleComments.get(placeholderPosition).setLoadingMoreChildren(false); + mVisibleComments.get(placeholderPosition).setLoadMoreChildrenFailed(true); } + ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.comment_load_more_comments_failed); } + currentParentComment.getChildren().get(currentParentComment.getChildren().size() - 1) + .setLoadingMoreChildren(false); + currentParentComment.getChildren().get(currentParentComment.getChildren().size() - 1) + .setLoadMoreChildrenFailed(true); } }); } @@ -807,23 +791,35 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi /** * Find position of comment with given {@code fullName} and + * {@link Comment#NOT_PLACEHOLDER} placeholder type + * @return position of the placeholder or -1 if not found + */ + private int findCommentPosition(String fullName, int positionHint) { + return findCommentPosition(fullName, positionHint, Comment.NOT_PLACEHOLDER); + } + + /** + * Find position of comment with given {@code fullName} and * {@link Comment#PLACEHOLDER_LOAD_MORE_COMMENTS} placeholder type * @return position of the placeholder or -1 if not found */ private int findLoadMoreCommentsPlaceholderPosition(String fullName, int positionHint) { + return findCommentPosition(fullName, positionHint, Comment.PLACEHOLDER_LOAD_MORE_COMMENTS); + } + + private int findCommentPosition(String fullName, int positionHint, int placeholderType) { if (0 <= positionHint && positionHint < mVisibleComments.size() && mVisibleComments.get(positionHint).getFullName().equals(fullName) - && mVisibleComments.get(positionHint).getPlaceholderType() == Comment.PLACEHOLDER_LOAD_MORE_COMMENTS) { + && mVisibleComments.get(positionHint).getPlaceholderType() == placeholderType) { return positionHint; } for (int i = 0; i < mVisibleComments.size(); i++) { Comment comment = mVisibleComments.get(i); - if (comment.getFullName().equals(fullName) && comment.getPlaceholderType() == Comment.PLACEHOLDER_LOAD_MORE_COMMENTS) { + if (comment.getFullName().equals(fullName) && comment.getPlaceholderType() == placeholderType) { return i; } } - return -1; } |