From 73c4ead2c05609b20bd1a6cabb45f4fe61a9b042 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 24 Sep 2020 22:27:05 +0800 Subject: Add favorite subreddits to the navigation drawer. Add favorite and subscriptions header to the navigation drawer. --- .../infinityforreddit/Activity/MainActivity.java | 13 ++-- .../NavigationDrawerRecyclerViewAdapter.java | 84 ++++++++++++++++++---- .../Settings/CustomizeMainPageTabsFragment.java | 3 + 3 files changed, 81 insertions(+), 19 deletions(-) (limited to 'app/src/main/java') 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 6dd98c4c..4387387b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java @@ -678,13 +678,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb sectionsPagerAdapter.setSubscribedSubreddits(subscribedSubredditData); } }); - if (mShowFavoriteSubscribedSubreddits) { - subscribedSubredditViewModel.getAllFavoriteSubscribedSubreddits().observe(this, subscribedSubredditData -> { - if (sectionsPagerAdapter != null) { - sectionsPagerAdapter.setFavoriteSubscribedSubreddits(subscribedSubredditData); - } - }); - } + subscribedSubredditViewModel.getAllFavoriteSubscribedSubreddits().observe(this, subscribedSubredditData -> { + adapter.setFavoriteSubscribedSubreddits(subscribedSubredditData); + if (mShowFavoriteSubscribedSubreddits && sectionsPagerAdapter != null) { + sectionsPagerAdapter.setFavoriteSubscribedSubreddits(subscribedSubredditData); + } + }); accountViewModel = new ViewModelProvider(this, new AccountViewModel.Factory(getApplication(), mRedditDataRoomDatabase, mAccountName)).get(AccountViewModel.class); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java index 088a576c..d8147480 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java @@ -50,8 +50,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter favoriteSubscribedSubreddits; private ArrayList subscribedSubreddits; private ArrayList accounts; private int primaryTextColor; @@ -94,14 +96,24 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter(); + subscribedSubreddits = new ArrayList<>(); } @Override public int getItemViewType(int position) { if (isInMainPage) { if (isLoggedIn) { - if (position >= CURRENT_MENU_ITEMS) { - return VIEW_TYPE_SUBSCRIBED_SUBREDDIT; + if (position == CURRENT_MENU_ITEMS) { + return VIEW_TYPE_MENU_GROUP_TITLE; + } else if (!favoriteSubscribedSubreddits.isEmpty() && position == CURRENT_MENU_ITEMS + favoriteSubscribedSubreddits.size() + 1) { + return VIEW_TYPE_MENU_GROUP_TITLE; + } else if (position > CURRENT_MENU_ITEMS) { + if (!favoriteSubscribedSubreddits.isEmpty() && position <= CURRENT_MENU_ITEMS + 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) { @@ -153,6 +165,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter itemClickListener.onMenuClick(finalStringId)); } } + } else if (holder instanceof FavoriteSubscribedThingViewHolder) { + SubscribedSubredditData subreddit = favoriteSubscribedSubreddits.get(position - CURRENT_MENU_ITEMS - 1); + String subredditName = subreddit.getName(); + String iconUrl = subreddit.getIconUrl(); + ((FavoriteSubscribedThingViewHolder) holder).subredditNameTextView.setText(subredditName); + if (iconUrl != null && !iconUrl.equals("")) { + glide.load(iconUrl) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((FavoriteSubscribedThingViewHolder) holder).iconGifImageView); + } else { + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((FavoriteSubscribedThingViewHolder) holder).iconGifImageView); + } + + ((FavoriteSubscribedThingViewHolder) holder).itemView.setOnClickListener(view -> { + itemClickListener.onSubscribedSubredditClick(subredditName); + }); } else if (holder instanceof SubscribedThingViewHolder) { - SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS); + SubscribedSubredditData subreddit = favoriteSubscribedSubreddits.isEmpty() ? subscribedSubreddits.get(position - CURRENT_MENU_ITEMS - 1) + : subscribedSubreddits.get(position - CURRENT_MENU_ITEMS - favoriteSubscribedSubreddits.size() - 2); String subredditName = subreddit.getName(); String iconUrl = subreddit.getIconUrl(); ((SubscribedThingViewHolder) holder).subredditNameTextView.setText(subredditName); @@ -418,11 +460,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter favoriteSubscribedSubreddits) { + this.favoriteSubscribedSubreddits = (ArrayList) favoriteSubscribedSubreddits; + if (isInMainPage) { + notifyDataSetChanged(); + } + } + public void setSubscribedSubreddits(List subscribedSubreddits) { this.subscribedSubreddits = (ArrayList) subscribedSubreddits; if (isInMainPage) { @@ -564,6 +611,19 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter