Hide read posts in MainActivity (there is an issue in lazy mode).

This commit is contained in:
Alex Ning 2020-12-08 15:12:56 +08:00
parent 69af53ddba
commit a6975f2489
11 changed files with 131 additions and 2 deletions

View File

@ -35,4 +35,7 @@ public interface FragmentCommunicator {
} }
void applyTheme(); void applyTheme();
default void hideReadPosts() {
}
} }

View File

@ -591,6 +591,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM: case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM:
fab.setImageResource(R.drawable.ic_random_24dp); fab.setImageResource(R.drawable.ic_random_24dp);
break; break;
case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS:
fab.setImageResource(R.drawable.ic_hide_read_posts_24dp);
break;
default: default:
fab.setImageResource(R.drawable.ic_add_day_night_24dp); fab.setImageResource(R.drawable.ic_add_day_night_24dp);
break; break;
@ -625,6 +628,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM: case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_RANDOM:
randomThing(); randomThing();
break; break;
case SharedPreferencesUtils.MAIN_ACTIVITY_BOTTOM_APP_BAR_FAB_HIDE_READ_POSTS:
if (sectionsPagerAdapter != null) {
sectionsPagerAdapter.hideReadPosts();
}
break;
default: default:
postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag()); postTypeBottomSheetFragment.show(getSupportFragmentManager(), postTypeBottomSheetFragment.getTag());
break; break;
@ -1199,6 +1207,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
randomThing(); randomThing();
break; 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); Utils.displaySortTypeInToolbar(sortType, toolbar);
} }
} }
void hideReadPosts() {
PostFragment currentFragment = getCurrentFragment();
if (currentFragment != null) {
currentFragment.hideReadPosts();
}
}
} }
} }

View File

@ -173,6 +173,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
private int mButtonTextColor; private int mButtonTextColor;
private int mPostIconAndInfoColor; private int mPostIconAndInfoColor;
private int mDividerColor; private int mDividerColor;
private int mHideReadPostsIndex = 0;
private float mScale; private float mScale;
private boolean mDisplaySubredditName; private boolean mDisplaySubredditName;
private boolean mVoteButtonsOnTheRight; private boolean mVoteButtonsOnTheRight;
@ -394,6 +395,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
Post post = getItem(position); Post post = getItem(position);
if (post != null) { if (post != null) {
if (post.isRead()) { if (post.isRead()) {
if (position < mHideReadPostsIndex) {
holder.itemView.setVisibility(View.GONE);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
params.height = 0;
params.topMargin = 0;
params.bottomMargin = 0;
holder.itemView.setLayoutParams(params);
return;
}
holder.itemView.setBackgroundTintList(ColorStateList.valueOf(mReadPostCardViewBackgroundColor)); holder.itemView.setBackgroundTintList(ColorStateList.valueOf(mReadPostCardViewBackgroundColor));
((PostBaseViewHolder) holder).titleTextView.setTextColor(mReadPostTitleColor); ((PostBaseViewHolder) holder).titleTextView.setTextColor(mReadPostTitleColor);
} }
@ -706,6 +716,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
Post post = getItem(position); Post post = getItem(position);
if (post != null) { if (post != null) {
if (post.isRead()) { if (post.isRead()) {
if (position < mHideReadPostsIndex) {
holder.itemView.setVisibility(View.GONE);
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = 0;
holder.itemView.setLayoutParams(params);
return;
}
holder.itemView.setBackgroundColor(mReadPostCardViewBackgroundColor); holder.itemView.setBackgroundColor(mReadPostCardViewBackgroundColor);
((PostCompactBaseViewHolder) holder).titleTextView.setTextColor(mReadPostTitleColor); ((PostCompactBaseViewHolder) holder).titleTextView.setTextColor(mReadPostTitleColor);
} }
@ -1189,6 +1206,18 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
mShowAbsoluteNumberOfVotes = showAbsoluteNumberOfVotes; mShowAbsoluteNumberOfVotes = showAbsoluteNumberOfVotes;
} }
public int getHideReadPostsIndex() {
return mHideReadPostsIndex;
}
public void setHideReadPostsIndex(int hideReadPostsIndex) {
mHideReadPostsIndex = hideReadPostsIndex;
}
public void prepareToHideReadPosts() {
mHideReadPostsIndex = getItemCount();
}
private boolean hasExtraRow() { private boolean hasExtraRow() {
return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS; return networkState != null && networkState.getStatus() != NetworkState.Status.SUCCESS;
} }
@ -1265,6 +1294,13 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder); super.onViewRecycled(holder);
if (holder instanceof PostBaseViewHolder) { if (holder instanceof PostBaseViewHolder) {
((PostBaseViewHolder) holder).itemView.setVisibility(View.VISIBLE);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
int marginPixel = (int) Utils.convertDpToPixel(8, mActivity);
params.topMargin = marginPixel;
params.bottomMargin = marginPixel;
holder.itemView.setLayoutParams(params);
((PostBaseViewHolder) holder).itemView.setBackgroundTintList(ColorStateList.valueOf(mCardViewBackgroundColor)); ((PostBaseViewHolder) holder).itemView.setBackgroundTintList(ColorStateList.valueOf(mCardViewBackgroundColor));
((PostBaseViewHolder) holder).titleTextView.setTextColor(mPostTitleColor); ((PostBaseViewHolder) holder).titleTextView.setTextColor(mPostTitleColor);
if (holder instanceof PostVideoAutoplayViewHolder) { if (holder instanceof PostVideoAutoplayViewHolder) {
@ -1306,6 +1342,10 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
((PostBaseViewHolder) holder).scoreTextView.setTextColor(mPostIconAndInfoColor); ((PostBaseViewHolder) holder).scoreTextView.setTextColor(mPostIconAndInfoColor);
((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN); ((PostBaseViewHolder) holder).downvoteButton.setColorFilter(mPostIconAndInfoColor, android.graphics.PorterDuff.Mode.SRC_IN);
} else if (holder instanceof PostCompactBaseViewHolder) { } else if (holder instanceof PostCompactBaseViewHolder) {
((PostCompactBaseViewHolder) holder).itemView.setVisibility(View.VISIBLE);
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
holder.itemView.setLayoutParams(params);
((PostCompactBaseViewHolder) holder).itemView.setBackgroundColor(mCardViewBackgroundColor); ((PostCompactBaseViewHolder) holder).itemView.setBackgroundColor(mCardViewBackgroundColor);
((PostCompactBaseViewHolder) holder).titleTextView.setTextColor(mPostTitleColor); ((PostCompactBaseViewHolder) holder).titleTextView.setTextColor(mPostTitleColor);
mGlide.clear(((PostCompactBaseViewHolder) holder).imageView); mGlide.clear(((PostCompactBaseViewHolder) holder).imageView);

View File

@ -25,6 +25,7 @@ public class FABMoreOptionsBottomSheetFragment extends RoundedBottomSheetDialogF
public static final int FAB_OPTION_GO_TO_SUBREDDIT = 5; public static final int FAB_OPTION_GO_TO_SUBREDDIT = 5;
public static final int FAB_OPTION_GO_TO_USER = 6; public static final int FAB_OPTION_GO_TO_USER = 6;
public static final int FAB_RANDOM = 7; public static final int FAB_RANDOM = 7;
public static final int FAB_HIDE_READ_POSTS = 8;
@BindView(R.id.submit_post_text_view_fab_more_options_bottom_sheet_fragment) @BindView(R.id.submit_post_text_view_fab_more_options_bottom_sheet_fragment)
TextView submitPostTextView; TextView submitPostTextView;
@ -42,6 +43,8 @@ public class FABMoreOptionsBottomSheetFragment extends RoundedBottomSheetDialogF
TextView goToUserTextView; TextView goToUserTextView;
@BindView(R.id.random_text_view_fab_more_options_bottom_sheet_fragment) @BindView(R.id.random_text_view_fab_more_options_bottom_sheet_fragment)
TextView randomTextView; TextView randomTextView;
@BindView(R.id.hide_read_posts_text_view_fab_more_options_bottom_sheet_fragment)
TextView hideReadPostsTextView;
private FABOptionSelectionCallback activity; private FABOptionSelectionCallback activity;
public FABMoreOptionsBottomSheetFragment() { public FABMoreOptionsBottomSheetFragment() {
@ -96,6 +99,11 @@ public class FABMoreOptionsBottomSheetFragment extends RoundedBottomSheetDialogF
dismiss(); dismiss();
}); });
hideReadPostsTextView.setOnClickListener(view -> {
activity.fabOptionSelected(FAB_HIDE_READ_POSTS);
dismiss();
});
return rootView; return rootView;
} }

View File

@ -135,6 +135,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS"; private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS";
private static final String READ_POST_LIST_STATE = "RPLS"; private static final String READ_POST_LIST_STATE = "RPLS";
private static final String SUBREDDIT_FILTER_LIST_STATE = "SFLS"; 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) @BindView(R.id.swipe_refresh_layout_post_fragment)
SwipeRefreshLayout mSwipeRefreshLayout; SwipeRefreshLayout mSwipeRefreshLayout;
@ -370,6 +371,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mSwipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); mSwipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true));
mSwipeRefreshLayout.setOnRefreshListener(this::refresh); mSwipeRefreshLayout.setOnRefreshListener(this::refresh);
int hideReadPostsIndex = 0;
if (savedInstanceState != null) { if (savedInstanceState != null) {
int recyclerViewPosition = savedInstanceState.getInt(RECYCLER_VIEW_POSITION_STATE); int recyclerViewPosition = savedInstanceState.getInt(RECYCLER_VIEW_POSITION_STATE);
if (recyclerViewPosition > 0) { if (recyclerViewPosition > 0) {
@ -379,6 +381,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE);
readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE);
subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE); subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE);
hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0);
} }
mPostRecyclerView.setOnTouchListener((view, motionEvent) -> { mPostRecyclerView.setOnTouchListener((view, motionEvent) -> {
@ -653,6 +656,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
}); });
} }
mAdapter.setHideReadPostsIndex(hideReadPostsIndex);
if (activity instanceof ActivityToolbarInterface) { if (activity instanceof ActivityToolbarInterface) {
((ActivityToolbarInterface) activity).displaySortType(); ((ActivityToolbarInterface) activity).displaySortType();
} }
@ -976,6 +981,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode);
outState.putParcelableArrayList(READ_POST_LIST_STATE, readPosts); outState.putParcelableArrayList(READ_POST_LIST_STATE, readPosts);
outState.putParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE, subredditFilterList); outState.putParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE, subredditFilterList);
if (mAdapter != null) {
outState.putInt(HIDE_READ_POSTS_INDEX_STATE, mAdapter.getHideReadPostsIndex());
}
if (mLinearLayoutManager != null) { if (mLinearLayoutManager != null) {
outState.putInt(RECYCLER_VIEW_POSITION_STATE, mLinearLayoutManager.findFirstVisibleItemPosition()); outState.putInt(RECYCLER_VIEW_POSITION_STATE, mLinearLayoutManager.findFirstVisibleItemPosition());
} else if (mStaggeredGridLayoutManager != null) { } else if (mStaggeredGridLayoutManager != null) {
@ -1143,6 +1151,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mFetchPostInfoTextView.setTextColor(customThemeWrapper.getSecondaryTextColor()); mFetchPostInfoTextView.setTextColor(customThemeWrapper.getSecondaryTextColor());
} }
@Override
public void hideReadPosts() {
if (mAdapter != null) {
mAdapter.prepareToHideReadPosts();
refreshAdapter();
}
}
@Subscribe @Subscribe
public void onPostUpdateEvent(PostUpdateEventToPostList event) { public void onPostUpdateEvent(PostUpdateEventToPostList event) {
PagedList<Post> posts = mAdapter.getCurrentList(); PagedList<Post> posts = mAdapter.getCurrentList();

View File

@ -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_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_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_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_HOME = 0;
public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS = 1; public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS = 1;
public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX = 2; public static final int OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX = 2;

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFF"
android:pathData="M12,6c3.79,0 7.17,2.13 8.82,5.5 -0.59,1.22 -1.42,2.27 -2.41,3.12l1.41,1.41c1.39,-1.23 2.49,-2.77 3.18,-4.53C21.27,7.11 17,4 12,4c-1.27,0 -2.49,0.2 -3.64,0.57l1.65,1.65C10.66,6.09 11.32,6 12,6zM10.93,7.14L13,9.21c0.57,0.25 1.03,0.71 1.28,1.28l2.07,2.07c0.08,-0.34 0.14,-0.7 0.14,-1.07C16.5,9.01 14.48,7 12,7c-0.37,0 -0.72,0.05 -1.07,0.14zM2.01,3.87l2.68,2.68C3.06,7.83 1.77,9.53 1,11.5 2.73,15.89 7,19 12,19c1.52,0 2.98,-0.29 4.32,-0.82l3.42,3.42 1.41,-1.41L3.42,2.45 2.01,3.87zM9.51,11.37l2.61,2.61c-0.04,0.01 -0.08,0.02 -0.12,0.02 -1.38,0 -2.5,-1.12 -2.5,-2.5 0,-0.05 0.01,-0.08 0.01,-0.13zM6.11,7.97l1.75,1.75c-0.23,0.55 -0.36,1.15 -0.36,1.78 0,2.48 2.02,4.5 4.5,4.5 0.63,0 1.23,-0.13 1.77,-0.36l0.98,0.98c-0.88,0.24 -1.8,0.38 -2.75,0.38 -3.79,0 -7.17,-2.13 -8.82,-5.5 0.7,-1.43 1.72,-2.61 2.93,-3.53z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M12,6c3.79,0 7.17,2.13 8.82,5.5 -0.59,1.22 -1.42,2.27 -2.41,3.12l1.41,1.41c1.39,-1.23 2.49,-2.77 3.18,-4.53C21.27,7.11 17,4 12,4c-1.27,0 -2.49,0.2 -3.64,0.57l1.65,1.65C10.66,6.09 11.32,6 12,6zM10.93,7.14L13,9.21c0.57,0.25 1.03,0.71 1.28,1.28l2.07,2.07c0.08,-0.34 0.14,-0.7 0.14,-1.07C16.5,9.01 14.48,7 12,7c-0.37,0 -0.72,0.05 -1.07,0.14zM2.01,3.87l2.68,2.68C3.06,7.83 1.77,9.53 1,11.5 2.73,15.89 7,19 12,19c1.52,0 2.98,-0.29 4.32,-0.82l3.42,3.42 1.41,-1.41L3.42,2.45 2.01,3.87zM9.51,11.37l2.61,2.61c-0.04,0.01 -0.08,0.02 -0.12,0.02 -1.38,0 -2.5,-1.12 -2.5,-2.5 0,-0.05 0.01,-0.08 0.01,-0.13zM6.11,7.97l1.75,1.75c-0.23,0.55 -0.36,1.15 -0.36,1.78 0,2.48 2.02,4.5 4.5,4.5 0.63,0 1.23,-0.13 1.77,-0.36l0.98,0.98c-0.88,0.24 -1.8,0.38 -2.75,0.38 -3.79,0 -7.17,-2.13 -8.82,-5.5 0.7,-1.43 1.72,-2.61 2.93,-3.53z"/>
</vector>

View File

@ -163,6 +163,25 @@
android:focusable="true" android:focusable="true"
android:background="?attr/selectableItemBackground" /> android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/hide_read_posts_text_view_fab_more_options_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:text="@string/hide_read_posts"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
app:drawableStartCompat="@drawable/ic_hide_read_posts_24dp"
android:drawablePadding="48dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
</LinearLayout> </LinearLayout>
</androidx.core.widget.NestedScrollView> </androidx.core.widget.NestedScrollView>

View File

@ -260,6 +260,7 @@
<item>Go to Subreddit</item> <item>Go to Subreddit</item>
<item>Go to User</item> <item>Go to User</item>
<item>Random</item> <item>Random</item>
<item>Hide Read Posts</item>
</string-array> </string-array>
<string-array name="settings_swipe_action_threshold"> <string-array name="settings_swipe_action_threshold">

View File

@ -929,8 +929,8 @@
<string name="dismiss">Dismiss</string> <string name="dismiss">Dismiss</string>
<string name="leave">Leave</string> <string name="leave">Leave</string>
<string name="go_to_subreddit">Go to subreddit</string> <string name="go_to_subreddit">Go to Subreddit</string>
<string name="go_to_user">Go to user</string> <string name="go_to_user">Go to User</string>
<string name="go_to_thing_hint">Name</string> <string name="go_to_thing_hint">Name</string>
<string name="random">Random</string> <string name="random">Random</string>
<string name="random_subreddit">Random Subreddit</string> <string name="random_subreddit">Random Subreddit</string>
@ -941,4 +941,6 @@
<string name="select_video_quality">Select Video Quality</string> <string name="select_video_quality">Select Video Quality</string>
<string name="hide_read_posts">Hide Read Posts</string>
</resources> </resources>