Show favorite subreddits when selecting subreddits.

This commit is contained in:
Alex Ning 2020-02-13 20:47:37 +08:00
parent 805ce260a2
commit af6d364322
2 changed files with 110 additions and 57 deletions

View File

@ -56,22 +56,54 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
mOauthRetrofit = oauthRetrofit;
mRedditDataRoomDatabase = redditDataRoomDatabase;
this.accessToken = accessToken;
}
public SubscribedSubredditsRecyclerViewAdapter(Context context, boolean hasClearSelectionRow, ItemClickListener itemClickListener) {
public SubscribedSubredditsRecyclerViewAdapter(Context context, Retrofit oauthRetrofit,
RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, boolean hasClearSelectionRow,
ItemClickListener itemClickListener) {
this(context, oauthRetrofit, redditDataRoomDatabase, accessToken);
this.hasClearSelectionRow = hasClearSelectionRow;
this.itemClickListener = itemClickListener;
}
/*public SubscribedSubredditsRecyclerViewAdapter(Context context, boolean hasClearSelectionRow, ItemClickListener itemClickListener) {
mContext = context;
this.hasClearSelectionRow = hasClearSelectionRow;
glide = Glide.with(context.getApplicationContext());
this.itemClickListener = itemClickListener;
}
}*/
@Override
public int getItemViewType(int position) {
if (itemClickListener != null) {
return VIEW_TYPE_SUBREDDIT;
} else {
if (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) {
if (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) {
if (itemClickListener != null && !hasClearSelectionRow) {
if (position == 0) {
return VIEW_TYPE_SUBREDDIT;
} else if (position == 1) {
return VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER;
} else if (position == mFavoriteSubscribedSubredditData.size() + 2) {
return VIEW_TYPE_SUBREDDIT_DIVIDER;
} else if (position <= mFavoriteSubscribedSubredditData.size() + 1) {
return VIEW_TYPE_FAVORITE_SUBREDDIT;
} else {
return VIEW_TYPE_SUBREDDIT;
}
} else if (hasClearSelectionRow) {
if (position == 0) {
return VIEW_TYPE_SUBREDDIT;
} else if (position == 1) {
return VIEW_TYPE_SUBREDDIT;
} else if (position == 2) {
return VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER;
} else if (position == mFavoriteSubscribedSubredditData.size() + 3) {
return VIEW_TYPE_SUBREDDIT_DIVIDER;
} else if (position <= mFavoriteSubscribedSubredditData.size() + 2) {
return VIEW_TYPE_FAVORITE_SUBREDDIT;
} else {
return VIEW_TYPE_SUBREDDIT;
}
} else {
if (position == 0) {
return VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER;
} else if (position == mFavoriteSubscribedSubredditData.size() + 1) {
@ -81,9 +113,9 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
} else {
return VIEW_TYPE_SUBREDDIT;
}
} else {
return VIEW_TYPE_SUBREDDIT;
}
} else {
return VIEW_TYPE_SUBREDDIT;
}
}
@ -107,40 +139,38 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder instanceof SubredditViewHolder) {
String name;
String iconUrl;
if (itemClickListener != null) {
if (hasClearSelectionRow) {
if (viewHolder.getAdapterPosition() == 0) {
((SubredditViewHolder) viewHolder).subredditNameTextView.setText(R.string.all_subreddits);
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(null, null, false));
return;
} else if (viewHolder.getAdapterPosition() == 1) {
name = username;
iconUrl = userIconUrl;
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true));
if (hasClearSelectionRow && viewHolder.getAdapterPosition() == 0) {
((SubredditViewHolder) viewHolder).subredditNameTextView.setText(R.string.all_subreddits);
((SubredditViewHolder) viewHolder).favoriteImageView.setVisibility(View.GONE);
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(null, null, false));
return;
} else if (itemClickListener != null && !hasClearSelectionRow && viewHolder.getAdapterPosition() == 0) {
((SubredditViewHolder) viewHolder).favoriteImageView.setVisibility(View.GONE);
name = username;
iconUrl = userIconUrl;
} else if (hasClearSelectionRow && viewHolder.getAdapterPosition() == 1) {
((SubredditViewHolder) viewHolder).favoriteImageView.setVisibility(View.GONE);
name = username;
iconUrl = userIconUrl;
} else {
int offset;
if (itemClickListener != null) {
if (hasClearSelectionRow) {
offset = (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) ?
mFavoriteSubscribedSubredditData.size() + 4 : 0;
} else {
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getName();
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getIconUrl();
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false));
offset = (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) ?
mFavoriteSubscribedSubredditData.size() + 3 : 0;
}
} else {
if (viewHolder.getAdapterPosition() == 0) {
name = username;
iconUrl = userIconUrl;
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true));
} else {
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName();
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl();
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false));
}
offset = (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) ?
mFavoriteSubscribedSubredditData.size() + 2 : 0;
}
} else {
int offset = (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) ?
mFavoriteSubscribedSubredditData.size() + 2 : 0;
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getName();
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getIconUrl();
@ -203,6 +233,11 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
});
}
});
}
if (itemClickListener != null) {
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true));
} else {
viewHolder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name);
@ -223,20 +258,30 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
}
((SubredditViewHolder) viewHolder).subredditNameTextView.setText(name);
} else if (viewHolder instanceof FavoriteSubredditViewHolder) {
String name = mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName();
String iconUrl = mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl();
if(mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).isFavorite()) {
int offset;
if (itemClickListener != null) {
if (hasClearSelectionRow) {
offset = 3;
} else {
offset = 2;
}
} else {
offset = 1;
}
String name = mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getName();
String iconUrl = mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getIconUrl();
if(mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) {
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
} else {
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setOnClickListener(view -> {
if(mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).isFavorite()) {
if(mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) {
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).setFavorite(false);
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(false);
FavoriteThing.unfavoriteSubreddit(mOauthRetrofit, mRedditDataRoomDatabase, accessToken,
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1),
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset),
new FavoriteThing.FavoriteThingListener() {
@Override
public void success() {
@ -259,9 +304,9 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
});
} else {
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).setFavorite(true);
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(true);
FavoriteThing.favoriteSubreddit(mOauthRetrofit, mRedditDataRoomDatabase, accessToken,
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1),
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset),
new FavoriteThing.FavoriteThingListener() {
@Override
public void success() {
@ -284,11 +329,16 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
});
}
});
viewHolder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name);
mContext.startActivity(intent);
});
if (itemClickListener != null) {
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true));
} else {
viewHolder.itemView.setOnClickListener(view -> {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, name);
mContext.startActivity(intent);
});
}
if (iconUrl != null && !iconUrl.equals("")) {
glide.load(iconUrl)
@ -308,15 +358,16 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
@Override
public int getItemCount() {
if (mSubscribedSubredditData != null) {
if (itemClickListener != null) {
if (hasClearSelectionRow) {
return mSubscribedSubredditData.size() + 2;
} else {
return mSubscribedSubredditData.size() + 1;
}
}
if(mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) {
if (itemClickListener != null) {
if (hasClearSelectionRow) {
return mSubscribedSubredditData.size() > 0 ?
mFavoriteSubscribedSubredditData.size() + mSubscribedSubredditData.size() + 4 : 0;
} else {
return mSubscribedSubredditData.size() > 0 ?
mFavoriteSubscribedSubredditData.size() + mSubscribedSubredditData.size() + 3 : 0;
}
}
return mSubscribedSubredditData.size() > 0 ?
mFavoriteSubscribedSubredditData.size() + mSubscribedSubredditData.size() + 2 : 0;
}
@ -330,6 +381,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
if(holder instanceof SubredditViewHolder) {
glide.clear(((SubredditViewHolder) holder).iconGifImageView);
((SubredditViewHolder) holder).favoriteImageView.setVisibility(View.VISIBLE);
} else if (holder instanceof FavoriteSubredditViewHolder) {
glide.clear(((FavoriteSubredditViewHolder) holder).iconGifImageView);
}

View File

@ -112,7 +112,8 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra
SubscribedSubredditsRecyclerViewAdapter adapter;
if (getArguments().getBoolean(EXTRA_IS_SUBREDDIT_SELECTION)) {
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, getArguments().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION),
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, mOauthRetrofit, mRedditDataRoomDatabase,
accessToken, getArguments().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION),
(name, iconUrl, subredditIsUser) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl, subredditIsUser));
} else {
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, mOauthRetrofit, mRedditDataRoomDatabase, accessToken);