From 4fa3fd8538797ccffa3961f66cb5117f89c6db3c Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Fri, 4 Mar 2016 19:08:02 -0500 Subject: [PATCH 1/7] Hide the "new posts" message when the reader list is scrolled --- .../android/ui/reader/ReaderPostListFragment.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index af69cefcf2c5..0812919efb29 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -6,6 +6,7 @@ import android.support.design.widget.Snackbar; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.ListPopupWindow; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -358,6 +359,14 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.reader_fragment_post_cards, container, false); mRecyclerView = (ReaderRecyclerView) rootView.findViewById(R.id.recycler_view); + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + hideNewPostsBar(); + } + }); + Context context = container.getContext(); // add the item decoration (dividers) to the recycler, skipping the first item if the first From c9507c4860a68be5d2a01d815967a6bf1031f9a1 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Fri, 4 Mar 2016 19:57:18 -0500 Subject: [PATCH 2/7] Don't assign the scroll listener until the new posts bar is shown --- .../ui/reader/ReaderPostListFragment.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 0812919efb29..9d644972a0d1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -359,14 +359,6 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.reader_fragment_post_cards, container, false); mRecyclerView = (ReaderRecyclerView) rootView.findViewById(R.id.recycler_view); - mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - hideNewPostsBar(); - } - }); - Context context = container.getContext(); // add the item decoration (dividers) to the recycler, skipping the first item if the first @@ -939,6 +931,15 @@ private void showNewPostsBar() { AniUtils.startAnimation(mNewPostsBar, R.anim.reader_top_bar_in); mNewPostsBar.setVisibility(View.VISIBLE); + // hide the bar when the recycler is scrolled + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + hideNewPostsBar(); + } + }); + // remove the gap marker if it's showing, since it's no longer valid getPostAdapter().removeGapMarker(); } @@ -949,6 +950,7 @@ private void hideNewPostsBar() { } mIsAnimatingOutNewPostsBar = true; + mRecyclerView.clearOnScrollListeners(); Animation.AnimationListener listener = new Animation.AnimationListener() { @Override From 7774ad06bdf8ae1f6fad743aabdb821471c8f23b Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Fri, 4 Mar 2016 20:25:29 -0500 Subject: [PATCH 3/7] Don't hide the new posts bar on scroll until the view is scrolled a reasonable amount --- .../android/ui/reader/ReaderPostListFragment.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 9d644972a0d1..f9c26f85098c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -47,6 +47,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; import org.wordpress.android.util.DateTimeUtils; +import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPActivityUtils; @@ -86,6 +87,7 @@ public class ReaderPostListFragment extends Fragment private ReaderPostListType mPostListType; private int mRestorePosition; + private int mHideNewPostsBarOnScrollOffset; private boolean mIsUpdating; private boolean mWasPaused; @@ -380,6 +382,9 @@ public void onClick(View view) { } }); + // determine how far the user has to scroll before the "new posts" bar is hidden + mHideNewPostsBarOnScrollOffset = DisplayUtils.dpToPx(context, 10); + // view that appears when current tag/blog has no posts - box images in this view are // displayed and animated for tags only mEmptyView = rootView.findViewById(R.id.empty_view); @@ -936,7 +941,9 @@ private void showNewPostsBar() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); - hideNewPostsBar(); + if (dy >= mHideNewPostsBarOnScrollOffset) { + hideNewPostsBar(); + } } }); @@ -950,6 +957,8 @@ private void hideNewPostsBar() { } mIsAnimatingOutNewPostsBar = true; + + // remove the onScrollListener assigned in showNewPostsBar() mRecyclerView.clearOnScrollListeners(); Animation.AnimationListener listener = new Animation.AnimationListener() { From 136ccd93329691eed5789dd11c144810302c7c97 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Sat, 5 Mar 2016 16:02:31 -0500 Subject: [PATCH 4/7] Use constant rather than hard-coded 10 --- .../android/ui/reader/ReaderPostListFragment.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index f9c26f85098c..28a1e62a9d01 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -87,7 +87,6 @@ public class ReaderPostListFragment extends Fragment private ReaderPostListType mPostListType; private int mRestorePosition; - private int mHideNewPostsBarOnScrollOffset; private boolean mIsUpdating; private boolean mWasPaused; @@ -97,6 +96,9 @@ public class ReaderPostListFragment extends Fragment private static boolean mHasPurgedReaderDb; private static Date mLastAutoUpdateDt; + private int mHideNewPostsBarOnScrollOffsetPx; + private static final int HIDE_NEW_POSTS_BAR_ON_SCROLL_OFFSET_DP = 10; + private final HistoryStack mTagPreviewHistory = new HistoryStack("tag_preview_history"); private static class HistoryStack extends Stack { @@ -383,7 +385,7 @@ public void onClick(View view) { }); // determine how far the user has to scroll before the "new posts" bar is hidden - mHideNewPostsBarOnScrollOffset = DisplayUtils.dpToPx(context, 10); + mHideNewPostsBarOnScrollOffsetPx = DisplayUtils.dpToPx(context, HIDE_NEW_POSTS_BAR_ON_SCROLL_OFFSET_DP); // view that appears when current tag/blog has no posts - box images in this view are // displayed and animated for tags only @@ -941,7 +943,7 @@ private void showNewPostsBar() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); - if (dy >= mHideNewPostsBarOnScrollOffset) { + if (dy >= mHideNewPostsBarOnScrollOffsetPx) { hideNewPostsBar(); } } From 9522e377948e439f13ba3fab0e6f1afc5ab9e0df Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Sat, 5 Mar 2016 16:06:12 -0500 Subject: [PATCH 5/7] Hide bar regardless of scroll direction --- .../wordpress/android/ui/reader/ReaderPostListFragment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 28a1e62a9d01..9a63bcbc06c4 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -805,7 +805,7 @@ && getPostListType() == ReaderPostListType.TAG_FOLLOWED && !isFirstPostVisible()) { showNewPostsBar(); } else if (event.getResult().isNewOrChanged()) { - refreshPosts(); + showNewPostsBar(); // TODO: refreshPosts(); } else { boolean requestFailed = (event.getResult() == ReaderActions.UpdateResult.FAILED); setEmptyTitleAndDescription(requestFailed); @@ -943,7 +943,7 @@ private void showNewPostsBar() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); - if (dy >= mHideNewPostsBarOnScrollOffsetPx) { + if (Math.abs(dy) >= mHideNewPostsBarOnScrollOffsetPx) { hideNewPostsBar(); } } From 0f0887415e75eed79921adb3119725057f8cf962 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Sat, 5 Mar 2016 16:12:43 -0500 Subject: [PATCH 6/7] Use a single instance of the scroll listener --- .../ui/reader/ReaderPostListFragment.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index 9a63bcbc06c4..f3d6d6289020 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -805,7 +805,7 @@ && getPostListType() == ReaderPostListType.TAG_FOLLOWED && !isFirstPostVisible()) { showNewPostsBar(); } else if (event.getResult().isNewOrChanged()) { - showNewPostsBar(); // TODO: refreshPosts(); + refreshPosts(); } else { boolean requestFailed = (event.getResult() == ReaderActions.UpdateResult.FAILED); setEmptyTitleAndDescription(requestFailed); @@ -930,6 +930,20 @@ private boolean isNewPostsBarShowing() { return (mNewPostsBar != null && mNewPostsBar.getVisibility() == View.VISIBLE); } + /* + * scroll listener assigned to the recycler when the "new posts" bar is shown to hide + * it upon scrolling + */ + private final RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (Math.abs(dy) >= mHideNewPostsBarOnScrollOffsetPx) { + hideNewPostsBar(); + } + } + }; + private void showNewPostsBar() { if (!isAdded() || isNewPostsBarShowing()) { return; @@ -939,15 +953,7 @@ private void showNewPostsBar() { mNewPostsBar.setVisibility(View.VISIBLE); // hide the bar when the recycler is scrolled - mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - if (Math.abs(dy) >= mHideNewPostsBarOnScrollOffsetPx) { - hideNewPostsBar(); - } - } - }); + mRecyclerView.addOnScrollListener(mOnScrollListener); // remove the gap marker if it's showing, since it's no longer valid getPostAdapter().removeGapMarker(); @@ -961,7 +967,7 @@ private void hideNewPostsBar() { mIsAnimatingOutNewPostsBar = true; // remove the onScrollListener assigned in showNewPostsBar() - mRecyclerView.clearOnScrollListeners(); + mRecyclerView.removeOnScrollListener(mOnScrollListener); Animation.AnimationListener listener = new Animation.AnimationListener() { @Override From 5d516a4f937a2447f86d60fc999ace293e8ce7fa Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Sat, 5 Mar 2016 17:28:01 -0500 Subject: [PATCH 7/7] Don't enforce scroll distance, and wait a second before assigning scroll listener --- .../ui/reader/ReaderPostListFragment.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java index f3d6d6289020..d5f985d7e8fd 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListFragment.java @@ -47,7 +47,6 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; import org.wordpress.android.util.DateTimeUtils; -import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPActivityUtils; @@ -96,9 +95,6 @@ public class ReaderPostListFragment extends Fragment private static boolean mHasPurgedReaderDb; private static Date mLastAutoUpdateDt; - private int mHideNewPostsBarOnScrollOffsetPx; - private static final int HIDE_NEW_POSTS_BAR_ON_SCROLL_OFFSET_DP = 10; - private final HistoryStack mTagPreviewHistory = new HistoryStack("tag_preview_history"); private static class HistoryStack extends Stack { @@ -384,9 +380,6 @@ public void onClick(View view) { } }); - // determine how far the user has to scroll before the "new posts" bar is hidden - mHideNewPostsBarOnScrollOffsetPx = DisplayUtils.dpToPx(context, HIDE_NEW_POSTS_BAR_ON_SCROLL_OFFSET_DP); - // view that appears when current tag/blog has no posts - box images in this view are // displayed and animated for tags only mEmptyView = rootView.findViewById(R.id.empty_view); @@ -938,9 +931,7 @@ private boolean isNewPostsBarShowing() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); - if (Math.abs(dy) >= mHideNewPostsBarOnScrollOffsetPx) { - hideNewPostsBar(); - } + hideNewPostsBar(); } }; @@ -952,8 +943,17 @@ private void showNewPostsBar() { AniUtils.startAnimation(mNewPostsBar, R.anim.reader_top_bar_in); mNewPostsBar.setVisibility(View.VISIBLE); - // hide the bar when the recycler is scrolled - mRecyclerView.addOnScrollListener(mOnScrollListener); + // assign the scroll listener to hide the bar when the recycler is scrolled, but don't assign + // it right away since the user may be scrolling when the bar appears (which would cause it + // to disappear as soon as it's displayed) + mRecyclerView.postDelayed(new Runnable() { + @Override + public void run() { + if (isAdded() && isNewPostsBarShowing()) { + mRecyclerView.addOnScrollListener(mOnScrollListener); + } + } + }, 1000L); // remove the gap marker if it's showing, since it's no longer valid getPostAdapter().removeGapMarker();