From 2a356aa12e67954043cf72732719bc2093e1fc6e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 26 Aug 2019 18:03:44 +0800 Subject: Don't start lazy mode if there is no posts. Stop lazy mode when refreshing. --- .../infinityforreddit/AccountPostsActivity.java | 11 ++++++----- .../infinityforreddit/FragmentCommunicator.java | 4 +++- .../infinityforreddit/MainActivity.java | 23 +++++++++++----------- .../infinityforreddit/PostFragment.java | 22 +++++++++++++++++---- .../ViewSubredditDetailActivity.java | 13 +++++++----- .../infinityforreddit/ViewUserDetailActivity.java | 18 ++++++++++------- app/src/main/res/values/strings.xml | 1 + 7 files changed, 59 insertions(+), 33 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java index b3d50c2f..786ec3b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java @@ -240,11 +240,12 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); collapsingToolbarLayout.setLayoutParams(params); } else { - ((FragmentCommunicator) mFragment).startLazyMode(); - isInLazyMode = true; - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); - collapsingToolbarLayout.setLayoutParams(params); + if(((FragmentCommunicator) mFragment).startLazyMode()) { + isInLazyMode = true; + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); + collapsingToolbarLayout.setLayoutParams(params); + } } return true; case android.R.id.home: diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index f61d3beb..c50ef12c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -3,7 +3,9 @@ package ml.docilealligator.infinityforreddit; interface FragmentCommunicator { void refresh(); default void changeNSFW(boolean nsfw) {}; - default void startLazyMode() {} + default boolean startLazyMode() { + return false; + } default void stopLazyMode() {} default void resumeLazyMode(boolean resumeNow) {} default void pauseLazyMode(boolean startTimer) {} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 8488adbc..3791b5cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -719,11 +719,12 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS); collapsingToolbarLayout.setLayoutParams(params); } else { - sectionsPagerAdapter.startLazyMode(); - isInLazyMode = true; - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); - collapsingToolbarLayout.setLayoutParams(params); + if(sectionsPagerAdapter.startLazyMode()) { + isInLazyMode = true; + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL); + collapsingToolbarLayout.setLayoutParams(params); + } } return true; } @@ -935,17 +936,17 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe return fragment; } - void startLazyMode() { + boolean startLazyMode() { switch (viewPager.getCurrentItem()) { case 0: - ((FragmentCommunicator) frontPagePostFragment).startLazyMode(); - break; + return ((FragmentCommunicator) frontPagePostFragment).startLazyMode(); case 1: - ((FragmentCommunicator) popularPostFragment).startLazyMode(); - break; + return ((FragmentCommunicator) popularPostFragment).startLazyMode(); case 2: - ((FragmentCommunicator) allPostFragment).startLazyMode(); + return ((FragmentCommunicator) allPostFragment).startLazyMode(); } + + return false; } void stopLazyMode() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 493d0a27..b9275e62 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -75,6 +75,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean isInLazyMode = false; private boolean isLazyModePaused = false; + private boolean hasPost = false; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; @@ -367,16 +368,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); mPostViewModel.hasPost().observe(this, hasPost -> { + this.hasPost = hasPost; if(hasPost) { mFetchPostInfoLinearLayout.setVisibility(View.GONE); } else { + if(isInLazyMode) { + stopLazyMode(); + } + showErrorView(R.string.no_posts); } }); - mPostViewModel.getPaginationNetworkState().observe(this, networkState -> { - mAdapter.setNetworkState(networkState); - }); + mPostViewModel.getPaginationNetworkState().observe(this, networkState -> mAdapter.setNetworkState(networkState)); return rootView; } @@ -399,6 +403,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void refresh() { + if(isInLazyMode) { + stopLazyMode(); + } mPostViewModel.refresh(); } @@ -417,7 +424,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } @Override - public void startLazyMode() { + public boolean startLazyMode() { + if(!hasPost) { + Toast.makeText(activity, R.string.no_posts_no_lazy_mode, Toast.LENGTH_SHORT).show(); + return false; + } + isInLazyMode = true; isLazyModePaused = false; @@ -426,6 +438,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Toast.makeText(activity, getString(R.string.lazy_mode_start, lazyModeInterval), Toast.LENGTH_SHORT).show(); + + return true; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index bf718932..6527c928 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -493,11 +493,14 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So collapsingToolbarLayout.setLayoutParams(params); } else { isInLazyMode = true; - ((FragmentCommunicator) mFragment).startLazyMode(); - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - appBarLayout.setExpanded(false); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); - collapsingToolbarLayout.setLayoutParams(params); + if(((FragmentCommunicator) mFragment).startLazyMode()) { + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + appBarLayout.setExpanded(false); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); + collapsingToolbarLayout.setLayoutParams(params); + } else { + isInLazyMode = false; + } } } return false; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index e21789f3..939ca6d9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -546,11 +546,14 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi collapsingToolbarLayout.setLayoutParams(params); } else { isInLazyMode = true; - sectionsPagerAdapter.startLazyMode(); - lazyModeItem.setTitle(R.string.action_stop_lazy_mode); - appBarLayout.setExpanded(false); - params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); - collapsingToolbarLayout.setLayoutParams(params); + if(sectionsPagerAdapter.startLazyMode()) { + lazyModeItem.setTitle(R.string.action_stop_lazy_mode); + appBarLayout.setExpanded(false); + params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED); + collapsingToolbarLayout.setLayoutParams(params); + } else { + isInLazyMode = false; + } } return true; } @@ -708,10 +711,11 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi } } - void startLazyMode() { + boolean startLazyMode() { if(postFragment != null) { - ((FragmentCommunicator) postFragment).startLazyMode(); + return ((FragmentCommunicator) postFragment).startLazyMode(); } + return false; } void stopLazyMode() { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2d19ae76..762a5bf6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -102,6 +102,7 @@ Infinity Search anything + No posts available Lazy Mode starts in %1$.1fs Lazy Mode stopped -- cgit v1.2.3