From 7e0fa498ea343d5fbbe3a9757a4ecc4f66389649 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Thu, 26 Sep 2024 14:19:03 -0400 Subject: Continue optimizing class structures in PostRecyclerViewAdapter. --- .../adapters/PostRecyclerViewAdapter.java | 431 ++++----------------- 1 file changed, 75 insertions(+), 356 deletions(-) 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 f19c3c6e..7af77dd3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -2597,8 +2597,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { + int position = getBindingAdapterPosition(); + if (position < 0) { + return; + } + Post post = getItem(position); + if (post != null && canStartActivity) { + canStartActivity = false; + if (mDisplaySubredditName) { + Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); + intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, + post.getSubredditName()); + mActivity.startActivity(intent); + } else if (!post.isAuthorDeleted()) { + Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); + mActivity.startActivity(intent); + } + } + }); + + iconGifImageView.setOnClickListener(view -> nameTextView.performClick()); + + setOnClickListeners(upvoteButton, + scoreTextView, + downvoteButton, + commentsCountButton, + saveButton, + shareButton); + } + + void setOnClickListeners(MaterialButton upvoteButton, + TextView scoreTextView, + MaterialButton downvoteButton, + MaterialButton commentsCountButton, + MaterialButton saveButton, + MaterialButton shareButton) { upvoteButton.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { @@ -2996,6 +3053,8 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter requestListener; Post post; @@ -4033,7 +4087,15 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null && canStartActivity) { - canStartActivity = false; - if (mDisplaySubredditName) { - Intent intent = new Intent(mActivity, ViewSubredditDetailActivity.class); - intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, - post.getSubredditName()); - mActivity.startActivity(intent); - } else if (!post.isAuthorDeleted()) { - Intent intent = new Intent(mActivity, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, post.getAuthor()); - mActivity.startActivity(intent); - } - } - }); - - iconGifImageView.setOnClickListener(view -> nameTextView.performClick()); - nsfwTextView.setOnClickListener(view -> { int position = getBindingAdapterPosition(); if (position < 0) { @@ -4252,306 +4285,6 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter { - 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(); - int previousScoreTextViewColor = scoreTextView.getCurrentTextColor(); - 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)); - - 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)); - 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()); - } - }); - - commentsCountButton.setOnClickListener(view -> itemView.performClick()); - - saveButton.setOnClickListener(view -> { - if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); - return; - } - - int position = getBindingAdapterPosition(); - if (position < 0) { - return; - } - Post post = getItem(position); - if (post != null) { - 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; - }); - requestListener = new RequestListener<>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -5183,19 +4916,12 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter