Favorite and unfavorite subreddits are now available.

This commit is contained in:
Alex Ning 2019-10-09 16:40:45 +08:00
parent c4fd313839
commit 77a31056b2
21 changed files with 526 additions and 78 deletions

View File

@ -5,6 +5,7 @@ import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
@ -16,25 +17,45 @@ import com.bumptech.glide.request.RequestOptions;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import ml.docilealligator.infinityforreddit.Activity.ViewSubredditDetailActivity;
import ml.docilealligator.infinityforreddit.FavoriteThing;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData;
import pl.droidsonroids.gif.GifImageView;
import retrofit2.Retrofit;
public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER = 0;
private static final int VIEW_TYPE_FAVORITE_SUBREDDIT = 1;
private static final int VIEW_TYPE_SUBREDDIT_DIVIDER = 2;
private static final int VIEW_TYPE_SUBREDDIT = 3;
private Context mContext;
private Retrofit mOauthRetrofit;
private RedditDataRoomDatabase mRedditDataRoomDatabase;
private List<SubscribedSubredditData> mSubscribedSubredditData;
private List<SubscribedSubredditData> mFavoriteSubscribedSubredditData;
private RequestManager glide;
private ItemClickListener itemClickListener;
private String accessToken;
private String username;
private String userIconUrl;
private boolean hasClearSelectionRow;
public SubscribedSubredditsRecyclerViewAdapter(Context context) {
public SubscribedSubredditsRecyclerViewAdapter(Context context, Retrofit oauthRetrofit,
RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken) {
mContext = context;
glide = Glide.with(context.getApplicationContext());
mOauthRetrofit = oauthRetrofit;
mRedditDataRoomDatabase = redditDataRoomDatabase;
this.accessToken = accessToken;
}
public SubscribedSubredditsRecyclerViewAdapter(Context context, boolean hasClearSelectionRow, ItemClickListener itemClickListener) {
@ -44,66 +65,239 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
this.itemClickListener = itemClickListener;
}
@Override
public int getItemViewType(int position) {
if (itemClickListener != null) {
return VIEW_TYPE_SUBREDDIT;
} else {
if (mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) {
if (position == 0) {
return VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER;
} else if (position == mFavoriteSubscribedSubredditData.size() + 1) {
return VIEW_TYPE_SUBREDDIT_DIVIDER;
} else if (position <= mFavoriteSubscribedSubredditData.size()) {
return VIEW_TYPE_FAVORITE_SUBREDDIT;
} else {
return VIEW_TYPE_SUBREDDIT;
}
} else {
return VIEW_TYPE_SUBREDDIT;
}
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new SubredditViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_subscribed_thing, viewGroup, false));
switch (i) {
case VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER:
return new FavoriteSubredditsDividerViewHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_favorite_subreddits_divider, viewGroup, false));
case VIEW_TYPE_FAVORITE_SUBREDDIT:
return new FavoriteSubredditViewHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_subscribed_thing, viewGroup, false));
case VIEW_TYPE_SUBREDDIT_DIVIDER:
return new AllSubredditsDividerViewHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_favorite_subreddits_divider, viewGroup, false));
default:
return new SubredditViewHolder(LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_subscribed_thing, viewGroup, false));
}
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int i) {
String name;
String iconUrl;
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 (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));
} else {
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getName();
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getIconUrl();
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false));
}
} else {
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getName();
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 2).getIconUrl();
viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false));
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));
}
}
} 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));
}
}
} else {
name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName();
iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getIconUrl();
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();
if(mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) {
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
} else {
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
((SubredditViewHolder) viewHolder).favoriteImageView.setOnClickListener(view -> {
if(mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) {
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(false);
FavoriteThing.unfavoriteThing(mOauthRetrofit, mRedditDataRoomDatabase, accessToken,
mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), false,
new FavoriteThing.FavoriteThingListener() {
@Override
public void success() {
int position = viewHolder.getAdapterPosition() - offset;
if(position >= 0 && mSubscribedSubredditData.size() > position) {
mSubscribedSubredditData.get(position).setFavorite(false);
}
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
@Override
public void failed() {
int position = viewHolder.getAdapterPosition() - offset;
if(position >= 0 && mSubscribedSubredditData.size() > position) {
mSubscribedSubredditData.get(position).setFavorite(true);
}
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
}
});
} else {
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(true);
FavoriteThing.favoriteThing(mOauthRetrofit, mRedditDataRoomDatabase, accessToken,
mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), false,
new FavoriteThing.FavoriteThingListener() {
@Override
public void success() {
int position = viewHolder.getAdapterPosition() - offset;
if(position >= 0 && mSubscribedSubredditData.size() > position) {
mSubscribedSubredditData.get(position).setFavorite(true);
}
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
}
@Override
public void failed() {
int position = viewHolder.getAdapterPosition() - offset;
if(position >= 0 && mSubscribedSubredditData.size() > position) {
mSubscribedSubredditData.get(position).setFavorite(false);
}
((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
});
}
});
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)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(glide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((SubredditViewHolder) viewHolder).iconGifImageView);
} else {
glide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(((SubredditViewHolder) viewHolder).iconGifImageView);
}
((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()) {
((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()) {
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).setFavorite(false);
FavoriteThing.unfavoriteThing(mOauthRetrofit, mRedditDataRoomDatabase, accessToken,
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1), false,
new FavoriteThing.FavoriteThingListener() {
@Override
public void success() {
int position = viewHolder.getAdapterPosition() - 1;
if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) {
mFavoriteSubscribedSubredditData.get(position).setFavorite(false);
}
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
@Override
public void failed() {
int position = viewHolder.getAdapterPosition() - 1;
if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) {
mFavoriteSubscribedSubredditData.get(position).setFavorite(true);
}
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
}
});
} else {
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).setFavorite(true);
FavoriteThing.favoriteThing(mOauthRetrofit, mRedditDataRoomDatabase, accessToken,
mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1), false,
new FavoriteThing.FavoriteThingListener() {
@Override
public void success() {
int position = viewHolder.getAdapterPosition() - 1;
if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) {
mFavoriteSubscribedSubredditData.get(position).setFavorite(true);
}
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp);
}
@Override
public void failed() {
int position = viewHolder.getAdapterPosition() - 1;
if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) {
mFavoriteSubscribedSubredditData.get(position).setFavorite(false);
}
((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp);
}
});
}
});
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)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(glide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(((SubredditViewHolder) viewHolder).iconGifImageView);
} else {
glide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(((SubredditViewHolder) viewHolder).iconGifImageView);
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(((FavoriteSubredditViewHolder) viewHolder).iconGifImageView);
} else {
glide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(((FavoriteSubredditViewHolder) viewHolder).iconGifImageView);
}
((FavoriteSubredditViewHolder) viewHolder).subredditNameTextView.setText(name);
}
((SubredditViewHolder) viewHolder).subredditNameTextView.setText(name);
}
@Override
@ -117,6 +311,11 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
}
}
if(mFavoriteSubscribedSubredditData != null && mFavoriteSubscribedSubredditData.size() > 0) {
return mSubscribedSubredditData.size() > 0 ?
mFavoriteSubscribedSubredditData.size() + mSubscribedSubredditData.size() + 2 : 0;
}
return mSubscribedSubredditData.size();
}
return 0;
@ -124,7 +323,11 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
glide.clear(((SubredditViewHolder) holder).iconGifImageView);
if(holder instanceof SubredditViewHolder) {
glide.clear(((SubredditViewHolder) holder).iconGifImageView);
} else if (holder instanceof FavoriteSubredditViewHolder) {
glide.clear(((FavoriteSubredditViewHolder) holder).iconGifImageView);
}
}
public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) {
@ -132,6 +335,11 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
notifyDataSetChanged();
}
public void setFavoriteSubscribedSubreddits(List<SubscribedSubredditData> favoriteSubscribedSubredditData) {
mFavoriteSubscribedSubredditData = favoriteSubscribedSubredditData;
notifyDataSetChanged();
}
public void addUser(String username, String userIconUrl) {
this.username = username;
this.userIconUrl = userIconUrl;
@ -141,14 +349,51 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte
void onClick(String name, String iconUrl, boolean subredditIsUser);
}
private class SubredditViewHolder extends RecyclerView.ViewHolder {
private final GifImageView iconGifImageView;
private final TextView subredditNameTextView;
class SubredditViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit)
GifImageView iconGifImageView;
@BindView(R.id.subreddit_name_text_view_item_subscribed_subreddit)
TextView subredditNameTextView;
@BindView(R.id.favorite_image_view_item_subscribed_subreddit)
ImageView favoriteImageView;
SubredditViewHolder(View itemView) {
super(itemView);
iconGifImageView = itemView.findViewById(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit);
subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subscribed_subreddit);
ButterKnife.bind(this, itemView);
}
}
class FavoriteSubredditViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit)
GifImageView iconGifImageView;
@BindView(R.id.subreddit_name_text_view_item_subscribed_subreddit)
TextView subredditNameTextView;
@BindView(R.id.favorite_image_view_item_subscribed_subreddit)
ImageView favoriteImageView;
FavoriteSubredditViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
class FavoriteSubredditsDividerViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.divider_text_view_item_favorite_subreddits_divider) TextView dividerTextView;
FavoriteSubredditsDividerViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
dividerTextView.setText(R.string.favorites);
}
}
class AllSubredditsDividerViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.divider_text_view_item_favorite_subreddits_divider) TextView dividerTextView;
AllSubredditsDividerViewHolder(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
dividerTextView.setText(R.string.all);
}
}
}

View File

@ -21,10 +21,11 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
private SubscribedSubredditDao mSubscribedSubredditDao;
private SubscribedUserDao mUserDao;
private SubredditDao mSubredditDao;
private SubscribedSubredditData mSingleSubscribedSubredditData;
private List<SubscribedSubredditData> subscribedSubredditData;
private List<SubscribedUserData> subscribedUserData;
private List<SubredditData> subredditData;
private InsertSubscribedThingListener insertSubscribedThingListener;
private InsertSubscribedThingListener mInsertSubscribedThingListener;
public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, @Nullable String accountName,
List<SubscribedSubredditData> subscribedSubredditData,
List<SubscribedUserData> subscribedUserData,
@ -39,7 +40,17 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
this.subscribedSubredditData = subscribedSubredditData;
this.subscribedUserData = subscribedUserData;
this.subredditData = subredditData;
this.insertSubscribedThingListener = insertSubscribedThingListener;
mInsertSubscribedThingListener = insertSubscribedThingListener;
}
public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase,
SubscribedSubredditData subscribedSubredditData,
InsertSubscribedThingListener insertSubscribedThingListener) {
mRedditDataRoomDatabase = redditDataRoomDatabase;
mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao();
mAccountName = subscribedSubredditData.getUsername();
mSingleSubscribedSubredditData = subscribedSubredditData;
mInsertSubscribedThingListener = insertSubscribedThingListener;
}
@Override
@ -48,21 +59,25 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
return null;
}
if (subscribedSubredditData != null) {
for (SubscribedSubredditData s : subscribedSubredditData) {
mSubscribedSubredditDao.insert(s);
if(mSingleSubscribedSubredditData != null) {
mSubscribedSubredditDao.insert(mSingleSubscribedSubredditData);
} else {
if (subscribedSubredditData != null) {
for (SubscribedSubredditData s : subscribedSubredditData) {
mSubscribedSubredditDao.insert(s);
}
}
}
if (subscribedUserData != null) {
for (SubscribedUserData s : subscribedUserData) {
mUserDao.insert(s);
if (subscribedUserData != null) {
for (SubscribedUserData s : subscribedUserData) {
mUserDao.insert(s);
}
}
}
if (subredditData != null) {
for (SubredditData s : subredditData) {
mSubredditDao.insert(s);
if (subredditData != null) {
for (SubredditData s : subredditData) {
mSubredditDao.insert(s);
}
}
}
return null;
@ -70,7 +85,7 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void>
@Override
protected void onPostExecute(Void aVoid) {
insertSubscribedThingListener.insertSuccess();
mInsertSubscribedThingListener.insertSuccess();
}
public interface InsertSubscribedThingListener {

View File

@ -0,0 +1,77 @@
package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;
import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubscribedThingsAsyncTask;
import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
public class FavoriteThing {
public static void favoriteThing(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, SubscribedSubredditData subscribedSubredditData,
boolean isUser, FavoriteThingListener favoriteThingListener) {
Map<String, String> params = new HashMap<>();
if (isUser) {
params.put(RedditUtils.SR_NAME_KEY, "u_" + subscribedSubredditData.getName());
} else {
params.put(RedditUtils.SR_NAME_KEY, subscribedSubredditData.getName());
}
params.put(RedditUtils.MAKE_FAVORITE_KEY, "true");
oauthRetrofit.create(RedditAPI.class).favoriteThing(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new InsertSubscribedThingsAsyncTask(redditDataRoomDatabase, subscribedSubredditData,
favoriteThingListener::success).execute();
} else {
favoriteThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
favoriteThingListener.failed();
}
});
}
public static void unfavoriteThing(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase,
String accessToken, SubscribedSubredditData subscribedSubredditData,
boolean isUser, FavoriteThingListener favoriteThingListener) {
Map<String, String> params = new HashMap<>();
if (isUser) {
params.put(RedditUtils.SR_NAME_KEY, "u_" + subscribedSubredditData.getName());
} else {
params.put(RedditUtils.SR_NAME_KEY, subscribedSubredditData.getName());
}
params.put(RedditUtils.MAKE_FAVORITE_KEY, "false");
oauthRetrofit.create(RedditAPI.class).favoriteThing(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new InsertSubscribedThingsAsyncTask(redditDataRoomDatabase, subscribedSubredditData,
favoriteThingListener::success).execute();
} else {
favoriteThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
favoriteThingListener.failed();
}
});
}
public interface FavoriteThingListener {
void success();
void failed();
}
}

View File

@ -22,6 +22,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import javax.inject.Inject;
import javax.inject.Named;
import butterknife.BindView;
import butterknife.ButterKnife;
@ -31,6 +32,7 @@ import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditViewModel;
import retrofit2.Retrofit;
/**
@ -38,7 +40,8 @@ import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.Subscrib
*/
public class SubscribedSubredditsListingFragment extends Fragment {
public static final String EXTRA_ACCOUNT_NAME = "EAT";
public static final String EXTRA_ACCOUNT_NAME = "EAN";
public static final String EXTRA_ACCESS_TOKEN = "EAT";
public static final String EXTRA_ACCOUNT_PROFILE_IMAGE_URL = "EAPIU";
public static final String EXTRA_IS_SUBREDDIT_SELECTION = "EISS";
public static final String EXTRA_EXTRA_CLEAR_SELECTION = "EECS";
@ -50,6 +53,9 @@ public class SubscribedSubredditsListingFragment extends Fragment {
@BindView(R.id.no_subscriptions_image_view_subreddits_listing_fragment)
ImageView mImageView;
@Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
RedditDataRoomDatabase mRedditDataRoomDatabase;
private Activity mActivity;
private RequestManager mGlide;
@ -82,6 +88,7 @@ public class SubscribedSubredditsListingFragment extends Fragment {
}
String accountName = getArguments().getString(EXTRA_ACCOUNT_NAME);
String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN);
mGlide = Glide.with(this);
@ -92,7 +99,7 @@ public class SubscribedSubredditsListingFragment extends Fragment {
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, getArguments().getBoolean(EXTRA_EXTRA_CLEAR_SELECTION),
(name, iconUrl, subredditIsUser) -> ((SubredditSelectionActivity) mActivity).getSelectedSubreddit(name, iconUrl, subredditIsUser));
} else {
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity);
adapter = new SubscribedSubredditsRecyclerViewAdapter(mActivity, mOauthRetrofit, mRedditDataRoomDatabase, accessToken);
}
mRecyclerView.setAdapter(adapter);
@ -115,6 +122,16 @@ public class SubscribedSubredditsListingFragment extends Fragment {
adapter.setSubscribedSubreddits(subscribedSubredditData);
});
mSubscribedSubredditViewModel.getAllFavoriteSubscribedSubreddits().observe(this, favoriteSubscribedSubredditData -> {
if (favoriteSubscribedSubredditData != null && favoriteSubscribedSubredditData.size() > 0) {
mLinearLayout.setVisibility(View.GONE);
mRecyclerView.setVisibility(View.VISIBLE);
mGlide.clear(mImageView);
}
adapter.setFavoriteSubscribedSubreddits(favoriteSubscribedSubredditData);
});
return rootView;
}
}

View File

@ -85,4 +85,5 @@ public class JSONUtils {
public static final String NEW_KEY = "new";
public static final String NUM_COMMENTS_KEY = "num_comments";
public static final String HIDDEN_KEY = "hidden";
public static final String USER_HAS_FAVORITED_KEY = "user_has_favorited";
}

View File

@ -87,22 +87,17 @@ class ParseSubscribedThing {
}
}
String id = data.getString(JSONUtils.NAME_KEY);
if (iconUrl.equals("") || iconUrl.equals("null")) {
iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY);
if (iconUrl.equals("null")) {
iconUrl = "";
}
}
boolean isFavorite = data.getBoolean(JSONUtils.USER_HAS_FAVORITED_KEY);
if (data.getString(JSONUtils.SUBREDDIT_TYPE_KEY)
.equals(JSONUtils.SUBREDDIT_TYPE_VALUE_USER)) {
//It's a user
newSubscribedUserData.add(new SubscribedUserData(name.substring(2), iconUrl, accountName));
newSubscribedUserData.add(new SubscribedUserData(name.substring(2), iconUrl, accountName, isFavorite));
} else {
String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME);
String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim();
int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY);
newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl, accountName));
newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl, accountName, isFavorite));
newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers));
}
}

View File

@ -195,4 +195,8 @@ public interface RedditAPI {
@FormUrlEncoded
@POST("/api/unhide")
Call<String> unhide(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@FormUrlEncoded
@POST("/api/favorite")
Call<String> favoriteThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
}

View File

@ -5,6 +5,8 @@ import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
import ml.docilealligator.infinityforreddit.Account.Account;
import ml.docilealligator.infinityforreddit.Account.AccountDao;
@ -17,7 +19,7 @@ import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUse
import ml.docilealligator.infinityforreddit.User.UserDao;
import ml.docilealligator.infinityforreddit.User.UserData;
@Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, SubscribedUserData.class}, version = 1)
@Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, SubscribedUserData.class}, version = 2)
public abstract class RedditDataRoomDatabase extends RoomDatabase {
private static RedditDataRoomDatabase INSTANCE;
@ -27,6 +29,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
RedditDataRoomDatabase.class, "reddit_data")
.addMigrations(MIGRATION_1_2)
.build();
}
}
@ -43,4 +46,14 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase {
public abstract UserDao userDao();
public abstract SubscribedUserDao subscribedUserDao();
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE subscribed_subreddits"
+ " ADD COLUMN is_favorite INTEGER DEFAULT 0 NOT NULL");
database.execSQL("ALTER TABLE subscribed_users"
+ " ADD COLUMN is_favorite INTEGER DEFAULT 0 NOT NULL");
}
};
}

View File

@ -79,6 +79,8 @@ public class RedditUtils {
public static final String LINK_KEY = "link";
public static final String FLAIR_TEMPLATE_ID_KEY = "flair_template_id";
public static final String MAKE_FAVORITE_KEY = "make_favorite";
public static Map<String, String> getHttpBasicAuthHeader() {
Map<String, String> params = new HashMap<>();
String credentials = String.format("%s:%s", RedditUtils.CLIENT_ID, "");

View File

@ -100,7 +100,7 @@ public class SubredditSubscription {
String accountName, boolean isSubscribing) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(),
subredditData.getIconUrl(), accountName);
subredditData.getIconUrl(), accountName, false);
this.accountName = accountName;
this.isSubscribing = isSubscribing;
}

View File

@ -19,6 +19,9 @@ public interface SubscribedSubredditDao {
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE username = :accountName AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits(String accountName);
@Query("SELECT * from subscribed_subreddits WHERE name = :subredditName AND username = :accountName COLLATE NOCASE LIMIT 1")
SubscribedSubredditData getSubscribedSubreddit(String subredditName, String accountName);

View File

@ -22,12 +22,16 @@ public class SubscribedSubredditData {
private String iconUrl;
@ColumnInfo(name = "username")
private String username;
@ColumnInfo(name = "is_favorite")
private boolean favorite;
public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, String username) {
public SubscribedSubredditData(@NonNull String id, String name, String iconUrl, String username,
boolean favorite) {
this.id = id;
this.name = name;
this.iconUrl = iconUrl;
this.username = username;
this.favorite = favorite;
}
@NonNull
@ -50,4 +54,12 @@ public class SubscribedSubredditData {
public void setUsername(String username) {
this.username = username;
}
public boolean isFavorite() {
return favorite;
}
public void setFavorite(boolean favorite) {
this.favorite = favorite;
}
}

View File

@ -11,16 +11,22 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SubscribedSubredditRepository {
private SubscribedSubredditDao mSubscribedSubredditDao;
private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits;
SubscribedSubredditRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao();
mAllSubscribedSubreddits = mSubscribedSubredditDao.getAllSubscribedSubreddits(accountName);
mAllFavoriteSubscribedSubreddits = mSubscribedSubredditDao.getAllFavoriteSubscribedSubreddits(accountName);
}
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() {
return mAllSubscribedSubreddits;
}
public LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits() {
return mAllFavoriteSubscribedSubreddits;
}
public void insert(SubscribedSubredditData subscribedSubredditData) {
new insertAsyncTask(mSubscribedSubredditDao).execute(subscribedSubredditData);
}

View File

@ -15,17 +15,23 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SubscribedSubredditViewModel extends AndroidViewModel {
private SubscribedSubredditRepository mSubscribedSubredditRepository;
private LiveData<List<SubscribedSubredditData>> mAllSubscribedSubreddits;
private LiveData<List<SubscribedSubredditData>> mAllFavoriteSubscribedSubreddits;
public SubscribedSubredditViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) {
super(application);
mSubscribedSubredditRepository = new SubscribedSubredditRepository(redditDataRoomDatabase, accountName);
mAllSubscribedSubreddits = mSubscribedSubredditRepository.getAllSubscribedSubreddits();
mAllFavoriteSubscribedSubreddits = mSubscribedSubredditRepository.getAllFavoriteSubscribedSubreddits();
}
public LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits() {
return mAllSubscribedSubreddits;
}
public LiveData<List<SubscribedSubredditData>> getAllFavoriteSubscribedSubreddits() {
return mAllFavoriteSubscribedSubreddits;
}
public void insert(SubscribedSubredditData subscribedSubredditData) {
mSubscribedSubredditRepository.insert(subscribedSubredditData);
}

View File

@ -19,11 +19,14 @@ public class SubscribedUserData {
private String iconUrl;
@ColumnInfo(name = "username")
private String username;
@ColumnInfo(name = "is_favorite")
private boolean favorite;
public SubscribedUserData(@NonNull String name, String iconUrl, String username) {
public SubscribedUserData(@NonNull String name, String iconUrl, String username, boolean favorite) {
this.name = name;
this.iconUrl = iconUrl;
this.username = username;
this.favorite = favorite;
}
@NonNull
@ -42,4 +45,12 @@ public class SubscribedUserData {
public void setUsername(String username) {
this.username = username;
}
public boolean isFavorite() {
return favorite;
}
public void setFavorite(boolean favorite) {
this.favorite = favorite;
}
}

View File

@ -99,7 +99,7 @@ public class UserFollowing {
String accountName, boolean isSubscribing) {
this.subscribedUserDao = subscribedUserDao;
this.subscribedUserData = new SubscribedUserData(userData.getName(), userData.getIconUrl(),
accountName);
accountName, false);
this.accountName = accountName;
this.isSubscribing = isSubscribing;
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFEE0264"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFEE0264"
android:pathData="M16.5,3c-1.74,0 -3.41,0.81 -4.5,2.09C10.91,3.81 9.24,3 7.5,3 4.42,3 2,5.42 2,8.5c0,3.78 3.4,6.86 8.55,11.54L12,21.35l1.45,-1.32C18.6,15.36 22,12.28 22,8.5 22,5.42 19.58,3 16.5,3zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z"/>
</vector>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/divider_text_view_item_favorite_subreddits_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@ -17,10 +17,18 @@
<TextView
android:id="@+id/subreddit_name_text_view_item_subscribed_subreddit"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/primaryTextColor"
android:textSize="?attr/font_default"
android:layout_gravity="center_vertical" />
<ImageView
android:id="@+id/favorite_image_view_item_subscribed_subreddit"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="end|center_vertical"
android:layout_marginStart="32dp" />
</LinearLayout>

View File

@ -332,4 +332,7 @@
<string name="comment_saved_failed">Unable to save comment</string>
<string name="comment_unsaved_success">Comment unsaved</string>
<string name="comment_unsaved_failed">Unable to unsave comment</string>
<string name="favorites">Favorites</string>
<string name="all">All</string>
</resources>