From b2aff0c04738135a01838493655832578c5f24c4 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 13 Feb 2021 18:27:40 +0800 Subject: Fix updating flair in ViewPostDetailFragment. --- .../activities/ViewPostDetailActivity.java | 13 +------------ .../bottomsheetfragments/FlairBottomSheetFragment.java | 12 +++++++++++- .../infinityforreddit/events/FlairSelectedEvent.java | 13 +++++++++++++ .../infinityforreddit/fragments/ViewPostDetailFragment.java | 12 ++++++++++++ 4 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/events/FlairSelectedEvent.java 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 0b6fb97e..b4efef1f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -44,14 +44,12 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; -import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; 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; @@ -62,8 +60,7 @@ import ml.docilealligator.infinityforreddit.fragments.ViewPostDetailFragment; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -public class ViewPostDetailActivity extends BaseActivity implements FlairBottomSheetFragment.FlairSelectionCallback, - SortTypeSelectionCallback, ActivityToolbarInterface { +public class ViewPostDetailActivity extends BaseActivity implements SortTypeSelectionCallback, ActivityToolbarInterface { public static final String EXTRA_POST_DATA = "EPD"; public static final String EXTRA_POST_ID = "EPI"; @@ -386,14 +383,6 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS return super.onKeyDown(keyCode, event); } - @Override - public void flairSelected(Flair flair) { - ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); - if (fragment != null) { - fragment.changeFlair(flair); - } - } - @Override public void sortTypeSelected(SortType sortType) { ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/FlairBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/FlairBottomSheetFragment.java index b67a04c9..d8b39b7e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/FlairBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/FlairBottomSheetFragment.java @@ -17,6 +17,8 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.bottomsheet.BottomSheetDialogFragment; +import org.greenrobot.eventbus.EventBus; + import java.util.ArrayList; import javax.inject.Inject; @@ -30,6 +32,7 @@ import ml.docilealligator.infinityforreddit.FetchFlairs; import ml.docilealligator.infinityforreddit.Flair; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.events.FlairSelectedEvent; import retrofit2.Retrofit; @@ -40,6 +43,7 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { public static final String EXTRA_ACCESS_TOKEN = "EAT"; public static final String EXTRA_SUBREDDIT_NAME = "ESN"; + public static final String EXTRA_VIEW_POST_DETAIL_FRAGMENT_ID = "EPFI"; @BindView(R.id.progress_bar_flair_bottom_sheet_fragment) ProgressBar progressBar; @BindView(R.id.error_text_view_flair_bottom_sheet_fragment) @@ -72,8 +76,14 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { ((Infinity) mActivity.getApplication()).getAppComponent().inject(this); + long viewPostFragmentId = getArguments().getLong(EXTRA_VIEW_POST_DETAIL_FRAGMENT_ID, -1); mAdapter = new FlairBottomSheetRecyclerViewAdapter(mActivity, mCustomThemeWrapper, flair -> { - ((FlairSelectionCallback) mActivity).flairSelected(flair); + if (viewPostFragmentId <= 0) { + //PostXXXActivity + ((FlairSelectionCallback) mActivity).flairSelected(flair); + } else { + EventBus.getDefault().post(new FlairSelectedEvent(viewPostFragmentId, flair)); + } dismiss(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/FlairSelectedEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/FlairSelectedEvent.java new file mode 100644 index 00000000..4ff85a89 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/FlairSelectedEvent.java @@ -0,0 +1,13 @@ +package ml.docilealligator.infinityforreddit.events; + +import ml.docilealligator.infinityforreddit.Flair; + +public class FlairSelectedEvent { + public long viewPostDetailFragmentId; + public Flair flair; + + public FlairSelectedEvent(long viewPostDetailFragmentId, Flair flair) { + this.viewPostDetailFragmentId = viewPostDetailFragmentId; + this.flair = flair; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index f4f6e49c..b41a7ec0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -88,6 +88,7 @@ import ml.docilealligator.infinityforreddit.customviews.CustomToroContainer; import ml.docilealligator.infinityforreddit.events.ChangeNSFWBlurEvent; import ml.docilealligator.infinityforreddit.events.ChangeNetworkStatusEvent; import ml.docilealligator.infinityforreddit.events.ChangeSpoilerBlurEvent; +import ml.docilealligator.infinityforreddit.events.FlairSelectedEvent; import ml.docilealligator.infinityforreddit.events.PostUpdateEventToDetailActivity; import ml.docilealligator.infinityforreddit.events.PostUpdateEventToPostList; import ml.docilealligator.infinityforreddit.message.ReadMessage; @@ -191,6 +192,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic String sortType; @State boolean mRespectSubredditRecommendedSortType; + @State + long viewPostDetailFragmentId; private ViewPostDetailActivity activity; private RequestManager mGlide; private Locale mLocale; @@ -262,6 +265,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mExpandChildren = !mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_TOP_LEVEL_COMMENTS_FIRST, false); if (savedInstanceState == null) { mRespectSubredditRecommendedSortType = mSharedPreferences.getBoolean(SharedPreferencesUtils.RESPECT_SUBREDDIT_RECOMMENDED_COMMENT_SORT_TYPE, false); + viewPostDetailFragmentId = System.currentTimeMillis(); } mGlide = Glide.with(this); @@ -915,6 +919,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic Bundle bundle = new Bundle(); bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName()); + bundle.putLong(FlairBottomSheetFragment.EXTRA_VIEW_POST_DETAIL_FRAGMENT_ID, viewPostDetailFragmentId); flairBottomSheetFragment.setArguments(bundle); flairBottomSheetFragment.show(activity.getSupportFragmentManager(), flairBottomSheetFragment.getTag()); return true; @@ -1724,6 +1729,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } } + @Subscribe + public void onFlairSelectedEvent(FlairSelectedEvent event) { + if (event.viewPostDetailFragmentId == viewPostDetailFragmentId) { + changeFlair(event.flair); + } + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); -- cgit v1.2.3