From 027cbdb7a3b918046f721c579155eab07b961267 Mon Sep 17 00:00:00 2001 From: martinsandfuchs Date: Sat, 7 Dec 2019 19:41:28 +0100 Subject: Use volume keys to navigate posts --- .../Activity/AccountPostsActivity.java | 6 +++ .../Activity/AccountSavedThingActivity.java | 10 +++++ .../Activity/FilteredThingActivity.java | 6 +++ .../infinityforreddit/Activity/MainActivity.java | 27 ++++++++++++ .../Activity/SearchResultActivity.java | 10 +++++ .../Activity/ViewSubredditDetailActivity.java | 6 +++ .../Activity/ViewUserDetailActivity.java | 10 +++++ .../infinityforreddit/Fragment/PostFragment.java | 48 ++++++++++++++++++++-- .../infinityforreddit/FragmentCommunicator.java | 2 + .../Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/main_preferences.xml | 5 +++ 12 files changed, 128 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java index 8eaef666..2fcb2801 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountPostsActivity.java @@ -5,6 +5,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -159,6 +160,11 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect } } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return ((FragmentCommunicator) mFragment).handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override public SharedPreferences getSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java index f55086de..44f4014d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/AccountSavedThingActivity.java @@ -5,6 +5,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -151,6 +152,11 @@ public class AccountSavedThingActivity extends BaseActivity { } } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return sectionsPagerAdapter.handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override public SharedPreferences getSharedPreferences() { return mSharedPreferences; @@ -339,6 +345,10 @@ public class AccountSavedThingActivity extends BaseActivity { return fragment; } + public boolean handleKeyDown(int keyCode) { + return viewPager.getCurrentItem() == 0 && postFragment.handleKeyDown(keyCode); + } + public void refresh() { if (viewPager.getCurrentItem() == 0) { if (postFragment != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java index 3c8160f9..b2c59ca9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FilteredThingActivity.java @@ -5,6 +5,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -176,6 +177,11 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return ((FragmentCommunicator) mFragment).handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override public SharedPreferences getSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java index a0c1883f..136f697c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -8,6 +8,7 @@ import android.content.res.Resources; import android.os.Build; import android.os.Bundle; import android.util.TypedValue; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -863,6 +864,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return sectionsPagerAdapter.handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override protected void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -1071,6 +1077,27 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb return fragment; } + boolean handleKeyDown(int keyCode) { + if (mAccessToken == null) { + switch (viewPager.getCurrentItem()) { + case 0: + return popularPostFragment.handleKeyDown(keyCode); + case 1: + return allPostFragment.handleKeyDown(keyCode); + } + } else { + switch (viewPager.getCurrentItem()) { + case 0: + return frontPagePostFragment.handleKeyDown(keyCode); + case 1: + return popularPostFragment.handleKeyDown(keyCode); + case 2: + return allPostFragment.handleKeyDown(keyCode); + } + } + return false; + } + boolean startLazyMode() { if (mAccessToken == null) { switch (viewPager.getCurrentItem()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java index beafad93..03684be1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SearchResultActivity.java @@ -6,6 +6,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -170,6 +171,11 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect } } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return sectionsPagerAdapter.handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override public SharedPreferences getSharedPreferences() { return mSharedPreferences; @@ -368,6 +374,10 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect return fragment; } + public boolean handleKeyDown(int keyCode) { + return viewPager.getCurrentItem() == 0 && postFragment.handleKeyDown(keyCode); + } + void changeSortType(SortType sortType) { mSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, sortType.getType().name()).apply(); if(sortType.getTime() != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java index 5a8cbaff..6dcbc863 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java @@ -7,6 +7,7 @@ import android.content.res.Resources; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -322,6 +323,11 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp }); } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return ((FragmentCommunicator)(mFragment)).handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override public SharedPreferences getSharedPreferences() { return mSharedPreferences; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java index b4617196..5ffaa717 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewUserDetailActivity.java @@ -8,6 +8,7 @@ import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.util.TypedValue; +import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -396,6 +397,11 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele postLayoutBottomSheetFragment = new PostLayoutBottomSheetFragment(); } + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + return sectionsPagerAdapter.handleKeyDown(keyCode) || super.onKeyDown(keyCode, event); + } + @Override public SharedPreferences getSharedPreferences() { return mSharedPreferences; @@ -741,6 +747,10 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele return fragment; } + public boolean handleKeyDown(int keyCode) { + return viewPager.getCurrentItem() == 0 && postFragment.handleKeyDown(keyCode); + } + public void refresh() { if (viewPager.getCurrentItem() == 0) { if (postFragment != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 53dcb587..2ae04cfa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -13,6 +13,7 @@ import android.os.Bundle; import android.os.CountDownTimer; import android.os.Handler; import android.util.TypedValue; +import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -121,6 +122,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private CountDownTimer resumeLazyModeCountDownTimer; private float lazyModeInterval; private int postLayout; + private boolean mIsSmoothScrolling = false; public PostFragment() { // Required empty public constructor @@ -137,6 +139,32 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + private boolean scrollPostsByCount(int count) { + mIsSmoothScrolling = true; + if (mLinearLayoutManager != null) { + int pos = mLinearLayoutManager.findFirstVisibleItemPosition(); + int targetPosition = pos + count; + mLinearLayoutManager.scrollToPositionWithOffset(targetPosition, 0); + return true; + } else { + return false; + } + } + + @Override + public boolean handleKeyDown(int keyCode) { + boolean volumeKeysNavigatePosts = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_POSTS, false); + if (volumeKeysNavigatePosts) { + switch (keyCode) { + case KeyEvent.KEYCODE_VOLUME_UP: + return scrollPostsByCount(-1); + case KeyEvent.KEYCODE_VOLUME_DOWN: + return scrollPostsByCount(1); + } + } + return false; + } + @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -160,6 +188,16 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }; + mPostRecyclerView.clearOnScrollListeners(); + mPostRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { + if (newState == RecyclerView.SCROLL_STATE_IDLE) { + mIsSmoothScrolling = false; + } + } + }); + window = activity.getWindow(); Resources resources = getResources(); @@ -260,10 +298,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (dy > 0) { - ((MainActivity) activity).postScrollDown(); - } else if (dy < 0) { - ((MainActivity) activity).postScrollUp(); + if (!mIsSmoothScrolling) { + if (dy > 0) { + ((MainActivity) activity).postScrollDown(); + } else if (dy < 0) { + ((MainActivity) activity).postScrollUp(); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java index 5d66dc49..07cda1bb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FragmentCommunicator.java @@ -3,6 +3,8 @@ package ml.docilealligator.infinityforreddit; public interface FragmentCommunicator { void refresh(); + default boolean handleKeyDown(int keyCode) { return false; } + default void changeNSFW(boolean nsfw) { } 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 8333db44..9e04530f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -59,4 +59,5 @@ public class SharedPreferencesUtils { public static final String SHOW_ELAPSED_TIME_KEY = "show_elapsed_time"; public static final String SWIPE_RIGHT_TO_GO_BACK_FROM_POST_DETAIL = "swipe_to_go_back_from_post_detail"; public static final String VOLUME_KEYS_NAVIGATE_COMMENTS = "volume_keys_navigate_comments"; + public static final String VOLUME_KEYS_NAVIGATE_POSTS = "volume_keys_navigate_posts"; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 24bd34c7..8f1aa5ca 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -291,6 +291,7 @@ Immersive Interface Vote Buttons on the Right Use Volume Keys to Navigate Comments in Posts + Use Volume Keys to Navigate Posts Show Elpased Time in Posts and Comments Swipe Right to Go Back From Comments Lazy Mode Interval diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 894642fa..98e566eb 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -46,6 +46,11 @@ app:key="volume_keys_navigate_comments" app:title="@string/settings_volume_keys_navigate_comments_title" /> + + Date: Sun, 8 Dec 2019 15:01:29 +0100 Subject: Because we do not use a SmoothScroller we do not need to keep track of smooth scrolling --- .../infinityforreddit/Fragment/PostFragment.java | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 2ae04cfa..17661c24 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -122,7 +122,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private CountDownTimer resumeLazyModeCountDownTimer; private float lazyModeInterval; private int postLayout; - private boolean mIsSmoothScrolling = false; public PostFragment() { // Required empty public constructor @@ -140,7 +139,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } private boolean scrollPostsByCount(int count) { - mIsSmoothScrolling = true; if (mLinearLayoutManager != null) { int pos = mLinearLayoutManager.findFirstVisibleItemPosition(); int targetPosition = pos + count; @@ -188,16 +186,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }; - mPostRecyclerView.clearOnScrollListeners(); - mPostRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { - if (newState == RecyclerView.SCROLL_STATE_IDLE) { - mIsSmoothScrolling = false; - } - } - }); - window = activity.getWindow(); Resources resources = getResources(); @@ -298,12 +286,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (!mIsSmoothScrolling) { - if (dy > 0) { - ((MainActivity) activity).postScrollDown(); - } else if (dy < 0) { - ((MainActivity) activity).postScrollUp(); - } + if (dy > 0) { + ((MainActivity) activity).postScrollDown(); + } else if (dy < 0) { + ((MainActivity) activity).postScrollUp(); } } -- cgit v1.2.3