Support showing favorite subscribed subreddits in the main page.

This commit is contained in:
Alex Ning 2020-09-24 19:01:54 +08:00
parent d6051ca932
commit 0c251c972c
5 changed files with 83 additions and 14 deletions

View File

@ -206,6 +206,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private boolean mConfirmToExit; private boolean mConfirmToExit;
private boolean mLockBottomAppBar; private boolean mLockBottomAppBar;
private boolean mDisableSwipingBetweenTabs; private boolean mDisableSwipingBetweenTabs;
private boolean mShowFavoriteSubscribedSubreddits;
private boolean mShowSubscribedSubreddits; private boolean mShowSubscribedSubreddits;
@Override @Override
@ -602,13 +603,15 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
navDrawerRecyclerView.setAdapter(adapter); navDrawerRecyclerView.setAdapter(adapter);
int tabCount = mMainActivityTabsSharedPreferences.getInt((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_COUNT, 3); int tabCount = mMainActivityTabsSharedPreferences.getInt((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_COUNT, 3);
mShowFavoriteSubscribedSubreddits = mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, false);
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(), tabCount, mShowSubscribedSubreddits); sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), tabCount,
mShowFavoriteSubscribedSubreddits, mShowSubscribedSubreddits);
viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setAdapter(sectionsPagerAdapter);
viewPager2.setOffscreenPageLimit(1); viewPager2.setOffscreenPageLimit(1);
viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs); viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs);
if (mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, true)) { if (mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, true)) {
if (mShowSubscribedSubreddits) { if (mShowFavoriteSubscribedSubreddits || mShowSubscribedSubreddits) {
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
} else { } else {
tabLayout.setTabMode(TabLayout.MODE_FIXED); tabLayout.setTabMode(TabLayout.MODE_FIXED);
@ -635,10 +638,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all))); tab.setText(mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_TITLE, getString(R.string.all)));
break; break;
} }
if (position >= tabCount && mShowSubscribedSubreddits && sectionsPagerAdapter != null) { if (position >= tabCount && (mShowFavoriteSubscribedSubreddits || mShowSubscribedSubreddits) && sectionsPagerAdapter != null) {
List<SubscribedSubredditData> favoriteSubscribedSubreddits = sectionsPagerAdapter.favoriteSubscribedSubreddits;
List<SubscribedSubredditData> subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits; List<SubscribedSubredditData> subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits;
if (position - tabCount < subscribedSubreddits.size()) { if (position - tabCount < favoriteSubscribedSubreddits.size()) {
tab.setText(subscribedSubreddits.get(position - tabCount).getName()); tab.setText(favoriteSubscribedSubreddits.get(position - tabCount).getName());
} else if (position - tabCount - favoriteSubscribedSubreddits.size() < subscribedSubreddits.size()) {
tab.setText(subscribedSubreddits.get(position - tabCount - favoriteSubscribedSubreddits.size()).getName());
} }
} }
} }
@ -672,6 +678,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
sectionsPagerAdapter.setSubscribedSubreddits(subscribedSubredditData); sectionsPagerAdapter.setSubscribedSubreddits(subscribedSubredditData);
} }
}); });
if (mShowFavoriteSubscribedSubreddits) {
subscribedSubredditViewModel.getAllFavoriteSubscribedSubreddits().observe(this, subscribedSubredditData -> {
if (sectionsPagerAdapter != null) {
sectionsPagerAdapter.setFavoriteSubscribedSubreddits(subscribedSubredditData);
}
});
}
accountViewModel = new ViewModelProvider(this, accountViewModel = new ViewModelProvider(this,
new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class); new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class);
@ -1003,13 +1016,17 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private class SectionsPagerAdapter extends FragmentStateAdapter { private class SectionsPagerAdapter extends FragmentStateAdapter {
int tabCount; int tabCount;
boolean showFavoriteSubscribedSubreddits;
boolean showSubscribedSubreddits; boolean showSubscribedSubreddits;
List<SubscribedSubredditData> favoriteSubscribedSubreddits;
List<SubscribedSubredditData> subscribedSubreddits; List<SubscribedSubredditData> subscribedSubreddits;
SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, int tabCount, boolean showSubscribedSubreddits) { SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, int tabCount, boolean showFavoriteSubscribedSubreddits, boolean showSubscribedSubreddits) {
super(fm, lifecycle); super(fm, lifecycle);
this.tabCount = tabCount; this.tabCount = tabCount;
favoriteSubscribedSubreddits = new ArrayList<>();
subscribedSubreddits = new ArrayList<>(); subscribedSubreddits = new ArrayList<>();
this.showFavoriteSubscribedSubreddits = showFavoriteSubscribedSubreddits;
this.showSubscribedSubreddits = showSubscribedSubreddits; this.showSubscribedSubreddits = showSubscribedSubreddits;
} }
@ -1049,10 +1066,17 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
String name = mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_NAME, ""); String name = mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_2_NAME, "");
return generatePostFragment(postType, name); return generatePostFragment(postType, name);
} else { } else {
if (showSubscribedSubreddits) { if (showFavoriteSubscribedSubreddits) {
if (position >= tabCount && position - tabCount < subscribedSubreddits.size()) { if (position >= tabCount && position - tabCount < favoriteSubscribedSubreddits.size()) {
int postType = SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT; int postType = SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT;
String name = subscribedSubreddits.get(position - tabCount).getName(); String name = favoriteSubscribedSubreddits.get(position - tabCount).getName();
return generatePostFragment(postType, name);
}
}
if (showSubscribedSubreddits) {
if (position >= tabCount && position - tabCount - favoriteSubscribedSubreddits.size() < subscribedSubreddits.size()) {
int postType = SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT;
String name = subscribedSubreddits.get(position - tabCount - favoriteSubscribedSubreddits.size()).getName();
return generatePostFragment(postType, name); return generatePostFragment(postType, name);
} }
} }
@ -1062,6 +1086,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
} }
} }
public void setFavoriteSubscribedSubreddits(List<SubscribedSubredditData> favoriteSubscribedSubreddits) {
this.favoriteSubscribedSubreddits = favoriteSubscribedSubreddits;
notifyDataSetChanged();
}
public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) { public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) {
this.subscribedSubreddits = subscribedSubreddits; this.subscribedSubreddits = subscribedSubreddits;
notifyDataSetChanged(); notifyDataSetChanged();
@ -1136,10 +1165,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
if (mAccessToken == null) { if (mAccessToken == null) {
return 2; return 2;
} }
if (showSubscribedSubreddits) { return tabCount + favoriteSubscribedSubreddits.size() + subscribedSubreddits.size();
return tabCount + subscribedSubreddits.size();
}
return tabCount;
} }
@Nullable @Nullable

View File

@ -103,6 +103,10 @@ public class CustomizeMainPageTabsFragment extends Fragment {
LinearLayout showSubscribedSubredditsLinearLayout; LinearLayout showSubscribedSubredditsLinearLayout;
@BindView(R.id.show_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment) @BindView(R.id.show_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment)
SwitchMaterial showSubscribedSubredditsSwitchMaterial; SwitchMaterial showSubscribedSubredditsSwitchMaterial;
@BindView(R.id.show_favorite_subscribed_subreddits_linear_layout_customize_main_page_tabs_fragment)
LinearLayout showFavoriteSubscribedSubredditsLinearLayout;
@BindView(R.id.show_favorite_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment)
SwitchMaterial showFavoriteSubscribedSubredditsSwitchMaterial;
@Inject @Inject
@Named("main_activity_tabs") @Named("main_activity_tabs")
SharedPreferences sharedPreferences; SharedPreferences sharedPreferences;
@ -153,7 +157,7 @@ public class CustomizeMainPageTabsFragment extends Fragment {
divider4.setVisibility(View.GONE); divider4.setVisibility(View.GONE);
moreTabsGroupSummaryTextView.setVisibility(View.GONE); moreTabsGroupSummaryTextView.setVisibility(View.GONE);
showSubscribedSubredditsLinearLayout.setVisibility(View.GONE); showSubscribedSubredditsLinearLayout.setVisibility(View.GONE);
showSubscribedSubredditsSwitchMaterial.setVisibility(View.GONE); showFavoriteSubscribedSubredditsLinearLayout.setVisibility(View.GONE);
return rootView; return rootView;
} }
@ -482,6 +486,12 @@ public class CustomizeMainPageTabsFragment extends Fragment {
showSubscribedSubredditsSwitchMaterial.performClick(); showSubscribedSubredditsSwitchMaterial.performClick();
}); });
showFavoriteSubscribedSubredditsSwitchMaterial.setChecked(sharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, false));
showFavoriteSubscribedSubredditsSwitchMaterial.setOnCheckedChangeListener((compoundButton, b) -> sharedPreferences.edit().putBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS, b).apply());
showFavoriteSubscribedSubredditsLinearLayout.setOnClickListener(view -> {
showFavoriteSubscribedSubredditsSwitchMaterial.performClick();
});
return rootView; return rootView;
} }

View File

@ -149,6 +149,7 @@ public class SharedPreferencesUtils {
public static final int MAIN_PAGE_TAB_POST_TYPE_MULTIREDDIT = 4; public static final int MAIN_PAGE_TAB_POST_TYPE_MULTIREDDIT = 4;
public static final int MAIN_PAGE_TAB_POST_TYPE_USER = 5; public static final int MAIN_PAGE_TAB_POST_TYPE_USER = 5;
public static final String MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS = "_main_page_show_subscribed_subreddits"; public static final String MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS = "_main_page_show_subscribed_subreddits";
public static final String MAIN_PAGE_SHOW_FAVORITE_SUBSCRIBED_SUBREDDITS = "_main_page_show_favorite_subscribed_subreddits";
public static final String NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.nsfw_and_spoiler"; public static final String NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.nsfw_and_spoiler";
public static final String NSFW_BASE = "_nsfw"; public static final String NSFW_BASE = "_nsfw";

View File

@ -447,6 +447,37 @@
android:textSize="?attr/font_default" android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family" /> android:fontFamily="?attr/font_family" />
<LinearLayout
android:id="@+id/show_favorite_subscribed_subreddits_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_more_tabs_show_favorite_subscribed_subreddits_title"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_16"
android:fontFamily="?attr/font_family" />
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/show_favorite_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/show_subscribed_subreddits_linear_layout_customize_main_page_tabs_fragment" android:id="@+id/show_subscribed_subreddits_linear_layout_customize_main_page_tabs_fragment"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -453,6 +453,7 @@
<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>
<string name="settings_more_tabs_summary">More Tabs</string> <string name="settings_more_tabs_summary">More Tabs</string>
<string name="settings_more_tabs_show_favorite_subscribed_subreddits_title">Show Favorite Subscribed Subreddits</string>
<string name="settings_more_tabs_show_subscribed_subreddits_title">Show Subscribed Subreddits</string> <string name="settings_more_tabs_show_subscribed_subreddits_title">Show Subscribed Subreddits</string>
<string name="settings_tab_title">Title</string> <string name="settings_tab_title">Title</string>
<string name="settings_tab_post_type">Type</string> <string name="settings_tab_post_type">Type</string>