From 7c23be5d2fc583e1aa1aa251287fee4c35f2c751 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 15 Sep 2020 23:03:08 +0800 Subject: Add an option to disable swiping between tabs in MainActivity. --- .../infinityforreddit/Activity/MainActivity.java | 11 ++++++++++- .../Event/ChangeDisableSwipingBetweenTabsEvent.java | 9 +++++++++ .../Settings/SwipeActionPreferenceFragment.java | 9 +++++++++ .../infinityforreddit/Utils/SharedPreferencesUtils.java | 1 + 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableSwipingBetweenTabsEvent.java (limited to 'app/src/main/java') 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 330059a9..dea3aaf2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -72,6 +72,7 @@ import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSh import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.Event.ChangeConfirmToExitEvent; +import ml.docilealligator.infinityforreddit.Event.ChangeDisableSwipingBetweenTabsEvent; import ml.docilealligator.infinityforreddit.Event.ChangeLockBottomAppBarEvent; import ml.docilealligator.infinityforreddit.Event.ChangeNSFWEvent; import ml.docilealligator.infinityforreddit.Event.RecreateActivityEvent; @@ -199,6 +200,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private boolean showBottomAppBar; private boolean mConfirmToExit; private boolean mLockBottomAppBar; + private boolean mDisableSwipingBetweenTabs; @Override protected void onCreate(Bundle savedInstanceState) { @@ -265,6 +267,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb showBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY, false); mConfirmToExit = mSharedPreferences.getBoolean(SharedPreferencesUtils.CONFIRM_TO_EXIT, false); mLockBottomAppBar = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR, false); + mDisableSwipingBetweenTabs = mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS, false); if (savedInstanceState != null) { mFetchUserInfoSuccess = savedInstanceState.getBoolean(FETCH_USER_INFO_STATE); @@ -585,7 +588,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle()); viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setOffscreenPageLimit(3); - viewPager2.requestDisallowInterceptTouchEvent(true); + viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs); new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { if (mAccessToken == null) { switch (position) { @@ -963,6 +966,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb mLockBottomAppBar = changeLockBottomAppBarEvent.lockBottomAppBar; } + @Subscribe + public void onChangeDisableSwipingBetweenTabsEvent(ChangeDisableSwipingBetweenTabsEvent changeDisableSwipingBetweenTabsEvent) { + mDisableSwipingBetweenTabs = changeDisableSwipingBetweenTabsEvent.disableSwipingBetweenTabs; + viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs); + } + @Override public void onLongPress() { if (sectionsPagerAdapter != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableSwipingBetweenTabsEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableSwipingBetweenTabsEvent.java new file mode 100644 index 00000000..371954e8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/ChangeDisableSwipingBetweenTabsEvent.java @@ -0,0 +1,9 @@ +package ml.docilealligator.infinityforreddit.Event; + +public class ChangeDisableSwipingBetweenTabsEvent { + public boolean disableSwipingBetweenTabs; + + public ChangeDisableSwipingBetweenTabsEvent(boolean disableSwipingBetweenTabs) { + this.disableSwipingBetweenTabs = disableSwipingBetweenTabs; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java index 80358559..115cd4d3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/SwipeActionPreferenceFragment.java @@ -7,6 +7,7 @@ import androidx.preference.SwitchPreference; import org.greenrobot.eventbus.EventBus; +import ml.docilealligator.infinityforreddit.Event.ChangeDisableSwipingBetweenTabsEvent; import ml.docilealligator.infinityforreddit.Event.ChangeVibrateWhenActionTriggeredEvent; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -18,6 +19,7 @@ public class SwipeActionPreferenceFragment extends PreferenceFragmentCompat { setPreferencesFromResource(R.xml.swipe_action_preferences, rootKey); SwitchPreference vibrateWhenActionTriggeredSwitch = findPreference(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED); + SwitchPreference disableSwipingBetweenTabsSwitch = findPreference(SharedPreferencesUtils.DISABLE_SWIPING_BETWEEN_TABS); if (vibrateWhenActionTriggeredSwitch != null) { vibrateWhenActionTriggeredSwitch.setOnPreferenceChangeListener((preference, newValue) -> { @@ -25,5 +27,12 @@ public class SwipeActionPreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + if (disableSwipingBetweenTabsSwitch != null) { + disableSwipingBetweenTabsSwitch.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new ChangeDisableSwipingBetweenTabsEvent((Boolean) newValue)); + return true; + }); + } } } \ No newline at end of file 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 1beff64f..338afe81 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -120,6 +120,7 @@ public class SharedPreferencesUtils { public static final String VIDEO_DOWNLOAD_LOCATION = "video_download_location"; public static final String SEPARATE_FOLDER_FOR_EACH_SUBREDDIT = "separate_folder_for_each_subreddit"; 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 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"; -- cgit v1.2.3