From d6051ca932a234da18dcd9eb351be741d085c9d4 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 24 Sep 2020 17:28:13 +0800 Subject: Support hiding tab titles. Support changing tab count. --- .../infinityforreddit/Activity/MainActivity.java | 83 ++++++++++++---------- .../Settings/CustomizeMainPageTabsFragment.java | 33 ++++++++- .../Utils/SharedPreferencesUtils.java | 2 + 3 files changed, 78 insertions(+), 40 deletions(-) (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 384f7621..be8799c6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -601,46 +601,51 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setAdapter(adapter); + int tabCount = mMainActivityTabsSharedPreferences.getInt((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_COUNT, 3); mShowSubscribedSubreddits = mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false); - sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), mShowSubscribedSubreddits); + sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), tabCount, mShowSubscribedSubreddits); viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setOffscreenPageLimit(1); viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs); - if (mShowSubscribedSubreddits) { - tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); - } else { - tabLayout.setTabMode(TabLayout.MODE_FIXED); - } - new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { - if (mAccessToken == null) { - switch (position) { - case 0: - tab.setText(R.string.popular); - break; - case 1: - tab.setText(R.string.all); - break; - } + if (mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, true)) { + if (mShowSubscribedSubreddits) { + tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); } else { - switch (position) { - case 0: - tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home))); - break; - case 1: - tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, getString(R.string.popular))); - break; - case 2: - tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all))); - break; - } - if (position >= 3 && mShowSubscribedSubreddits && sectionsPagerAdapter != null) { - List subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits; - if (position - 3 < subscribedSubreddits.size()) { - tab.setText(subscribedSubreddits.get(position - 3).getName()); + tabLayout.setTabMode(TabLayout.MODE_FIXED); + } + new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> { + if (mAccessToken == null) { + switch (position) { + case 0: + tab.setText(R.string.popular); + break; + case 1: + tab.setText(R.string.all); + break; + } + } else { + switch (position) { + case 0: + tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home))); + break; + case 1: + tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, getString(R.string.popular))); + break; + case 2: + tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all))); + break; + } + if (position >= tabCount && mShowSubscribedSubreddits && sectionsPagerAdapter != null) { + List subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits; + if (position - tabCount < subscribedSubreddits.size()) { + tab.setText(subscribedSubreddits.get(position - tabCount).getName()); + } } } - } - }).attach(); + }).attach(); + } else { + tabLayout.setVisibility(View.GONE); + } viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { @Override @@ -997,11 +1002,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } private class SectionsPagerAdapter extends FragmentStateAdapter { + int tabCount; boolean showSubscribedSubreddits; List subscribedSubreddits; - SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, boolean showSubscribedSubreddits) { + SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, int tabCount, boolean showSubscribedSubreddits) { super(fm, lifecycle); + this.tabCount = tabCount; subscribedSubreddits = new ArrayList<>(); this.showSubscribedSubreddits = showSubscribedSubreddits; } @@ -1043,9 +1050,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb return generatePostFragment(postType, name); } else { if (showSubscribedSubreddits) { - if (position > 2 && position - 3 < subscribedSubreddits.size()) { + if (position >= tabCount && position - tabCount < subscribedSubreddits.size()) { int postType = SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT; - String name = subscribedSubreddits.get(position - 3).getName(); + String name = subscribedSubreddits.get(position - tabCount).getName(); return generatePostFragment(postType, name); } } @@ -1130,9 +1137,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb return 2; } if (showSubscribedSubreddits) { - return 3 + subscribedSubreddits.size(); + return tabCount + subscribedSubreddits.size(); } - return 3; + return tabCount; } @Nullable diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java index fe75de74..a07ad3c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java @@ -33,6 +33,14 @@ public class CustomizeMainPageTabsFragment extends Fragment { @BindView(R.id.info_text_view_customize_main_page_tabs_fragment) TextView infoTextView; + @BindView(R.id.tab_count_linear_layout_customize_main_page_tabs_fragment) + LinearLayout tabCountLinearLayout; + @BindView(R.id.tab_count_text_view_customize_main_page_tabs_fragment) + TextView tabCountTextView; + @BindView(R.id.show_tab_names_linear_layout_customize_main_page_tabs_fragment) + LinearLayout showTabNamesLinearLayout; + @BindView(R.id.show_tab_names_switch_material_customize_main_page_tabs_fragment) + SwitchMaterial showTabNamesSwitch; @BindView(R.id.divider_1_customize_main_page_tabs_fragment) View divider1; @BindView(R.id.tab_1_group_summary_customize_main_page_tabs_fragment) @@ -99,6 +107,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { @Named("main_activity_tabs") SharedPreferences sharedPreferences; private Activity activity; + private int tabCount; private String tab1CurrentTitle; private int tab1CurrentPostType; private String tab1CurrentName; @@ -128,6 +137,8 @@ public class CustomizeMainPageTabsFragment extends Fragment { if (accountName == null) { infoTextView.setText(R.string.settings_customize_tabs_in_main_page_summary); divider1.setVisibility(View.GONE); + tabCountLinearLayout.setVisibility(View.GONE); + showTabNamesLinearLayout.setVisibility(View.GONE); tab1GroupSummaryTextView.setVisibility(View.GONE); tab1TitleLinearLayout.setVisibility(View.GONE); tab1TypeLinearLayout.setVisibility(View.GONE); @@ -147,6 +158,25 @@ public class CustomizeMainPageTabsFragment extends Fragment { return rootView; } + tabCount = sharedPreferences.getInt((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_COUNT, 3); + tabCountTextView.setText(Integer.toString(tabCount)); + tabCountLinearLayout.setOnClickListener(view -> { + new MaterialAlertDialogBuilder(activity, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.settings_tab_count) + .setSingleChoiceItems(R.array.settings_main_page_tab_count, tabCount - 1, (dialogInterface, i) -> { + tabCount = i + 1; + sharedPreferences.edit().putInt((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_COUNT, tabCount).apply(); + tabCountTextView.setText(Integer.toString(tabCount)); + dialogInterface.dismiss(); + }) + .show(); + }); + + boolean showTabNames = sharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, true); + showTabNamesSwitch.setChecked(showTabNames); + showTabNamesSwitch.setOnCheckedChangeListener((compoundButton, b) -> sharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, b).apply()); + showTabNamesLinearLayout.setOnClickListener(view -> showTabNamesSwitch.performClick()); + String[] typeValues = activity.getResources().getStringArray(R.array.settings_tab_post_type); tab1CurrentTitle = sharedPreferences.getString((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home)); @@ -447,12 +477,11 @@ public class CustomizeMainPageTabsFragment extends Fragment { }); showSubscribedSubredditsSwitchMaterial.setChecked(sharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false)); + showSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> sharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, b).apply()); showSubscribedSubredditsLinearLayout.setOnClickListener(view -> { showSubscribedSubredditsSwitchMaterial.performClick(); }); - showSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> sharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, b).apply()); - return rootView; } 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 20f56eb7..fd5e9a47 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -131,6 +131,8 @@ public class SharedPreferencesUtils { public static final String SAVE_FRONT_PAGE_SCROLLED_POSITION = "save_front_page_scrolled_position"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; + public static final String MAIN_PAGE_TAB_COUNT = "_main_page_tab_count"; + public static final String MAIN_PAGE_SHOW_TAB_NAMES = "_main_page_show_tab_names"; public static final String MAIN_PAGE_TAB_1_TITLE = "_main_page_tab_1_title"; public static final String MAIN_PAGE_TAB_2_TITLE = "_main_page_tab_2_title"; public static final String MAIN_PAGE_TAB_3_TITLE = "_main_page_tab_3_title"; -- cgit v1.2.3