aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-10-09 08:40:45 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-10-09 08:40:45 +0000
commit77a31056b2213999acc94cd5dad1a24c5b006f90 (patch)
tree18c8dc78e53ec4cf0c343c27318c1b3a60906387
parentc4fd313839466df975a2afc6ad03e8f06dedf2e8 (diff)
downloadinfinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.tar
infinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.tar.gz
infinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.tar.bz2
infinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.tar.lz
infinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.tar.xz
infinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.tar.zst
infinity-for-reddit-77a31056b2213999acc94cd5dad1a24c5b006f90.zip
Favorite and unfavorite subreddits are now available.
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java335
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java43
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java77
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java21
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java1
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java15
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java14
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java6
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java6
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java13
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java2
-rw-r--r--app/src/main/res/drawable/ic_favorite_24dp.xml9
-rw-r--r--app/src/main/res/drawable/ic_favorite_border_24dp.xml9
-rw-r--r--app/src/main/res/layout/item_favorite_subreddits_divider.xml12
-rw-r--r--app/src/main/res/layout/item_subscribed_thing.xml10
-rw-r--r--app/src/main/res/values/strings.xml3
21 files changed, 523 insertions, 75 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java
index 9be1284c..1d1b7e22 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java
@@ -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));
+ 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 {
- name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getName();
- iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1).getIconUrl();
- viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, false));
+ ((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);
+ });
}
- } else {
- name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName();
- iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getIconUrl();
+ 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);
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java
index ab52b035..dcc36a0b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java
@@ -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 {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java
new file mode 100644
index 00000000..6fdd28e2
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java
@@ -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();
+ }
+}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java
index 4f60b6a8..ceb4aeed 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubscribedSubredditsListingFragment.java
@@ -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;
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
index 1e73bcb2..62322490 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java
@@ -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";
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java
index 990542d8..116d2ba2 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java
@@ -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));
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
index 05311cf1..2a326228 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
@@ -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);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java
index 8681fb35..bc73c266 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java
@@ -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");
+ }
+ };
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java
index 78d0d9b0..cc282ca3 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java
@@ -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, "");
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java
index 6508020a..15d9ee3b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSubscription.java
@@ -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;
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java
index 6caf7703..6bfa103e 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditDao.java
@@ -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);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java
index 50cc103e..8cc75e5a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditData.java
@@ -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;
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java
index f10f93b8..3f2da07d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditRepository.java
@@ -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);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java
index c1965641..56ff6c5e 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditDatabase/SubscribedSubredditViewModel.java
@@ -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);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java
index 50b05f3d..701e7e5d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserData.java
@@ -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;
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java
index bbe8849a..b48bfde9 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java
@@ -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;
}
diff --git a/app/src/main/res/drawable/ic_favorite_24dp.xml b/app/src/main/res/drawable/ic_favorite_24dp.xml
new file mode 100644
index 00000000..64afd4b3
--- /dev/null
+++ b/app/src/main/res/drawable/ic_favorite_24dp.xml
@@ -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>
diff --git a/app/src/main/res/drawable/ic_favorite_border_24dp.xml b/app/src/main/res/drawable/ic_favorite_border_24dp.xml
new file mode 100644
index 00000000..48a506b1
--- /dev/null
+++ b/app/src/main/res/drawable/ic_favorite_border_24dp.xml
@@ -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>
diff --git a/app/src/main/res/layout/item_favorite_subreddits_divider.xml b/app/src/main/res/layout/item_favorite_subreddits_divider.xml
new file mode 100644
index 00000000..7b3f34a8
--- /dev/null
+++ b/app/src/main/res/layout/item_favorite_subreddits_divider.xml
@@ -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> \ No newline at end of file
diff --git a/app/src/main/res/layout/item_subscribed_thing.xml b/app/src/main/res/layout/item_subscribed_thing.xml
index 92e381aa..bb76a5fb 100644
--- a/app/src/main/res/layout/item_subscribed_thing.xml
+++ b/app/src/main/res/layout/item_subscribed_thing.xml
@@ -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> \ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 24407978..bcd99f75 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -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>