From a6975f2489119a0e0cc278fcdee8cead97978b14 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 8 Dec 2020 15:12:56 +0800 Subject: Hide read posts in MainActivity (there is an issue in lazy mode). --- .../infinityforreddit/FragmentCommunicator.java | 3 ++ .../infinityforreddit/activities/MainActivity.java | 21 ++++++++++++ .../adapters/PostRecyclerViewAdapter.java | 40 ++++++++++++++++++++++ .../FABMoreOptionsBottomSheetFragment.java | 8 +++++ .../infinityforreddit/fragments/PostFragment.java | 16 +++++++++ .../utils/SharedPreferencesUtils.java | 1 + 6 files changed, 89 insertions(+) (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index 07a18a72..3fb00c2a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -35,4 +35,7 @@ public interface FragmentCommunicator { } void applyTheme(); + + default void hideReadPosts() { + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index c963cf84..9219de22 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -591,6 +591,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM: fab.setImageResource(R.drawable.ic_random_24dp); break; + case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS: + fab.setImageResource(R.drawable.ic_hide_read_posts_24dp); + break; default: fab.setImageResource(R.drawable.ic_add_day_night_24dp); break; @@ -625,6 +628,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM: randomThing(); break; + case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS: + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.hideReadPosts(); + } + break; default: postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()); break; @@ -1199,6 +1207,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb randomThing(); break; } + case FABMoreOptionsBottomSheetFragment.FAB_HIDE_READ_POSTS: { + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.hideReadPosts(); + } + break; + } } } @@ -1523,5 +1537,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb Utils.displaySortTypeInToolbar(sortType, toolbar); } } + + void hideReadPosts() { + PostFragment currentFragment = getCurrentFragment(); + if (currentFragment != null) { + currentFragment.hideReadPosts(); + } + } } } 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 76df281b..9d9e890d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -173,6 +173,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { + activity.fabOptionSelected(FAB_HIDE_READ_POSTS); + dismiss(); + }); + return rootView; } 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 6686e3ff..c682e181 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -135,6 +135,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS"; private static final String READ_POST_LIST_STATE = "RPLS"; private static final String SUBREDDIT_FILTER_LIST_STATE = "SFLS"; + private static final String HIDE_READ_POSTS_INDEX_STATE = "HRPIS"; @BindView(R.id.swipe_refresh_layout_post_fragment) SwipeRefreshLayout mSwipeRefreshLayout; @@ -370,6 +371,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mSwipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); mSwipeRefreshLayout.setOnRefreshListener(this::refresh); + int hideReadPostsIndex = 0; if (savedInstanceState != null) { int recyclerViewPosition = savedInstanceState.getInt(RECYCLER_VIEW_POSITION_STATE); if (recyclerViewPosition > 0) { @@ -379,6 +381,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE); + hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0); } mPostRecyclerView.setOnTouchListener((view, motionEvent) -> { @@ -653,6 +656,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } + mAdapter.setHideReadPostsIndex(hideReadPostsIndex); + if (activity instanceof ActivityToolbarInterface) { ((ActivityToolbarInterface) activity).displaySortType(); } @@ -976,6 +981,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putParcelableArrayList(READ_POST_LIST_STATE, readPosts); outState.putParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE, subredditFilterList); + if (mAdapter != null) { + outState.putInt(HIDE_READ_POSTS_INDEX_STATE, mAdapter.getHideReadPostsIndex()); + } if (mLinearLayoutManager != null) { outState.putInt(RECYCLER_VIEW_POSITION_STATE, mLinearLayoutManager.findFirstVisibleItemPosition()); } else if (mStaggeredGridLayoutManager != null) { @@ -1143,6 +1151,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mFetchPostInfoTextView.setTextColor(customThemeWrapper.getSecondaryTextColor()); } + @Override + public void hideReadPosts() { + if (mAdapter != null) { + mAdapter.prepareToHideReadPosts(); + refreshAdapter(); + } + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToPostList event) { PagedList posts = mAdapter.getCurrentList(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index a5ba50de..44bf874f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -194,6 +194,7 @@ public class SharedPreferencesUtils { public static final int MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_GO_TO_SUBREDDIT = 5; public static final int MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_GO_TO_USER = 6; public static final int MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM = 7; + public static final int MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS = 8; public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_HOME = 0; public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS = 1; public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX = 2; -- cgit v1.2.3