diff options
author | martinsandfuchs <martin@cluginbuehl.ch> | 2019-12-07 18:41:28 +0000 |
---|---|---|
committer | martinsandfuchs <martin@cluginbuehl.ch> | 2019-12-08 12:21:37 +0000 |
commit | 027cbdb7a3b918046f721c579155eab07b961267 (patch) | |
tree | a3783d872f123eb7b5cea1f4eeaf06f00fb8e3ba /app/src/main/java/ml | |
parent | f7eaa7ca06811e39b16dc80203f053e4cca118bc (diff) | |
download | infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.tar infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.tar.gz infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.tar.bz2 infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.tar.lz infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.tar.xz infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.tar.zst infinity-for-reddit-027cbdb7a3b918046f721c579155eab07b961267.zip |
Use volume keys to navigate posts
Diffstat (limited to 'app/src/main/java/ml')
10 files changed, 122 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; @@ -160,6 +161,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; @@ -152,6 +153,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; @@ -177,6 +178,11 @@ public class FilteredThingActivity extends BaseActivity implements SortTypeSelec } @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; @@ -864,6 +865,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); outState.putBoolean(FETCH_USER_INFO_STATE, mFetchUserInfoSuccess); @@ -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; @@ -171,6 +172,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; @@ -323,6 +324,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; @@ -397,6 +398,11 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } @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"; } |