From 288f1777d9b8ee1c4480a3184ebc0981f1e3494e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 12 Feb 2021 23:36:44 +0800 Subject: Use EventBus to send post list from PostFragment to ViewPostDetailActivity. --- .../activities/ViewPostDetailActivity.java | 33 +++++- .../adapters/PostRecyclerViewAdapter.java | 118 +++++++++++---------- .../NeedForPostListFromPostFragmentEvent.java | 9 ++ ...ovidePostListToViewPostDetailActivityEvent.java | 15 +++ .../infinityforreddit/fragments/PostFragment.java | 31 +++++- 5 files changed, 143 insertions(+), 63 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/NeedForPostListFromPostFragmentEvent.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java (limited to 'app/src/main') 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= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -488,7 +488,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setSubredditIconUrl(iconImageUrl); } } @@ -522,7 +522,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -637,14 +637,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -818,7 +818,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setSubredditIconUrl(iconImageUrl); } } @@ -855,7 +855,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { + if (holder.getBindingAdapterPosition() >= 0) { post.setAuthorIconUrl(iconImageUrl); } } @@ -1044,7 +1044,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { Post post = getItem(position); ((PostBaseViewHolder) holder).markPostRead(post, false); @@ -1398,7 +1398,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= 0) { Post post = getItem(position); ((PostCompactBaseViewHolder) holder).markPostRead(post, false); @@ -1624,7 +1624,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - 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 { if (canStartActivity) { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1657,7 +1658,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1683,7 +1684,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter 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 { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1720,7 +1721,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1732,7 +1733,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1781,7 +1782,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1871,7 +1872,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -1930,7 +1931,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2111,7 +2112,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2257,7 +2258,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2638,7 +2639,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2648,8 +2649,9 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2702,7 +2704,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter nameTextView.performClick()); nsfwTextView.setOnClickListener(view -> { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2713,7 +2715,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2724,7 +2726,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - int position = getAdapterPosition(); + int position = getBindingAdapterPosition(); if (position < 0) { return; } @@ -2789,7 +2791,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { - 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 posts; + + public ProvidePostListToViewPostDetailActivityEvent(long postFragmentId, ArrayList 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 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) { -- cgit v1.2.3