diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-02-12 15:36:44 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-02-12 15:36:44 +0000 |
commit | 288f1777d9b8ee1c4480a3184ebc0981f1e3494e (patch) | |
tree | 72df02850b7b2c2d898633fd420b773bca3f2c06 /app/src/main/java/ml | |
parent | a5e70b977506342684daa3b036f939462b6d85c7 (diff) | |
download | infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.tar infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.tar.gz infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.tar.bz2 infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.tar.lz infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.tar.xz infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.tar.zst infinity-for-reddit-288f1777d9b8ee1c4480a3184ebc0981f1e3494e.zip |
Use EventBus to send post list from PostFragment to ViewPostDetailActivity.
Diffstat (limited to 'app/src/main/java/ml')
5 files changed, 143 insertions, 63 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 3e395af8..0b6fb97e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -54,6 +54,8 @@ import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment; import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.NeedForPostListFromPostFragmentEvent; +import ml.docilealligator.infinityforreddit.events.ProvidePostListToViewPostDetailActivityEvent; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.PostFragment; import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; @@ -63,13 +65,13 @@ import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class ViewPostDetailActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback, SortTypeSelectionCallback, ActivityToolbarInterface { - public static final String EXTRA_POST_LIST = "EPL"; public static final String EXTRA_POST_DATA = "EPD"; public static final String EXTRA_POST_ID = "EPI"; public static final String EXTRA_POST_LIST_POSITION = "EPLP"; public static final String EXTRA_SINGLE_COMMENT_ID = "ESCI"; public static final String EXTRA_MESSAGE_FULLNAME = "ENI"; public static final String EXTRA_NEW_ACCOUNT_NAME = "ENAN"; + public static final String EXTRA_POST_FRAGMENT_ID = "EPFI"; public static final int EDIT_COMMENT_REQUEST_CODE = 3; public static final int GIVE_AWARD_REQUEST_CODE = 100; @State @@ -105,6 +107,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private FragmentManager fragmentManager; private SlidrInterface mSlidrInterface; private SectionsPagerAdapter sectionsPagerAdapter; + private long postFragmentId; + private int postListPosition = -1; private int orientation; private boolean mVolumeKeysNavigateComments; @@ -157,10 +161,15 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } + postFragmentId = getIntent().getLongExtra(EXTRA_POST_FRAGMENT_ID, -1); + if (posts == null && postFragmentId > 0) { + EventBus.getDefault().post(new NeedForPostListFromPostFragmentEvent(postFragmentId)); + } + + fragmentManager = getSupportFragmentManager(); if (savedInstanceState == null) { - posts = getIntent().getParcelableArrayListExtra(EXTRA_POST_LIST); post = getIntent().getParcelableExtra(EXTRA_POST_DATA); } @@ -296,6 +305,17 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } } + @Subscribe + public void onProvidePostListToViewPostDetailActivityEvent(ProvidePostListToViewPostDetailActivityEvent event) { + if (event.postFragmentId == postFragmentId && posts == null) { + posts = event.posts; + if (sectionsPagerAdapter != null) { + if (postListPosition > 0) + sectionsPagerAdapter.notifyDataSetChanged(); + } + } + } + @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (item.getItemId() == android.R.id.home) { @@ -415,12 +435,19 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS ViewPostDetailFragment fragment = new ViewPostDetailFragment(); Bundle bundle = new Bundle(); if (posts != null) { - bundle.putParcelable(ViewPostDetailFragment.EXTRA_POST_DATA, posts.get(position)); + if (postListPosition == position && post != null) { + bundle.putParcelable(ViewPostDetailFragment.EXTRA_POST_DATA, post); + bundle.putInt(ViewPostDetailFragment.EXTRA_POST_LIST_POSITION, position); + } else { + bundle.putParcelable(ViewPostDetailFragment.EXTRA_POST_DATA, posts.get(position)); + bundle.putInt(ViewPostDetailFragment.EXTRA_POST_LIST_POSITION, position); + } } else { if (post == null) { bundle.putString(ViewPostDetailFragment.EXTRA_POST_ID, getIntent().getStringExtra(EXTRA_POST_ID)); } else { bundle.putParcelable(ViewPostDetailFragment.EXTRA_POST_DATA, post); + bundle.putInt(ViewPostDetailFragment.EXTRA_POST_LIST_POSITION, position); } } fragment.setArguments(bundle); 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 60043539..5418a482 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -454,7 +454,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView .into(((PostBaseViewHolder) holder).iconGifImageView); } - if (holder.getAdapterPosition() >= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -488,7 +488,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView .into(((PostBaseViewHolder) holder).iconGifImageView); } - if (holder.getAdapterPosition() >= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setSubredditIconUrl(iconImageUrl); } } @@ -522,7 +522,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView .into(((PostBaseViewHolder) holder).iconGifImageView); } - if (holder.getAdapterPosition() >= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -637,14 +637,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView post.setVideoDownloadUrl(mp4); post.setVideoUrl(webm); post.setLoadGfyOrRedgifsVideoSuccess(true); - if (position == holder.getAdapterPosition()) { + if (position == holder.getBindingAdapterPosition()) { ((PostVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(post.getVideoUrl())); } } @Override public void failed(int errorCode) { - if (position == holder.getAdapterPosition()) { + if (position == holder.getBindingAdapterPosition()) { ((PostVideoAutoplayViewHolder) holder).errorLoadingGfycatImageView.setVisibility(View.VISIBLE); } } @@ -739,7 +739,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostTextTypeViewHolder) holder).contentTextView.setText(post.getSelfTextPlainTrimmed()); } } - mCallback.currentlyBindItem(holder.getAdapterPosition()); + mCallback.currentlyBindItem(holder.getBindingAdapterPosition()); } } else if (holder instanceof PostCompactBaseViewHolder) { Post post = getItem(position); @@ -784,7 +784,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView .into(((PostCompactBaseViewHolder) holder).iconGifImageView); } - if (holder.getAdapterPosition() >= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -818,7 +818,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView .into(((PostCompactBaseViewHolder) holder).iconGifImageView); } - if (holder.getAdapterPosition() >= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setSubredditIconUrl(iconImageUrl); } } @@ -855,7 +855,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView .into(((PostCompactBaseViewHolder) holder).iconGifImageView); } - if (holder.getAdapterPosition() >= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -1044,7 +1044,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostCompactBaseViewHolder) holder).saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); } - mCallback.currentlyBindItem(holder.getAdapterPosition()); + mCallback.currentlyBindItem(holder.getBindingAdapterPosition()); } } } @@ -1343,7 +1343,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView super.onViewRecycled(holder); if (holder instanceof PostBaseViewHolder) { if (mMarkPostsAsReadOnScroll) { - int position = holder.getAdapterPosition(); + int position = holder.getBindingAdapterPosition(); if (position < super.getItemCount() && position >= 0) { Post post = getItem(position); ((PostBaseViewHolder) holder).markPostRead(post, false); @@ -1398,7 +1398,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); } else if (holder instanceof PostCompactBaseViewHolder) { if (mMarkPostsAsReadOnScroll) { - int position = holder.getAdapterPosition(); + int position = holder.getBindingAdapterPosition(); if (position < super.getItemCount() && position >= 0) { Post post = getItem(position); ((PostCompactBaseViewHolder) holder).markPostRead(post, false); @@ -1624,7 +1624,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView shareButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); cardView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position >= 0 && canStartActivity) { Post post = getItem(position); if (post != null) { @@ -1632,8 +1632,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView canStartActivity = false; Intent intent = new Intent(mActivity, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST, mFragment.getPostList()); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, getAdapterPosition()); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, getBindingAdapterPosition()); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_FRAGMENT_ID, mFragment.getPostFragmentId()); mActivity.startActivity(intent); } } @@ -1641,7 +1642,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView userTextView.setOnClickListener(view -> { if (canStartActivity) { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1657,7 +1658,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (mDisplaySubredditName) { subredditTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1683,7 +1684,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView iconGifImageView.setOnClickListener(view -> subredditTextView.performClick()); } else { subredditTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1710,7 +1711,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (!(mActivity instanceof FilteredPostsActivity)) { nsfwTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1720,7 +1721,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } }); typeTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1732,7 +1733,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } upvoteButton.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1781,7 +1782,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - int currentPosition = getAdapterPosition(); + int currentPosition = getBindingAdapterPosition(); if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { post.setVoteType(1); if (currentPosition == position) { @@ -1808,7 +1809,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView public void onVoteThingFail(int position1) { Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); post.setVoteType(previousVoteType); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + previousVoteType)); upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); @@ -1817,12 +1818,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } - }, post.getFullName(), newVoteType, getAdapterPosition()); + }, post.getFullName(), newVoteType, getBindingAdapterPosition()); } }); downvoteButton.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1871,7 +1872,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - int currentPosition = getAdapterPosition(); + int currentPosition = getBindingAdapterPosition(); if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { post.setVoteType(-1); if (currentPosition == position) { @@ -1898,7 +1899,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView public void onVoteThingFail(int position1) { Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); post.setVoteType(previousVoteType); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + previousVoteType)); upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); @@ -1907,12 +1908,12 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } - }, post.getFullName(), newVoteType, getAdapterPosition()); + }, post.getFullName(), newVoteType, getBindingAdapterPosition()); } }); saveButton.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1930,7 +1931,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void success() { post.setSaved(false); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); } Toast.makeText(mActivity, R.string.post_unsaved_success, Toast.LENGTH_SHORT).show(); @@ -1940,7 +1941,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void failed() { post.setSaved(true); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); } Toast.makeText(mActivity, R.string.post_unsaved_failed, Toast.LENGTH_SHORT).show(); @@ -1954,7 +1955,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void success() { post.setSaved(true); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); } Toast.makeText(mActivity, R.string.post_saved_success, Toast.LENGTH_SHORT).show(); @@ -1964,7 +1965,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void failed() { post.setSaved(false); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); } Toast.makeText(mActivity, R.string.post_saved_failed, Toast.LENGTH_SHORT).show(); @@ -1976,7 +1977,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView }); shareButton.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2111,7 +2112,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView }); fullscreenButton.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2257,7 +2258,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public int getPlayerOrder() { - return getAdapterPosition(); + return getBindingAdapterPosition(); } } @@ -2356,7 +2357,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView errorTextView.setTextColor(mPrimaryTextColor); imageView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2638,7 +2639,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView noPreviewLinkImageFrameLayout.setClipToOutline(true); itemView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2648,8 +2649,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView canStartActivity = false; Intent intent = new Intent(mActivity, ViewPostDetailActivity.class); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST, mFragment.getPostList()); - intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, getAdapterPosition()); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, post); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_LIST_POSITION, getBindingAdapterPosition()); + intent.putExtra(ViewPostDetailActivity.EXTRA_POST_FRAGMENT_ID, mFragment.getPostFragmentId()); mActivity.startActivity(intent); } }); @@ -2672,7 +2674,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView }); nameTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2702,7 +2704,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView iconGifImageView.setOnClickListener(view -> nameTextView.performClick()); nsfwTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2713,7 +2715,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView }); typeTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2724,7 +2726,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView }); imageView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2789,7 +2791,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView return; } - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2833,7 +2835,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - int currentPosition = getAdapterPosition(); + int currentPosition = getBindingAdapterPosition(); if (newVoteType.equals(APIUtils.DIR_UPVOTE)) { post.setVoteType(1); if (currentPosition == position) { @@ -2860,7 +2862,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView public void onVoteThingFail(int position1) { Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); post.setVoteType(previousVoteType); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + previousVoteType)); upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); @@ -2869,7 +2871,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } - }, post.getFullName(), newVoteType, getAdapterPosition()); + }, post.getFullName(), newVoteType, getBindingAdapterPosition()); } }); @@ -2879,7 +2881,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView return; } - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2923,7 +2925,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView VoteThing.voteThing(mActivity, mOauthRetrofit, mAccessToken, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { - int currentPosition = getAdapterPosition(); + int currentPosition = getBindingAdapterPosition(); if (newVoteType.equals(APIUtils.DIR_DOWNVOTE)) { post.setVoteType(-1); if (currentPosition == position) { @@ -2951,7 +2953,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView public void onVoteThingFail(int position1) { Toast.makeText(mActivity, R.string.vote_failed, Toast.LENGTH_SHORT).show(); post.setVoteType(previousVoteType); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { scoreTextView.setText(Utils.getNVotes(mShowAbsoluteNumberOfVotes, post.getScore() + previousVoteType)); upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); @@ -2960,7 +2962,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } - }, post.getFullName(), newVoteType, getAdapterPosition()); + }, post.getFullName(), newVoteType, getBindingAdapterPosition()); } }); @@ -2970,7 +2972,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView return; } - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2983,7 +2985,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void success() { post.setSaved(false); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); } Toast.makeText(mActivity, R.string.post_unsaved_success, Toast.LENGTH_SHORT).show(); @@ -2993,7 +2995,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void failed() { post.setSaved(true); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); } Toast.makeText(mActivity, R.string.post_unsaved_failed, Toast.LENGTH_SHORT).show(); @@ -3007,7 +3009,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void success() { post.setSaved(true); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp); } Toast.makeText(mActivity, R.string.post_saved_success, Toast.LENGTH_SHORT).show(); @@ -3017,7 +3019,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public void failed() { post.setSaved(false); - if (getAdapterPosition() == position) { + if (getBindingAdapterPosition() == position) { saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp); } Toast.makeText(mActivity, R.string.post_saved_failed, Toast.LENGTH_SHORT).show(); @@ -3029,7 +3031,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView }); shareButton.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/NeedForPostListFromPostFragmentEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/NeedForPostListFromPostFragmentEvent.java new file mode 100644 index 00000000..06abcfd5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/NeedForPostListFromPostFragmentEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.events; + +public class NeedForPostListFromPostFragmentEvent { + public long postFragmentTimeId; + + public NeedForPostListFromPostFragmentEvent(long postFragmentId) { + this.postFragmentTimeId = postFragmentId; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java new file mode 100644 index 00000000..40fc7bab --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java @@ -0,0 +1,15 @@ +package ml.docilealligator.infinityforreddit.events; + +import java.util.ArrayList; + +import ml.docilealligator.infinityforreddit.post.Post; + +public class ProvidePostListToViewPostDetailActivityEvent { + public long postFragmentId; + public ArrayList<Post> posts; + + public ProvidePostListToViewPostDetailActivityEvent(long postFragmentId, ArrayList<Post> posts) { + this.postFragmentId = postFragmentId; + this.posts = posts; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index eb69cbee..140a36a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -52,6 +52,7 @@ import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; import java.util.Locale; +import java.util.Random; import java.util.concurrent.Executor; import javax.inject.Inject; @@ -101,7 +102,9 @@ import ml.docilealligator.infinityforreddit.events.ChangeTimeFormatEvent; import ml.docilealligator.infinityforreddit.events.ChangeVibrateWhenActionTriggeredEvent; import ml.docilealligator.infinityforreddit.events.ChangeVideoAutoplayEvent; import ml.docilealligator.infinityforreddit.events.ChangeVoteButtonsPositionEvent; +import ml.docilealligator.infinityforreddit.events.NeedForPostListFromPostFragmentEvent; import ml.docilealligator.infinityforreddit.events.PostUpdateEventToPostList; +import ml.docilealligator.infinityforreddit.events.ProvidePostListToViewPostDetailActivityEvent; import ml.docilealligator.infinityforreddit.events.ShowDividerInCompactLayoutPreferenceEvent; import ml.docilealligator.infinityforreddit.events.ShowThumbnailOnTheRightInCompactLayoutEvent; import ml.docilealligator.infinityforreddit.post.Post; @@ -138,6 +141,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private static final String READ_POST_LIST_STATE = "RPLS"; private static final String HIDE_READ_POSTS_INDEX_STATE = "HRPIS"; private static final String POST_FILTER_STATE = "PFS"; + private static final String POST_FRAGMENT_ID_STATE = "PFIS"; @BindView(R.id.swipe_refresh_layout_post_fragment) SwipeRefreshLayout mSwipeRefreshLayout; @@ -192,6 +196,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private AppCompatActivity activity; private LinearLayoutManager mLinearLayoutManager; private StaggeredGridLayoutManager mStaggeredGridLayoutManager; + private long postFragmentId; private int postType; private boolean isInLazyMode = false; private boolean isLazyModePaused = false; @@ -390,8 +395,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0); postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE); + postFragmentId = savedInstanceState.getLong(POST_FRAGMENT_ID_STATE); } else { postFilter = getArguments().getParcelable(EXTRA_FILTER); + postFragmentId = System.currentTimeMillis() + new Random().nextInt(1000); } mPostRecyclerView.setOnTouchListener((view, motionEvent) -> { @@ -428,6 +435,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (postType == PostDataSource.TYPE_SEARCH) { subredditName = getArguments().getString(EXTRA_NAME); query = getArguments().getString(EXTRA_QUERY); + if (savedInstanceState == null) { + postFragmentId += query.hashCode(); + } usage = PostFilterUsage.SEARCH_TYPE; nameOfUsage = PostFilterUsage.NO_USAGE; @@ -481,6 +491,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { subredditName = getArguments().getString(EXTRA_NAME); + if (savedInstanceState == null) { + postFragmentId += subredditName.hashCode(); + } usage = PostFilterUsage.SUBREDDIT_TYPE; nameOfUsage = subredditName; @@ -545,6 +558,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { multiRedditPath = getArguments().getString(EXTRA_NAME); + if (savedInstanceState == null) { + postFragmentId += multiRedditPath.hashCode(); + } usage = PostFilterUsage.MULTIREDDIT_TYPE; nameOfUsage = multiRedditPath; @@ -610,6 +626,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else if (postType == PostDataSource.TYPE_USER) { username = getArguments().getString(EXTRA_USER_NAME); where = getArguments().getString(EXTRA_USER_WHERE); + if (savedInstanceState == null) { + postFragmentId += username.hashCode(); + } usage = PostFilterUsage.USER_TYPE; nameOfUsage = username; @@ -1061,8 +1080,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } - public ArrayList<Post> getPostList() { - return new ArrayList<>(mPostViewModel.getPosts().getValue()); + public long getPostFragmentId() { + return postFragmentId; } @Override @@ -1087,6 +1106,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]); } outState.putParcelable(POST_FILTER_STATE, postFilter); + outState.putLong(POST_FRAGMENT_ID_STATE, postFragmentId); } @Override @@ -1575,6 +1595,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { initializeSwipeActionDrawable(); } + @Subscribe + public void onNeedForPostListFromPostRecyclerViewAdapterEvent(NeedForPostListFromPostFragmentEvent event) { + if (postFragmentId == event.postFragmentTimeId) { + EventBus.getDefault().post(new ProvidePostListToViewPostDetailActivityEvent(postFragmentId, new ArrayList<>(mPostViewModel.getPosts().getValue()))); + } + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { |