From cf1d14264d305f079f6d1aaa93dd0d499eb48ade Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 24 Sep 2020 15:20:11 +0800 Subject: Support showing subscribed subreddits in the main page. --- .../infinityforreddit/Activity/MainActivity.java | 48 +++++++++++++++++++--- .../Settings/CustomizeMainPageTabsFragment.java | 20 +++++++++ .../Utils/SharedPreferencesUtils.java | 1 + 3 files changed, 64 insertions(+), 5 deletions(-) (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit') 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 1d05e6d6..384f7621 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -52,6 +52,7 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.TimeUnit; import javax.inject.Inject; @@ -205,6 +206,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb private boolean mConfirmToExit; private boolean mLockBottomAppBar; private boolean mDisableSwipingBetweenTabs; + private boolean mShowSubscribedSubreddits; @Override protected void onCreate(Bundle savedInstanceState) { @@ -599,10 +601,16 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this)); navDrawerRecyclerView.setAdapter(adapter); - sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle()); + mShowSubscribedSubreddits = mMainActivityTabsSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false); + sectionsPagerAdapter = new SectionsPagerAdapter(fragmentManager, getLifecycle(), mShowSubscribedSubreddits); viewPager2.setAdapter(sectionsPagerAdapter); - viewPager2.setOffscreenPageLimit(3); + 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) { @@ -625,6 +633,12 @@ 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 >= 3 && mShowSubscribedSubreddits && sectionsPagerAdapter != null) { + List subscribedSubreddits = sectionsPagerAdapter.subscribedSubreddits; + if (position - 3 < subscribedSubreddits.size()) { + tab.setText(subscribedSubreddits.get(position - 3).getName()); + } + } } }).attach(); @@ -647,7 +661,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb new SubscribedSubredditViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)) .get(SubscribedSubredditViewModel.class); subscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, - subscribedSubredditData -> adapter.setSubscribedSubreddits(subscribedSubredditData)); + subscribedSubredditData -> { + adapter.setSubscribedSubreddits(subscribedSubredditData); + if (mShowSubscribedSubreddits && sectionsPagerAdapter != null) { + sectionsPagerAdapter.setSubscribedSubreddits(subscribedSubredditData); + } + }); accountViewModel = new ViewModelProvider(this, new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class); @@ -978,8 +997,13 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } private class SectionsPagerAdapter extends FragmentStateAdapter { - SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) { + boolean showSubscribedSubreddits; + List subscribedSubreddits; + + SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle, boolean showSubscribedSubreddits) { super(fm, lifecycle); + subscribedSubreddits = new ArrayList<>(); + this.showSubscribedSubreddits = showSubscribedSubreddits; } @NonNull @@ -1018,14 +1042,25 @@ 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 > 2 && position - 3 < subscribedSubreddits.size()) { + int postType = SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT; + String name = subscribedSubreddits.get(position - 3).getName(); + return generatePostFragment(postType, name); + } + } int postType = mMainActivityTabsSharedPreferences.getInt((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_POST_TYPE, SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_ALL); String name = mMainActivityTabsSharedPreferences.getString((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.MAIN_PAGE_TAB_3_NAME, ""); return generatePostFragment(postType, name); } } - private Fragment generatePostFragment(int postType, String name) { + public void setSubscribedSubreddits(List subscribedSubreddits) { + this.subscribedSubreddits = subscribedSubreddits; + notifyDataSetChanged(); + } + private Fragment generatePostFragment(int postType, String name) { if (postType == SharedPreferencesUtils.MAIN_PAGE_TAB_POST_TYPE_HOME) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); @@ -1094,6 +1129,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (mAccessToken == null) { return 2; } + if (showSubscribedSubreddits) { + return 3 + subscribedSubreddits.size(); + } return 3; } 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 68d324b8..fe75de74 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Settings/CustomizeMainPageTabsFragment.java @@ -16,6 +16,7 @@ import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.switchmaterial.SwitchMaterial; import javax.inject.Inject; import javax.inject.Named; @@ -86,6 +87,14 @@ public class CustomizeMainPageTabsFragment extends Fragment { TextView tab3NameTitleTextView; @BindView(R.id.tab_3_name_summary_text_view_customize_main_page_tabs_fragment) TextView tab3NameSummaryTextView; + @BindView(R.id.divider_4_customize_main_page_tabs_fragment) + View divider4; + @BindView(R.id.more_tabs_group_summary_customize_main_page_tabs_fragment) + TextView moreTabsGroupSummaryTextView; + @BindView(R.id.show_subscribed_subreddits_linear_layout_customize_main_page_tabs_fragment) + LinearLayout showSubscribedSubredditsLinearLayout; + @BindView(R.id.show_subscribed_subreddits_switch_material_customize_main_page_tabs_fragment) + SwitchMaterial showSubscribedSubredditsSwitchMaterial; @Inject @Named("main_activity_tabs") SharedPreferences sharedPreferences; @@ -130,6 +139,10 @@ public class CustomizeMainPageTabsFragment extends Fragment { tab3GroupSummaryTextView.setVisibility(View.GONE); tab3TitleLinearLayout.setVisibility(View.GONE); tab3TypeLinearLayout.setVisibility(View.GONE); + divider4.setVisibility(View.GONE); + moreTabsGroupSummaryTextView.setVisibility(View.GONE); + showSubscribedSubredditsLinearLayout.setVisibility(View.GONE); + showSubscribedSubredditsSwitchMaterial.setVisibility(View.GONE); return rootView; } @@ -433,6 +446,13 @@ public class CustomizeMainPageTabsFragment extends Fragment { .show(); }); + showSubscribedSubredditsSwitchMaterial.setChecked(sharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MAIN_PAGE_SHOW_SUBSCRIBED_SUBREDDITS, false)); + 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 598c3d98..20f56eb7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -146,6 +146,7 @@ public class SharedPreferencesUtils { public static final int MAIN_PAGE_TAB_POST_TYPE_SUBREDDIT = 3; 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 NSFW_AND_SPOILER_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.nsfw_and_spoiler"; public static final String NSFW_BASE = "_nsfw"; -- cgit v1.2.3