From 0c251c972c61867f7ebe802390df2ace9c19761f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 24 Sep 2020 19:01:54 +0800 Subject: [PATCH] Support showing favorite subscribed subreddits in the main page. --- .../Activity/MainActivity.java | 52 ++++++++++++++----- .../CustomizeMainPageTabsFragment.java | 12 ++++- .../Utils/SharedPreferencesUtils.java | 1 + .../fragment_customize_main_page_tabs.xml | 31 +++++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 83 insertions(+), 14 deletions(-) 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 be8799c6..6dd98c4c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -206,6 +206,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private boolean mConfirmToExit; private boolean mLockBottomAppBar; private boolean mDisableSwipingBetweenTabs; + private boolean mShowFavoriteSubscribedSubreddits; private boolean mShowSubscribedSubreddits; @Override @@ -602,13 +603,15 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb navDrawerRecyclerView.setAdapter(adapter); 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); - sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), tabCount, mShowSubscribedSubreddits); + sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), tabCount, + mShowFavoriteSubscribedSubreddits, mShowSubscribedSubreddits); viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setOffscreenPageLimit(1); viewPager2.setUserInputEnabled(!mDisableSwipingBetweenTabs); if (mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_TAB_NAMES, true)) { - if (mShowSubscribedSubreddits) { + if (mShowFavoriteSubscribedSubreddits || mShowSubscribedSubreddits) { tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE); } else { 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))); break; } - if (position >= tabCount && mShowSubscribedSubreddits && sectionsPagerAdapter != null) { + if (position >= tabCount && (mShowFavoriteSubscribedSubreddits || mShowSubscribedSubreddits) && sectionsPagerAdapter != null) { + List favoriteSubscribedSubreddits = sectionsPagerAdapter.favoriteSubscribedSubreddits; List subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits; - if (position - tabCount < subscribedSubreddits.size()) { - tab.setText(subscribedSubreddits.get(position - tabCount).getName()); + if (position - tabCount < favoriteSubscribedSubreddits.size()) { + 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); } }); + if (mShowFavoriteSubscribedSubreddits) { + subscribedSubredditViewModel.getAllFavoriteSubscribedSubreddits().observe(this, subscribedSubredditData -> { + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.setFavoriteSubscribedSubreddits(subscribedSubredditData); + } + }); + } accountViewModel = new ViewModelProvider(this, 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 { int tabCount; + boolean showFavoriteSubscribedSubreddits; boolean showSubscribedSubreddits; + List favoriteSubscribedSubreddits; List subscribedSubreddits; - SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, int tabCount, boolean showSubscribedSubreddits) { + SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, int tabCount, boolean showFavoriteSubscribedSubreddits, boolean showSubscribedSubreddits) { super(fm, lifecycle); this.tabCount = tabCount; + favoriteSubscribedSubreddits = new ArrayList<>(); subscribedSubreddits = new ArrayList<>(); + this.showFavoriteSubscribedSubreddits = showFavoriteSubscribedSubreddits; 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, ""); return generatePostFragment(postType, name); } else { - if (showSubscribedSubreddits) { - if (position >= tabCount && position - tabCount < subscribedSubreddits.size()) { + if (showFavoriteSubscribedSubreddits) { + if (position >= tabCount && position - tabCount < favoriteSubscribedSubreddits.size()) { 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); } } @@ -1062,6 +1086,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } } + public void setFavoriteSubscribedSubreddits(List favoriteSubscribedSubreddits) { + this.favoriteSubscribedSubreddits = favoriteSubscribedSubreddits; + notifyDataSetChanged(); + } + public void setSubscribedSubreddits(List subscribedSubreddits) { this.subscribedSubreddits = subscribedSubreddits; notifyDataSetChanged(); @@ -1136,10 +1165,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (mAccessToken == null) { return 2; } - if (showSubscribedSubreddits) { - return tabCount + subscribedSubreddits.size(); - } - return tabCount; + return tabCount + favoriteSubscribedSubreddits.size() + subscribedSubreddits.size(); } @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 a07ad3c4..1dbb9a4e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java @@ -103,6 +103,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { LinearLayout showSubscribedSubredditsLinearLayout; @BindView(R.id.show_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment) 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 @Named("main_activity_tabs") SharedPreferences sharedPreferences; @@ -153,7 +157,7 @@ public class CustomizeMainPageTabsFragment extends Fragment { divider4.setVisibility(View.GONE); moreTabsGroupSummaryTextView.setVisibility(View.GONE); showSubscribedSubredditsLinearLayout.setVisibility(View.GONE); - showSubscribedSubredditsSwitchMaterial.setVisibility(View.GONE); + showFavoriteSubscribedSubredditsLinearLayout.setVisibility(View.GONE); return rootView; } @@ -482,6 +486,12 @@ public class CustomizeMainPageTabsFragment extends Fragment { 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; } 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 fd5e9a47..8d08c696 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -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_USER = 5; 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_BASE = "_nsfw"; diff --git a/app/src/main/res/layout/fragment_customize_main_page_tabs.xml b/app/src/main/res/layout/fragment_customize_main_page_tabs.xml index 9a91549a..c9991b92 100644 --- a/app/src/main/res/layout/fragment_customize_main_page_tabs.xml +++ b/app/src/main/res/layout/fragment_customize_main_page_tabs.xml @@ -447,6 +447,37 @@ android:textSize="?attr/font_default" android:fontFamily="?attr/font_family" /> + + + + + + + + Tab 2 Tab 3 More Tabs + Show Favorite Subscribed Subreddits Show Subscribed Subreddits Title Type