Use volume keys to navigate posts

This commit is contained in:
martinsandfuchs 2019-12-07 19:41:28 +01:00
parent f7eaa7ca06
commit 027cbdb7a3
12 changed files with 128 additions and 4 deletions

View File

@ -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;

View File

@ -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) {

View File

@ -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;

View File

@ -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()) {

View File

@ -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) {

View File

@ -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;

View File

@ -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) {

View File

@ -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();
}
}
}

View File

@ -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) {
}

View File

@ -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";
}

View File

@ -291,6 +291,7 @@
<string name="settings_immersive_interface_title">Immersive Interface</string>
<string name="settings_vote_buttons_on_the_right_title">Vote Buttons on the Right</string>
<string name="settings_volume_keys_navigate_comments_title">Use Volume Keys to Navigate Comments in Posts</string>
<string name="settings_volume_keys_navigate_posts_title">Use Volume Keys to Navigate Posts</string>
<string name="settings_show_elapsed_time">Show Elpased Time in Posts and Comments</string>
<string name="swipe_to_go_back_from_post_detail">Swipe Right to Go Back From Comments</string>
<string name="settings_lazy_mode_interval_title">Lazy Mode Interval</string>

View File

@ -46,6 +46,11 @@
app:key="volume_keys_navigate_comments"
app:title="@string/settings_volume_keys_navigate_comments_title" />
<SwitchPreference
app:defaultValue="false"
app:key="volume_keys_navigate_posts"
app:title="@string/settings_volume_keys_navigate_posts_title" />
<Preference
app:title="@string/settings_font_size_title"
app:icon="@drawable/ic_font_size_24dp"