From ef514a85c703464134934012b051979fa049a326 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 15 Sep 2020 23:58:17 +0800 Subject: Add an option to disable pull to refresh. --- .../infinityforreddit/Activity/SettingsActivity.java | 3 +++ .../Fragment/ChangePullToRefreshEvent.java | 9 +++++++++ .../infinityforreddit/Fragment/PostFragment.java | 9 +++++++-- .../Settings/GesturesAndButtonsPreferenceFragment.java | 13 +++++++++++++ .../infinityforreddit/Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/gestures_and_buttons_preference.xml | 5 +++++ 7 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ChangePullToRefreshEvent.java (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java index 325e8e91..07abf2d5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SettingsActivity.java @@ -30,6 +30,7 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.Settings.AboutPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.CustomizeMainPageTabsFragment; import ml.docilealligator.infinityforreddit.Settings.FontPreferenceFragment; +import ml.docilealligator.infinityforreddit.Settings.GesturesAndButtonsPreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.InterfacePreferenceFragment; import ml.docilealligator.infinityforreddit.Settings.MainPreferenceFragment; @@ -86,6 +87,8 @@ public class SettingsActivity extends BaseActivity implements setTitle(R.string.settings_interface_title); } else if (getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity) instanceof FontPreferenceFragment) { setTitle(R.string.settings_font_title); + } else if (getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity) instanceof GesturesAndButtonsPreferenceFragment) { + setTitle(R.string.settings_gestures_and_buttons_title); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ChangePullToRefreshEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ChangePullToRefreshEvent.java new file mode 100644 index 00000000..ea1d2234 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/ChangePullToRefreshEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Fragment; + +public class ChangePullToRefreshEvent { + public boolean pullToRefresh; + + public ChangePullToRefreshEvent(boolean pullToRefresh) { + this.pullToRefresh = pullToRefresh; + } +} 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 e7807356..6208807e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -174,7 +174,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean isShown = false; private boolean savePostFeedScrolledPosition; private boolean vibrateWhenActionTriggered; - private boolean enableSwipeAction; private PostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller smoothScroller; private Window window; @@ -345,6 +344,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }; + mSwipeRefreshLayout.setEnabled(mSharedPreferences.getBoolean(SharedPreferencesUtils.PULL_TO_REFRESH, true)); mSwipeRefreshLayout.setOnRefreshListener(this::refresh); if (savedInstanceState != null) { @@ -401,7 +401,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false); vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); - enableSwipeAction = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false); + boolean enableSwipeAction = mSharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_SWIPE_ACTION, false); Locale locale = getResources().getConfiguration().locale; if (postType == PostDataSource.TYPE_SEARCH) { @@ -1202,6 +1202,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } + @Subscribe + public void onChangePullToRefreshEvent(ChangePullToRefreshEvent changePullToRefreshEvent) { + mSwipeRefreshLayout.setEnabled(changePullToRefreshEvent.pullToRefresh); + } + private void refreshAdapter() { int previousPosition = -1; if (mLinearLayoutManager != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/GesturesAndButtonsPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/GesturesAndButtonsPreferenceFragment.java index ec170cc8..3c1bde13 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/GesturesAndButtonsPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/GesturesAndButtonsPreferenceFragment.java @@ -8,6 +8,7 @@ import android.os.Bundle; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; +import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreference; @@ -17,6 +18,7 @@ import javax.inject.Inject; import javax.inject.Named; import ml.docilealligator.infinityforreddit.Event.ChangeLockBottomAppBarEvent; +import ml.docilealligator.infinityforreddit.Fragment.ChangePullToRefreshEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -41,6 +43,7 @@ public class GesturesAndButtonsPreferenceFragment extends PreferenceFragmentComp SwitchPreference lockBottomAppBarSwitch = findPreference(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR); SwitchPreference swipeUpToHideJumpToNextTopLevelCommentButtonSwitch = findPreference(SharedPreferencesUtils.SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON); + SwitchPreference pullToRefreshSwitch = findPreference(SharedPreferencesUtils.PULL_TO_REFRESH); if (lockJumpToNextTopLevelCommentButtonSwitch != null && lockBottomAppBarSwitch != null && swipeUpToHideJumpToNextTopLevelCommentButtonSwitch != null) { @@ -65,6 +68,16 @@ public class GesturesAndButtonsPreferenceFragment extends PreferenceFragmentComp swipeUpToHideJumpToNextTopLevelCommentButtonSwitch.setVisible(true); } } + + if (pullToRefreshSwitch != null) { + pullToRefreshSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + EventBus.getDefault().post(new ChangePullToRefreshEvent((Boolean) newValue)); + return true; + } + }); + } } @Override 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 8612369f..acf7566d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -122,6 +122,7 @@ public class SharedPreferencesUtils { public static final String VIBRATE_WHEN_ACTION_TRIGGERED = "vibrate_when_action_triggered"; public static final String DISABLE_SWIPING_BETWEEN_TABS = "disable_swiping_between_tabs"; public static final String ENABLE_SWIPE_ACTION = "enable_swipe_action"; + public static final String PULL_TO_REFRESH = "pull_to_refresh"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b881e2d9..3e70c54d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -482,6 +482,7 @@ Vibrate the phone when the action is triggered Disable Swiping Between Tabs Enable Swipe Action + Pull to Refresh Cannot get the link diff --git a/app/src/main/res/xml/gestures_and_buttons_preference.xml b/app/src/main/res/xml/gestures_and_buttons_preference.xml index 7b883826..bd407a7a 100644 --- a/app/src/main/res/xml/gestures_and_buttons_preference.xml +++ b/app/src/main/res/xml/gestures_and_buttons_preference.xml @@ -39,6 +39,11 @@ app:key="swap_tap_and_long_in_comments" app:title="@string/settings_swap_tap_and_long_title" /> + + -- cgit v1.2.3