Support hiding tab titles. Support changing tab count.

This commit is contained in:
Alex Ning 2020-09-24 17:28:13 +08:00
parent cf1d14264d
commit d6051ca932
6 changed files with 155 additions and 40 deletions

View File

@ -601,46 +601,51 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
navDrawerRecyclerView.setAdapter(adapter); 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); 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.setAdapter(sectionsPagerAdapter);
viewPager2.setOffscreenPageLimit(1); viewPager2.setOffscreenPageLimit(1);
viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs); viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs);
if (mShowSubscribedSubreddits) { if (mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, true)) {
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); if (mShowSubscribedSubreddits) {
} else { tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
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 { } else {
switch (position) { tabLayout.setTabMode(TabLayout.MODE_FIXED);
case 0: }
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_1_TITLE, getString(R.string.home))); new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
break; if (mAccessToken == null) {
case 1: switch (position) {
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_TITLE, getString(R.string.popular))); case 0:
break; tab.setText(R.string.popular);
case 2: break;
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all))); case 1:
break; tab.setText(R.string.all);
} break;
if (position >= 3 && mShowSubscribedSubreddits && sectionsPagerAdapter != null) { }
List<SubscribedSubredditData> subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits; } else {
if (position - 3 < subscribedSubreddits.size()) { switch (position) {
tab.setText(subscribedSubreddits.get(position - 3).getName()); 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<SubscribedSubredditData> 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() { viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override @Override
@ -997,11 +1002,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
private class SectionsPagerAdapter extends FragmentStateAdapter { private class SectionsPagerAdapter extends FragmentStateAdapter {
int tabCount;
boolean showSubscribedSubreddits; boolean showSubscribedSubreddits;
List<SubscribedSubredditData> subscribedSubreddits; List<SubscribedSubredditData> subscribedSubreddits;
SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, boolean showSubscribedSubreddits) { SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, int tabCount, boolean showSubscribedSubreddits) {
super(fm, lifecycle); super(fm, lifecycle);
this.tabCount = tabCount;
subscribedSubreddits = new ArrayList<>(); subscribedSubreddits = new ArrayList<>();
this.showSubscribedSubreddits = showSubscribedSubreddits; this.showSubscribedSubreddits = showSubscribedSubreddits;
} }
@ -1043,9 +1050,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
return generatePostFragment(postType, name); return generatePostFragment(postType, name);
} else { } else {
if (showSubscribedSubreddits) { 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; 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); return generatePostFragment(postType, name);
} }
} }
@ -1130,9 +1137,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
return 2; return 2;
} }
if (showSubscribedSubreddits) { if (showSubscribedSubreddits) {
return 3 + subscribedSubreddits.size(); return tabCount + subscribedSubreddits.size();
} }
return 3; return tabCount;
} }
@Nullable @Nullable

View File

@ -33,6 +33,14 @@ public class CustomizeMainPageTabsFragment extends Fragment {
@BindView(R.id.info_text_view_customize_main_page_tabs_fragment) @BindView(R.id.info_text_view_customize_main_page_tabs_fragment)
TextView infoTextView; 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) @BindView(R.id.divider_1_customize_main_page_tabs_fragment)
View divider1; View divider1;
@BindView(R.id.tab_1_group_summary_customize_main_page_tabs_fragment) @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") @Named("main_activity_tabs")
SharedPreferences sharedPreferences; SharedPreferences sharedPreferences;
private Activity activity; private Activity activity;
private int tabCount;
private String tab1CurrentTitle; private String tab1CurrentTitle;
private int tab1CurrentPostType; private int tab1CurrentPostType;
private String tab1CurrentName; private String tab1CurrentName;
@ -128,6 +137,8 @@ public class CustomizeMainPageTabsFragment extends Fragment {
if (accountName == null) { if (accountName == null) {
infoTextView.setText(R.string.settings_customize_tabs_in_main_page_summary); infoTextView.setText(R.string.settings_customize_tabs_in_main_page_summary);
divider1.setVisibility(View.GONE); divider1.setVisibility(View.GONE);
tabCountLinearLayout.setVisibility(View.GONE);
showTabNamesLinearLayout.setVisibility(View.GONE);
tab1GroupSummaryTextView.setVisibility(View.GONE); tab1GroupSummaryTextView.setVisibility(View.GONE);
tab1TitleLinearLayout.setVisibility(View.GONE); tab1TitleLinearLayout.setVisibility(View.GONE);
tab1TypeLinearLayout.setVisibility(View.GONE); tab1TypeLinearLayout.setVisibility(View.GONE);
@ -147,6 +158,25 @@ public class CustomizeMainPageTabsFragment extends Fragment {
return rootView; 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); 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)); 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.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 -> { showSubscribedSubredditsLinearLayout.setOnClickListener(view -> {
showSubscribedSubredditsSwitchMaterial.performClick(); showSubscribedSubredditsSwitchMaterial.performClick();
}); });
showSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> sharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, b).apply());
return rootView; return rootView;
} }

View File

@ -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 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_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_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_2_TITLE = "_main_page_tab_2_title";
public static final String MAIN_PAGE_TAB_3_TITLE = "_main_page_tab_3_title"; public static final String MAIN_PAGE_TAB_3_TITLE = "_main_page_tab_3_title";

View File

@ -24,6 +24,75 @@
android:fontFamily="?attr/font_family" android:fontFamily="?attr/font_family"
app:drawableStartCompat="@drawable/ic_info_preference_24dp" /> app:drawableStartCompat="@drawable/ic_info_preference_24dp" />
<LinearLayout
android:id="@+id/tab_count_linear_layout_customize_main_page_tabs_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="72dp"
android:paddingEnd="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="16dp"
android:layout_gravity="center_vertical"
android:text="@string/settings_tab_count"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<TextView
android:id="@+id/tab_count_text_view_customize_main_page_tabs_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="16dp"
android:layout_gravity="center_vertical"
android:text="@string/settings_more_tabs_show_subscribed_subreddits_title"
android:textColor="?attr/secondaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" />
</LinearLayout>
<LinearLayout
android:id="@+id/show_tab_names_linear_layout_customize_main_page_tabs_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:paddingStart="72dp"
android:paddingEnd="16dp"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginEnd="16dp"
android:layout_gravity="center_vertical"
android:text="@string/settings_show_tab_names"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/show_tab_names_switch_material_customize_main_page_tabs_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<View <View
android:id="@+id/divider_1_customize_main_page_tabs_fragment" android:id="@+id/divider_1_customize_main_page_tabs_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -224,4 +224,10 @@
<item>1</item> <item>1</item>
<item>2</item> <item>2</item>
</string-array> </string-array>
<string-array name="settings_main_page_tab_count">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources> </resources>

View File

@ -447,6 +447,8 @@
<string name="settings_reset_all_settings_title">Reset All Settings</string> <string name="settings_reset_all_settings_title">Reset All Settings</string>
<string name="settings_advanced_settings_summary">Clean the database and shared preferences</string> <string name="settings_advanced_settings_summary">Clean the database and shared preferences</string>
<string name="settings_tab_info">Restart the app to see the changes</string> <string name="settings_tab_info">Restart the app to see the changes</string>
<string name="settings_tab_count">Tab Count</string>
<string name="settings_show_tab_names">Show Tab Names</string>
<string name="settings_tab_1_summary">Tab 1</string> <string name="settings_tab_1_summary">Tab 1</string>
<string name="settings_tab_2_summary">Tab 2</string> <string name="settings_tab_2_summary">Tab 2</string>
<string name="settings_tab_3_summary">Tab 3</string> <string name="settings_tab_3_summary">Tab 3</string>