From 67fa19cdc147a18786718869e5ac96746117a3ca Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 14 Mar 2021 20:13:29 +0800 Subject: [PATCH] Collapsing sections in the navigation drawer is available. --- .../infinityforreddit/AppModule.java | 6 + .../activities/MainActivity.java | 5 +- .../NavigationDrawerRecyclerViewAdapter.java | 181 ++++++++++++++++-- .../utils/SharedPreferencesUtils.java | 9 + 4 files changed, 184 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index b3bb499c..845f1b49 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -247,6 +247,12 @@ class AppModule { return mApplication.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); } + @Provides + @Named("navigation_drawer") + SharedPreferences provideNavigationDrawerSharedPreferences() { + return mApplication.getSharedPreferences(SharedPreferencesUtils.NAVIGATION_DRAWER_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE); + } + @Provides @Singleton CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index ac4308ca..1cf15575 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -198,6 +198,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Named("current_account") SharedPreferences mCurrentAccountSharedPreferences; @Inject + @Named("navigation_drawer") + SharedPreferences mNavigationDrawerSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; @Inject Executor mExecutor; @@ -676,7 +679,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb fab.setVisibility(View.VISIBLE); adapter = new NavigationDrawerRecyclerViewAdapter(this, mSharedPreferences, - mNsfwAndSpoilerSharedPreferences, mCustomThemeWrapper, mAccountName, + mNsfwAndSpoilerSharedPreferences, mNavigationDrawerSharedPreferences, mCustomThemeWrapper, mAccountName, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() { @Override public void onMenuClick(int stringId) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java index d6de3458..3780f24d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java @@ -29,9 +29,9 @@ import java.util.concurrent.Executor; import butterknife.BindView; import butterknife.ButterKnife; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import pl.droidsonroids.gif.GifImageView; @@ -55,6 +55,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter favoriteSubscribedSubreddits; private ArrayList subscribedSubreddits; private ArrayList accounts; @@ -79,6 +89,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter CURRENT_MENU_ITEMS) { - if (!favoriteSubscribedSubreddits.isEmpty() && position <= CURRENT_MENU_ITEMS + favoriteSubscribedSubreddits.size()) { + } else if (position > CURRENT_MENU_ITEMS - (collapseAccountSection ? ACCOUNT_SECTION_ITEMS : 0) + - (collapsePostSection ? POST_SECTION_ITEMS : 0) + - (collapsePreferencesSection ? PREFERENCES_SECTION_ITEMS : 0)) { + if (!favoriteSubscribedSubreddits.isEmpty() && !collapseFavoriteSubredditsSection && position <= CURRENT_MENU_ITEMS + - (collapseAccountSection ? ACCOUNT_SECTION_ITEMS : 0) + - (collapsePostSection ? POST_SECTION_ITEMS : 0) + - (collapsePreferencesSection ? PREFERENCES_SECTION_ITEMS : 0) + + favoriteSubscribedSubreddits.size()) { return VIEW_TYPE_FAVORITE_SUBSCRIBED_SUBREDDIT; } else { return VIEW_TYPE_SUBSCRIBED_SUBREDDIT; } } else if (position == 0) { return VIEW_TYPE_NAV_HEADER; - } else if (position == 1 || position == 6 || position == 12) { + } else if (position == 1 + || position == 6 - (collapseAccountSection ? ACCOUNT_SECTION_ITEMS : 0) + || position == 12 - (collapseAccountSection ? ACCOUNT_SECTION_ITEMS : 0) + - (collapsePostSection ? POST_SECTION_ITEMS : 0)) { return VIEW_TYPE_MENU_GROUP_TITLE; - } else if (position == 16) { + } else if (position == 16 - (collapseAccountSection ? ACCOUNT_SECTION_ITEMS : 0) + - (collapsePostSection ? POST_SECTION_ITEMS : 0) + - (collapsePreferencesSection ? PREFERENCES_SECTION_ITEMS : 0)) { return VIEW_TYPE_DIVIDER; } else { return VIEW_TYPE_MENU_ITEM; @@ -251,19 +287,80 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter { + switch (type) { + case 0: + if (collapseAccountSection) { + collapseAccountSection = !collapseAccountSection; + notifyItemRangeInserted(position + 1, ACCOUNT_SECTION_ITEMS); + } else { + collapseAccountSection = !collapseAccountSection; + notifyItemRangeRemoved(position + 1, ACCOUNT_SECTION_ITEMS); + } + break; + case 1: + if (collapsePostSection) { + collapsePostSection = !collapsePostSection; + notifyItemRangeInserted(position + 1, POST_SECTION_ITEMS); + } else { + collapsePostSection = !collapsePostSection; + notifyItemRangeRemoved(position + 1, POST_SECTION_ITEMS); + } + break; + case 2: + if (collapsePreferencesSection) { + collapsePreferencesSection = !collapsePreferencesSection; + notifyItemRangeInserted(position + 1, PREFERENCES_SECTION_ITEMS); + } else { + collapsePreferencesSection = !collapsePreferencesSection; + notifyItemRangeRemoved(position + 1, PREFERENCES_SECTION_ITEMS); + } + break; + case 3: + if (collapseFavoriteSubredditsSection) { + collapseFavoriteSubredditsSection = !collapseFavoriteSubredditsSection; + notifyItemRangeInserted(position + 1, favoriteSubscribedSubreddits.size()); + } else { + collapseFavoriteSubredditsSection = !collapseFavoriteSubredditsSection; + notifyItemRangeRemoved(position + 1, favoriteSubscribedSubreddits.size()); + } + break; + case 4: + if (collapseSubscribedSubredditsSection) { + collapseSubscribedSubredditsSection = !collapseSubscribedSubredditsSection; + notifyItemRangeInserted(position + 1, subscribedSubreddits.size()); + } else { + collapseSubscribedSubredditsSection = !collapseSubscribedSubredditsSection; + notifyItemRangeRemoved(position + 1, subscribedSubreddits.size()); + } + break; + } + notifyDataSetChanged(); + }); } else if (holder instanceof MenuItemViewHolder) { int stringId = 0; int drawableId = 0; @@ -271,7 +368,19 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter ACCOUNT_SECTION_ITEMS + 1) { + pseudoPosition += POST_SECTION_ITEMS; + } + } + switch (pseudoPosition) { case 2: stringId = R.string.profile; drawableId = R.drawable.ic_account_circle_24dp; @@ -413,7 +522,11 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter