From 06fb6c2032ce7f80a01f27e48d6579d55920669a Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 26 Sep 2024 13:59:09 -0400 Subject: Start optimizing class structures in PostRecyclerViewAdapter. --- .../adapters/PostRecyclerViewAdapter.java | 821 +++++++-------------- 1 file changed, 252 insertions(+), 569 deletions(-) (limited to 'app/src/main/java/ml/docilealligator') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index 514a01df..f19c3c6e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -2595,51 +2595,24 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - int position = getBindingAdapterPosition(); - if (position >= 0 && canStartActivity) { - Post post = getItem(position); - if (post != null) { - markPostRead(post, true); - - openViewPostDetailActivity(post, getBindingAdapterPosition()); - } - } - }); - - itemView.setOnLongClickListener(v -> { - Post post = getItem(getBindingAdapterPosition()); - if (post == null) { - return false; - } - - PostOptionsBottomSheetFragment postOptionsBottomSheetFragment; - if (post.getPostType() == Post.GALLERY_TYPE && this instanceof PostBaseGalleryTypeViewHolder) { - postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post, - ((LinearLayoutManagerBugFixed) ((PostBaseGalleryTypeViewHolder) this).galleryRecyclerView.getLayoutManager()).findFirstVisibleItemPosition()); - } else { - postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post); - } - postOptionsBottomSheetFragment.show(mActivity.getSupportFragmentManager(), postOptionsBottomSheetFragment.getTag()); - return true; - }); - - itemView.setOnTouchListener((v, event) -> { - if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { - if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { - mActivity.unlockSwipeRightToGoBack(); - } - } else { - if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { - mActivity.lockSwipeRightToGoBack(); - } - } - return false; - }); - - userTextView.setOnClickListener(view -> { - if (!canStartActivity) { - return; - } - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post == null || post.isAuthorDeleted()) { - return; - } - canStartActivity = false; - Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); - mActivity.startActivity(intent); - }); - if (mDisplaySubredditName) { subredditTextView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); @@ -2835,39 +2669,23 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter userTextView.performClick()); } - if (!(mActivity instanceof FilteredPostsActivity)) { - nsfwTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - mCallback.nsfwChipClicked(); - } - }); - typeTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - mCallback.typeChipClicked(post.getPostType()); - } - }); - - flairTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - mCallback.flairChipClicked(post.getFlair()); - } - }); - } + userTextView.setOnClickListener(view -> { + if (!canStartActivity) { + return; + } + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post == null || post.isAuthorDeleted()) { + return; + } + canStartActivity = false; + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); + mActivity.startActivity(intent); + }); upvoteButton.setOnClickListener(view -> { int position = getBindingAdapterPosition(); @@ -3174,6 +2992,31 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position >= 0 && canStartActivity) { + Post post = getItem(position); + if (post != null) { + markPostRead(post, true); + + openViewPostDetailActivity(post, getBindingAdapterPosition()); + } + } + }); + + itemView.setOnLongClickListener(v -> { + Post post = getItem(getBindingAdapterPosition()); + if (post == null) { + return false; + } + + PostOptionsBottomSheetFragment postOptionsBottomSheetFragment; + if (post.getPostType() == Post.GALLERY_TYPE && this instanceof PostBaseGalleryTypeViewHolder) { + postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post, + ((LinearLayoutManagerBugFixed) ((PostBaseGalleryTypeViewHolder) this).galleryRecyclerView.getLayoutManager()).findFirstVisibleItemPosition()); + } else { + postOptionsBottomSheetFragment = PostOptionsBottomSheetFragment.newInstance(post); + } + postOptionsBottomSheetFragment.show(mActivity.getSupportFragmentManager(), postOptionsBottomSheetFragment.getTag()); + return true; + }); + + itemView.setOnTouchListener((v, event) -> { + if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) { + if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { + mActivity.unlockSwipeRightToGoBack(); + } + } else { + if (mFragment.isRecyclerViewItemSwipeable(PostBaseViewHolder.this)) { + mActivity.lockSwipeRightToGoBack(); + } + } + return false; + }); + + if (!(mActivity instanceof FilteredPostsActivity)) { + nsfwTextView.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.nsfwChipClicked(); + } + }); + typeTextView.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.typeChipClicked(post.getPostType()); + } + }); + + flairTextView.setOnClickListener(view -> { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null) { + mCallback.flairChipClicked(post.getFlair()); + } + }); + } + } + + void setBaseView(AspectRatioGifImageView iconGifImageView, + TextView subredditTextView, + TextView userTextView, + ImageView stickiedPostImageView, + TextView postTimeTextView, + TextView titleTextView, + CustomTextView typeTextView, + ImageView archivedImageView, + ImageView lockedImageView, + ImageView crosspostImageView, + CustomTextView nsfwTextView, + CustomTextView spoilerTextView, + CustomTextView flairTextView, + ConstraintLayout bottomConstraintLayout, + MaterialButton upvoteButton, + TextView scoreTextView, + MaterialButton downvoteButton, + MaterialButton commentsCountButton, + MaterialButton saveButton, + MaterialButton shareButton, boolean itemViewIsNotCardView) { + this.itemViewIsNotCardView = itemViewIsNotCardView; + + setBaseView(iconGifImageView, subredditTextView, userTextView, stickiedPostImageView, postTimeTextView, + titleTextView, typeTextView, archivedImageView, lockedImageView, crosspostImageView, + nsfwTextView, spoilerTextView, flairTextView, bottomConstraintLayout, + upvoteButton, scoreTextView, downvoteButton, commentsCountButton, saveButton, shareButton); + } + @Override void markPostRead(Post post, boolean changePostItemColor) { if (!mAccountName.equals(Account.ANONYMOUS_ACCOUNT) && !post.isRead() && mMarkPostsAsRead) { post.markAsRead(); @@ -5151,7 +5182,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - if (!canStartActivity) { - return; - } - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post == null || post.isAuthorDeleted()) { - return; - } - canStartActivity = false; - Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); - mActivity.startActivity(intent); - }); - - if (mDisplaySubredditName) { - subredditTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - if (canStartActivity) { - canStartActivity = false; - Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, - post.getSubredditName()); - mActivity.startActivity(intent); - } - } - }); - - iconGifImageView.setOnClickListener(view -> subredditTextView.performClick()); - } else { - subredditTextView.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - if (canStartActivity) { - canStartActivity = false; - Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, - post.getSubredditName()); - mActivity.startActivity(intent); - } - } - }); - - iconGifImageView.setOnClickListener(view -> userTextView.performClick()); - } - - upvoteButton.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - - if (mMarkPostsAsReadAfterVoting) { - markPostRead(post, true); - } - - if (post.isArchived()) { - Toast.makeText(mActivity, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); - return; - } - - ColorStateList previousUpvoteButtonIconTint = upvoteButton.getIconTint(); - ColorStateList previousDownvoteButtonIconTint = downvoteButton.getIconTint(); - int previousScoreTextViewColor = scoreTextView.getCurrentTextColor(); - Drawable previousUpvoteButtonDrawable = upvoteButton.getIcon(); - Drawable previousDownvoteButtonDrawable = downvoteButton.getIcon(); - - int previousVoteType = post.getVoteType(); - String newVoteType; - - downvoteButton.setIconResource(R.drawable.ic_downvote_24dp); - downvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - - if (previousVoteType != 1) { - //Not upvoted before - post.setVoteType(1); - newVoteType = APIUtils.DIR_UPVOTE; - upvoteButton.setIconResource(R.drawable.ic_upvote_filled_24dp); - upvoteButton.setIconTint(ColorStateList.valueOf(mUpvotedColor)); - scoreTextView.setTextColor(mUpvotedColor); - } else { - //Upvoted before - post.setVoteType(0); - newVoteType = APIUtils.DIR_UNVOTE; - upvoteButton.setIconResource(R.drawable.ic_upvote_24dp); - upvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - scoreTextView.setTextColor(mPostIconAndInfoColor); - } - - if (!mHideTheNumberOfVotes) { - scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType())); - } - - VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { - int currentPosition = getBindingAdapterPosition(); - if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { - post.setVoteType(1); - if (currentPosition == position) { - upvoteButton.setIconResource(R.drawable.ic_upvote_filled_24dp); - upvoteButton.setIconTint(ColorStateList.valueOf(mUpvotedColor)); - scoreTextView.setTextColor(mUpvotedColor); - } - } else { - post.setVoteType(0); - if (currentPosition == position) { - upvoteButton.setIconResource(R.drawable.ic_upvote_24dp); - upvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - scoreTextView.setTextColor(mPostIconAndInfoColor); - } - } - - if (currentPosition == position) { - downvoteButton.setIconResource(R.drawable.ic_downvote_24dp); - downvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - if (!mHideTheNumberOfVotes) { - scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType())); - } - } - - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - - @Override - public void onVoteThingFail(int position1) { - Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); - post.setVoteType(previousVoteType); - if (getBindingAdapterPosition() == position) { - if (!mHideTheNumberOfVotes) { - scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + previousVoteType)); - } - upvoteButton.setIcon(previousUpvoteButtonDrawable); - upvoteButton.setIconTint(previousUpvoteButtonIconTint); - scoreTextView.setTextColor(previousScoreTextViewColor); - downvoteButton.setIcon(previousDownvoteButtonDrawable); - downvoteButton.setIconTint(previousDownvoteButtonIconTint); - } - - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - }, post.getFullName(), newVoteType, getBindingAdapterPosition()); - } - }); - - downvoteButton.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - - if (mMarkPostsAsReadAfterVoting) { - markPostRead(post, true); - } - - if (post.isArchived()) { - Toast.makeText(mActivity, R.string.archived_post_vote_unavailable, Toast.LENGTH_SHORT).show(); - return; - } - - ColorStateList previousUpvoteButtonIconTint = upvoteButton.getIconTint(); - ColorStateList previousDownvoteButtonIconTint = downvoteButton.getIconTint(); - Drawable previousUpvoteButtonDrawable = upvoteButton.getIcon(); - Drawable previousDownvoteButtonDrawable = downvoteButton.getIcon(); - - int previousVoteType = post.getVoteType(); - String newVoteType; - - upvoteButton.setIconResource(R.drawable.ic_upvote_24dp); - upvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - scoreTextView.setTextColor(mPostIconAndInfoColor); - - if (previousVoteType != -1) { - //Not downvoted before - post.setVoteType(-1); - newVoteType = APIUtils.DIR_DOWNVOTE; - downvoteButton.setIconResource(R.drawable.ic_downvote_filled_24dp); - downvoteButton.setIconTint(ColorStateList.valueOf(mDownvotedColor)); - scoreTextView.setTextColor(mDownvotedColor); - } else { - //Downvoted before - post.setVoteType(0); - newVoteType = APIUtils.DIR_UNVOTE; - downvoteButton.setIconResource(R.drawable.ic_downvote_24dp); - downvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - scoreTextView.setTextColor(mPostIconAndInfoColor); - } - - if (!mHideTheNumberOfVotes) { - scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType())); - } - - VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { - @Override - public void onVoteThingSuccess(int position1) { - int currentPosition = getBindingAdapterPosition(); - if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { - post.setVoteType(-1); - if (currentPosition == position) { - downvoteButton.setIconResource(R.drawable.ic_downvote_filled_24dp); - downvoteButton.setIconTint(ColorStateList.valueOf(mDownvotedColor)); - scoreTextView.setTextColor(mDownvotedColor); - } - } else { - post.setVoteType(0); - if (currentPosition == position) { - downvoteButton.setIconResource(R.drawable.ic_downvote_24dp); - downvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - scoreTextView.setTextColor(mPostIconAndInfoColor); - } - } - - if (currentPosition == position) { - upvoteButton.setIconResource(R.drawable.ic_upvote_24dp); - upvoteButton.setIconTint(ColorStateList.valueOf(mPostIconAndInfoColor)); - scoreTextView.setTextColor(mPostIconAndInfoColor); - if (!mHideTheNumberOfVotes) { - scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + post.getVoteType())); - } - } - - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - - @Override - public void onVoteThingFail(int position1) { - Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); - post.setVoteType(previousVoteType); - if (getBindingAdapterPosition() == position) { - if (!mHideTheNumberOfVotes) { - scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + previousVoteType)); - } - upvoteButton.setIcon(previousUpvoteButtonDrawable); - upvoteButton.setIconTint(previousUpvoteButtonIconTint); - scoreTextView.setTextColor(previousUpvoteButtonIconTint); - downvoteButton.setIcon(previousDownvoteButtonDrawable); - downvoteButton.setIconTint(previousDownvoteButtonIconTint); - } - - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - }, post.getFullName(), newVoteType, getBindingAdapterPosition()); - } - }); - - commentsCountButton.setOnClickListener(view -> itemView.performClick()); - - saveButton.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - - if (post.isSaved()) { - saveButton.setIconResource(R.drawable.ic_bookmark_border_grey_24dp); - SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(), - new SaveThing.SaveThingListener() { - @Override - public void success() { - post.setSaved(false); - if (getBindingAdapterPosition() == position) { - saveButton.setIconResource(R.drawable.ic_bookmark_border_grey_24dp); - } - Toast.makeText(mActivity, R.string.post_unsaved_success, Toast.LENGTH_SHORT).show(); - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - - @Override - public void failed() { - post.setSaved(true); - if (getBindingAdapterPosition() == position) { - saveButton.setIconResource(R.drawable.ic_bookmark_grey_24dp); - } - Toast.makeText(mActivity, R.string.post_unsaved_failed, Toast.LENGTH_SHORT).show(); - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - }); - } else { - saveButton.setIconResource(R.drawable.ic_bookmark_grey_24dp); - SaveThing.saveThing(mOauthRetrofit, mAccessToken, post.getFullName(), - new SaveThing.SaveThingListener() { - @Override - public void success() { - post.setSaved(true); - if (getBindingAdapterPosition() == position) { - saveButton.setIconResource(R.drawable.ic_bookmark_grey_24dp); - } - Toast.makeText(mActivity, R.string.post_saved_success, Toast.LENGTH_SHORT).show(); - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - - @Override - public void failed() { - post.setSaved(false); - if (getBindingAdapterPosition() == position) { - saveButton.setIconResource(R.drawable.ic_bookmark_border_grey_24dp); - } - Toast.makeText(mActivity, R.string.post_saved_failed, Toast.LENGTH_SHORT).show(); - EventBus.getDefault().post(new PostUpdateEventToPostDetailFragment(post)); - } - }); - } - } - }); - - shareButton.setOnClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - shareLink(post); - } - }); - - shareButton.setOnLongClickListener(view -> { - int position = getBindingAdapterPosition(); - if (position < 0) { - return false; - } - Post post = getItem(position); - if (post != null) { - mActivity.copyLink(post.getPermalink()); - return true; - } - return false; - }); - } - + @Override void markPostRead(Post post, boolean changePostItemColor) { if (!mAccountName.equals(Account.ANONYMOUS_ACCOUNT) && !post.isRead() && mMarkPostsAsRead) { post.markAsRead(); -- cgit v1.2.3