diff options
11 files changed, 395 insertions, 69 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java index 7892ba32..71c1684c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/SubscribedThingListingActivity.java @@ -252,6 +252,7 @@ public class SubscribedThingListingActivity extends BaseActivity { FollowedUsersListingFragment fragment = new FollowedUsersListingFragment(); Bundle bundle = new Bundle(); bundle.putString(FollowedUsersListingFragment.EXTRA_ACCOUNT_NAME, mAccountName); + bundle.putString(FollowedUsersListingFragment.EXTRA_ACCESS_TOKEN, mAccessToken); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/FollowedUsersRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/FollowedUsersRecyclerViewAdapter.java index 48273d74..43adb6cb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/FollowedUsersRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/FollowedUsersRecyclerViewAdapter.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,52 +17,243 @@ 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.ViewUserDetailActivity; +import ml.docilealligator.infinityforreddit.FavoriteThing; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; import pl.droidsonroids.gif.GifImageView; +import retrofit2.Retrofit; public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { + private static final int VIEW_TYPE_FAVORITE_USER_DIVIDER = 0; + private static final int VIEW_TYPE_FAVORITE_USER = 1; + private static final int VIEW_TYPE_USER_DIVIDER = 2; + private static final int VIEW_TYPE_USER = 3; + private List<SubscribedUserData> mSubscribedUserData; + private List<SubscribedUserData> mFavoriteSubscribedUserData; private Context mContext; + private Retrofit mOauthRetrofit; + private RedditDataRoomDatabase mRedditDataRoomDatabase; + private String mAccessToken; private RequestManager glide; - public FollowedUsersRecyclerViewAdapter(Context context) { + public FollowedUsersRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, + RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken) { mContext = context; + mOauthRetrofit = oauthRetrofit; + mRedditDataRoomDatabase = redditDataRoomDatabase; + mAccessToken = accessToken; glide = Glide.with(context.getApplicationContext()); } + @Override + public int getItemViewType(int position) { + if (mFavoriteSubscribedUserData != null && mFavoriteSubscribedUserData.size() > 0) { + if (position == 0) { + return VIEW_TYPE_FAVORITE_USER_DIVIDER; + } else if (position == mFavoriteSubscribedUserData.size() + 1) { + return VIEW_TYPE_USER_DIVIDER; + } else if (position <= mFavoriteSubscribedUserData.size()) { + return VIEW_TYPE_FAVORITE_USER; + } else { + return VIEW_TYPE_USER; + } + } else { + return VIEW_TYPE_USER; + } + } + @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { - return new UserViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_subscribed_thing, viewGroup, false)); + switch (i) { + case VIEW_TYPE_FAVORITE_USER_DIVIDER: + return new FavoriteUsersDividerViewHolder(LayoutInflater.from(viewGroup.getContext()) + .inflate(R.layout.item_favorite_thing_divider, viewGroup, false)); + case VIEW_TYPE_FAVORITE_USER: + return new FavoriteUserViewHolder(LayoutInflater.from(viewGroup.getContext()) + .inflate(R.layout.item_subscribed_thing, viewGroup, false)); + case VIEW_TYPE_USER_DIVIDER: + return new AllUsersDividerViewHolder(LayoutInflater.from(viewGroup.getContext()) + .inflate(R.layout.item_favorite_thing_divider, viewGroup, false)); + default: + return new UserViewHolder(LayoutInflater.from(viewGroup.getContext()) + .inflate(R.layout.item_subscribed_thing, viewGroup, false)); + } } @Override public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, int i) { - viewHolder.itemView.setOnClickListener(view -> { - Intent intent = new Intent(mContext, ViewUserDetailActivity.class); - intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mSubscribedUserData.get(viewHolder.getAdapterPosition()).getName()); - mContext.startActivity(intent); - }); - if (!mSubscribedUserData.get(i).getIconUrl().equals("")) { - glide.load(mSubscribedUserData.get(i).getIconUrl()) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .error(glide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) - .into(((UserViewHolder) viewHolder).iconGifImageView); - } else { - glide.load(R.drawable.subreddit_default_icon) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) - .into(((UserViewHolder) viewHolder).iconGifImageView); + if (viewHolder instanceof UserViewHolder) { + int offset = (mFavoriteSubscribedUserData != null && mFavoriteSubscribedUserData.size() > 0) ? + mFavoriteSubscribedUserData.size() + 2 : 0; + + viewHolder.itemView.setOnClickListener(view -> { + Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getName()); + mContext.startActivity(intent); + }); + if (!mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getIconUrl().equals("")) { + glide.load(mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((UserViewHolder) viewHolder).iconGifImageView); + } else { + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((UserViewHolder) viewHolder).iconGifImageView); + } + ((UserViewHolder) viewHolder).userNameTextView.setText(mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).getName()); + + if(mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } else { + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + + ((UserViewHolder) viewHolder).favoriteImageView.setOnClickListener(view -> { + if(mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).setFavorite(false); + FavoriteThing.unfavoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = viewHolder.getAdapterPosition() - offset; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(false); + } + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + + @Override + public void failed() { + int position = viewHolder.getAdapterPosition() - offset; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(true); + } + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + }); + } else { + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset).setFavorite(true); + FavoriteThing.favoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mSubscribedUserData.get(viewHolder.getAdapterPosition() - offset), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = viewHolder.getAdapterPosition() - offset; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(true); + } + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + + @Override + public void failed() { + int position = viewHolder.getAdapterPosition() - offset; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(false); + } + ((UserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + }); + } + }); + } else if (viewHolder instanceof FavoriteUserViewHolder) { + viewHolder.itemView.setOnClickListener(view -> { + Intent intent = new Intent(mContext, ViewUserDetailActivity.class); + intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getName()); + mContext.startActivity(intent); + }); + if (!mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getIconUrl().equals("")) { + glide.load(mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getIconUrl()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .error(glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) + .into(((FavoriteUserViewHolder) viewHolder).iconGifImageView); + } else { + glide.load(R.drawable.subreddit_default_icon) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) + .into(((FavoriteUserViewHolder) viewHolder).iconGifImageView); + } + ((FavoriteUserViewHolder) viewHolder).userNameTextView.setText(mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).getName()); + + if(mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).isFavorite()) { + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } else { + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setOnClickListener(view -> { + if(mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).isFavorite()) { + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).setFavorite(false); + FavoriteThing.unfavoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = viewHolder.getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(false); + } + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + + @Override + public void failed() { + int position = viewHolder.getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(true); + } + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + }); + } else { + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1).setFavorite(true); + FavoriteThing.favoriteUser(mOauthRetrofit, mRedditDataRoomDatabase, mAccessToken, + mFavoriteSubscribedUserData.get(viewHolder.getAdapterPosition() - 1), + new FavoriteThing.FavoriteThingListener() { + @Override + public void success() { + int position = viewHolder.getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(true); + } + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); + } + + @Override + public void failed() { + int position = viewHolder.getAdapterPosition() - 1; + if(position >= 0 && mFavoriteSubscribedUserData.size() > position) { + mFavoriteSubscribedUserData.get(position).setFavorite(false); + } + ((FavoriteUserViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); + } + }); + } + }); } - ((UserViewHolder) viewHolder).subredditNameTextView.setText(mSubscribedUserData.get(i).getName()); } @Override public int getItemCount() { - if (mSubscribedUserData != null) { + if (mSubscribedUserData != null && mSubscribedUserData.size() > 0) { + if(mFavoriteSubscribedUserData != null && mFavoriteSubscribedUserData.size() > 0) { + return mSubscribedUserData.size() + mFavoriteSubscribedUserData.size() + 2; + } return mSubscribedUserData.size(); } return 0; @@ -69,7 +261,11 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter<Recyc @Override public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { - glide.clear(((UserViewHolder) holder).iconGifImageView); + if(holder instanceof UserViewHolder) { + glide.clear(((UserViewHolder) holder).iconGifImageView); + } else if (holder instanceof FavoriteUserViewHolder) { + glide.clear(((FavoriteUserViewHolder) holder).iconGifImageView); + } } public void setSubscribedUsers(List<SubscribedUserData> subscribedUsers) { @@ -77,15 +273,56 @@ public class FollowedUsersRecyclerViewAdapter extends RecyclerView.Adapter<Recyc notifyDataSetChanged(); } + public void setFavoriteSubscribedUsers(List<SubscribedUserData> favoriteSubscribedUsers) { + mFavoriteSubscribedUserData = favoriteSubscribedUsers; + notifyDataSetChanged(); + } + + class FavoriteUserViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.thing_icon_gif_image_view_item_subscribed_thing) + GifImageView iconGifImageView; + @BindView(R.id.thing_name_text_view_item_subscribed_thing) + TextView userNameTextView; + @BindView(R.id.favorite_image_view_item_subscribed_thing) + ImageView favoriteImageView; + + FavoriteUserViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + } + } + + class UserViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.thing_icon_gif_image_view_item_subscribed_thing) + GifImageView iconGifImageView; + @BindView(R.id.thing_name_text_view_item_subscribed_thing) + TextView userNameTextView; + @BindView(R.id.favorite_image_view_item_subscribed_thing) + ImageView favoriteImageView; + + UserViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + } + } + + class FavoriteUsersDividerViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.divider_text_view_item_favorite_thing_divider) TextView dividerTextView; + + FavoriteUsersDividerViewHolder(@NonNull View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + dividerTextView.setText(R.string.favorites); + } + } - private class UserViewHolder extends RecyclerView.ViewHolder { - private final GifImageView iconGifImageView; - private final TextView subredditNameTextView; + class AllUsersDividerViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.divider_text_view_item_favorite_thing_divider) TextView dividerTextView; - public UserViewHolder(View itemView) { + AllUsersDividerViewHolder(@NonNull 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); + dividerTextView.setText(R.string.all); } } } 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 1d1b7e22..1ccad0e9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/SubscribedSubredditsRecyclerViewAdapter.java @@ -92,13 +92,13 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte switch (i) { case VIEW_TYPE_FAVORITE_SUBREDDIT_DIVIDER: return new FavoriteSubredditsDividerViewHolder(LayoutInflater.from(viewGroup.getContext()) - .inflate(R.layout.item_favorite_subreddits_divider, viewGroup, false)); + .inflate(R.layout.item_favorite_thing_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)); + .inflate(R.layout.item_favorite_thing_divider, viewGroup, false)); default: return new SubredditViewHolder(LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.item_subscribed_thing, viewGroup, false)); @@ -153,8 +153,8 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte 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, + FavoriteThing.unfavoriteSubreddit(mOauthRetrofit, mRedditDataRoomDatabase, accessToken, + mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), new FavoriteThing.FavoriteThingListener() { @Override public void success() { @@ -177,8 +177,8 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } 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, + FavoriteThing.favoriteSubreddit(mOauthRetrofit, mRedditDataRoomDatabase, accessToken, + mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), new FavoriteThing.FavoriteThingListener() { @Override public void success() { @@ -232,8 +232,8 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte 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, + FavoriteThing.unfavoriteSubreddit(mOauthRetrofit, mRedditDataRoomDatabase, accessToken, + mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1), new FavoriteThing.FavoriteThingListener() { @Override public void success() { @@ -256,8 +256,8 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } 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, + FavoriteThing.favoriteSubreddit(mOauthRetrofit, mRedditDataRoomDatabase, accessToken, + mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - 1), new FavoriteThing.FavoriteThingListener() { @Override public void success() { @@ -350,11 +350,11 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } class SubredditViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit) + @BindView(R.id.thing_icon_gif_image_view_item_subscribed_thing) GifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_item_subscribed_subreddit) + @BindView(R.id.thing_name_text_view_item_subscribed_thing) TextView subredditNameTextView; - @BindView(R.id.favorite_image_view_item_subscribed_subreddit) + @BindView(R.id.favorite_image_view_item_subscribed_thing) ImageView favoriteImageView; SubredditViewHolder(View itemView) { @@ -364,11 +364,11 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } class FavoriteSubredditViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.subreddit_icon_gif_image_view_item_subscribed_subreddit) + @BindView(R.id.thing_icon_gif_image_view_item_subscribed_thing) GifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_item_subscribed_subreddit) + @BindView(R.id.thing_name_text_view_item_subscribed_thing) TextView subredditNameTextView; - @BindView(R.id.favorite_image_view_item_subscribed_subreddit) + @BindView(R.id.favorite_image_view_item_subscribed_thing) ImageView favoriteImageView; FavoriteSubredditViewHolder(View itemView) { @@ -378,7 +378,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } class FavoriteSubredditsDividerViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.divider_text_view_item_favorite_subreddits_divider) TextView dividerTextView; + @BindView(R.id.divider_text_view_item_favorite_thing_divider) TextView dividerTextView; FavoriteSubredditsDividerViewHolder(@NonNull View itemView) { super(itemView); @@ -388,7 +388,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } class AllSubredditsDividerViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.divider_text_view_item_favorite_subreddits_divider) TextView dividerTextView; + @BindView(R.id.divider_text_view_item_favorite_thing_divider) TextView dividerTextView; AllSubredditsDividerViewHolder(@NonNull View itemView) { super(itemView); 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 dcc36a0b..00c6c3e7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AsyncTask/InsertSubscribedThingsAsyncTask.java @@ -19,13 +19,15 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> private RedditDataRoomDatabase mRedditDataRoomDatabase; private String mAccountName; private SubscribedSubredditDao mSubscribedSubredditDao; - private SubscribedUserDao mUserDao; + private SubscribedUserDao mSubscribedUserDao; private SubredditDao mSubredditDao; private SubscribedSubredditData mSingleSubscribedSubredditData; + private SubscribedUserData mSingleSubscribedUserData; private List<SubscribedSubredditData> subscribedSubredditData; private List<SubscribedUserData> subscribedUserData; private List<SubredditData> subredditData; private InsertSubscribedThingListener mInsertSubscribedThingListener; + public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, @Nullable String accountName, List<SubscribedSubredditData> subscribedSubredditData, List<SubscribedUserData> subscribedUserData, @@ -34,7 +36,7 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> mRedditDataRoomDatabase = redditDataRoomDatabase; mAccountName = accountName; mSubscribedSubredditDao = redditDataRoomDatabase.subscribedSubredditDao(); - mUserDao = redditDataRoomDatabase.subscribedUserDao(); + mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao(); mSubredditDao = redditDataRoomDatabase.subredditDao(); this.subscribedSubredditData = subscribedSubredditData; @@ -53,14 +55,26 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> mInsertSubscribedThingListener = insertSubscribedThingListener; } + public InsertSubscribedThingsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, + SubscribedUserData subscribedUserData, + InsertSubscribedThingListener insertSubscribedThingListener) { + mRedditDataRoomDatabase = redditDataRoomDatabase; + mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao(); + mAccountName = subscribedUserData.getUsername(); + mSingleSubscribedUserData = subscribedUserData; + mInsertSubscribedThingListener = insertSubscribedThingListener; + } + @Override protected Void doInBackground(final Void... params) { if (mAccountName != null && mRedditDataRoomDatabase.accountDao().getAccountData(mAccountName) == null) { return null; } - if(mSingleSubscribedSubredditData != null) { + if (mSingleSubscribedSubredditData != null) { mSubscribedSubredditDao.insert(mSingleSubscribedSubredditData); + } else if (mSingleSubscribedUserData != null) { + mSubscribedUserDao.insert(mSingleSubscribedUserData); } else { if (subscribedSubredditData != null) { for (SubscribedSubredditData s : subscribedSubredditData) { @@ -70,7 +84,7 @@ public class InsertSubscribedThingsAsyncTask extends AsyncTask<Void, Void, Void> if (subscribedUserData != null) { for (SubscribedUserData s : subscribedUserData) { - mUserDao.insert(s); + mSubscribedUserDao.insert(s); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java index 6fdd28e2..e2756098 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java @@ -7,21 +7,18 @@ import java.util.Map; import ml.docilealligator.infinityforreddit.AsyncTask.InsertSubscribedThingsAsyncTask; import ml.docilealligator.infinityforreddit.SubscribedSubredditDatabase.SubscribedSubredditData; +import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserData; 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) { + public static void favoriteSubreddit(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, SubscribedSubredditData subscribedSubredditData, + 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.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 @@ -41,15 +38,11 @@ public class FavoriteThing { }); } - public static void unfavoriteThing(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, - String accessToken, SubscribedSubredditData subscribedSubredditData, - boolean isUser, FavoriteThingListener favoriteThingListener) { + public static void unfavoriteSubreddit(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, SubscribedSubredditData subscribedSubredditData, + 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.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 @@ -69,6 +62,54 @@ public class FavoriteThing { }); } + public static void favoriteUser(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, SubscribedUserData subscribedUserData, + FavoriteThingListener favoriteThingListener) { + Map<String, String> params = new HashMap<>(); + params.put(RedditUtils.SR_NAME_KEY, "u_" + subscribedUserData.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, subscribedUserData, + favoriteThingListener::success).execute(); + } else { + favoriteThingListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + favoriteThingListener.failed(); + } + }); + } + + public static void unfavoriteUser(Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, + String accessToken, SubscribedUserData subscribedUserData, + FavoriteThingListener favoriteThingListener) { + Map<String, String> params = new HashMap<>(); + params.put(RedditUtils.SR_NAME_KEY, "u_" + subscribedUserData.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, subscribedUserData, + favoriteThingListener::success).execute(); + } else { + favoriteThingListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + favoriteThingListener.failed(); + } + }); + } + public interface FavoriteThingListener { void success(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java index 662770a2..539aed91 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/FollowedUsersListingFragment.java @@ -21,6 +21,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; @@ -29,6 +30,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUserViewModel; +import retrofit2.Retrofit; /** @@ -37,6 +39,7 @@ import ml.docilealligator.infinityforreddit.SubscribedUserDatabase.SubscribedUse public class FollowedUsersListingFragment extends Fragment { public static final String EXTRA_ACCOUNT_NAME = "EAN"; + public static final String EXTRA_ACCESS_TOKEN = "EAT"; @BindView(R.id.recycler_view_followed_users_listing_fragment) RecyclerView mRecyclerView; @@ -45,6 +48,9 @@ public class FollowedUsersListingFragment extends Fragment { @BindView(R.id.no_subscriptions_image_view_followed_users_listing_fragment) ImageView mImageView; @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; private Activity mActivity; private RequestManager mGlide; @@ -79,12 +85,14 @@ public class FollowedUsersListingFragment extends Fragment { mGlide = Glide.with(this); mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity)); - FollowedUsersRecyclerViewAdapter adapter = new FollowedUsersRecyclerViewAdapter(mActivity); + FollowedUsersRecyclerViewAdapter adapter = new FollowedUsersRecyclerViewAdapter(mActivity, + mOauthRetrofit, mRedditDataRoomDatabase, getArguments().getString(EXTRA_ACCESS_TOKEN)); mRecyclerView.setAdapter(adapter); mSubscribedUserViewModel = new ViewModelProvider(this, new SubscribedUserViewModel.Factory(mActivity.getApplication(), mRedditDataRoomDatabase, getArguments().getString(EXTRA_ACCOUNT_NAME))) .get(SubscribedUserViewModel.class); + mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, subscribedUserData -> { if (subscribedUserData == null || subscribedUserData.size() == 0) { mRecyclerView.setVisibility(View.GONE); @@ -93,10 +101,20 @@ public class FollowedUsersListingFragment extends Fragment { } else { mLinearLayout.setVisibility(View.GONE); mRecyclerView.setVisibility(View.VISIBLE); + mGlide.clear(mImageView); } adapter.setSubscribedUsers(subscribedUserData); }); + mSubscribedUserViewModel.getAllFavoriteSubscribedUsers().observe(this, favoriteSubscribedUserData -> { + if (favoriteSubscribedUserData != null && favoriteSubscribedUserData.size() > 0) { + mLinearLayout.setVisibility(View.GONE); + mRecyclerView.setVisibility(View.VISIBLE); + mGlide.clear(mImageView); + } + adapter.setFavoriteSubscribedUsers(favoriteSubscribedUserData); + }); + return rootView; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java index 4143e7c7..33ecac48 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserDao.java @@ -19,6 +19,9 @@ public interface SubscribedUserDao { @Query("SELECT * FROM subscribed_users WHERE username = :accountName ORDER BY name COLLATE NOCASE ASC") LiveData<List<SubscribedUserData>> getAllSubscribedUsers(String accountName); + @Query("SELECT * FROM subscribed_users WHERE username = :accountName AND is_favorite = 1 ORDER BY name COLLATE NOCASE ASC") + LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsers(String accountName); + @Query("SELECT * FROM subscribed_users WHERE name = :name AND username = :accountName COLLATE NOCASE LIMIT 1") SubscribedUserData getSubscribedUser(String name, String accountName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java index 7d295037..f5a1c1ab 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserRepository.java @@ -11,16 +11,22 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class SubscribedUserRepository { private SubscribedUserDao mSubscribedUserDao; private LiveData<List<SubscribedUserData>> mAllSubscribedUsers; + private LiveData<List<SubscribedUserData>> mAllFavoriteSubscribedUsers; SubscribedUserRepository(RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { mSubscribedUserDao = redditDataRoomDatabase.subscribedUserDao(); mAllSubscribedUsers = mSubscribedUserDao.getAllSubscribedUsers(accountName); + mAllFavoriteSubscribedUsers = mSubscribedUserDao.getAllFavoriteSubscribedUsers(accountName); } LiveData<List<SubscribedUserData>> getAllSubscribedSubreddits() { return mAllSubscribedUsers; } + LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedSubreddits() { + return mAllFavoriteSubscribedUsers; + } + public void insert(SubscribedUserData subscribedUserData) { new SubscribedUserRepository.insertAsyncTask(mSubscribedUserDao).execute(subscribedUserData); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java index b659fbc5..9f1b033f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedUserDatabase/SubscribedUserViewModel.java @@ -15,17 +15,23 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class SubscribedUserViewModel extends AndroidViewModel { private SubscribedUserRepository mSubscribedUserRepository; private LiveData<List<SubscribedUserData>> mAllSubscribedUsers; + private LiveData<List<SubscribedUserData>> mAllFavoriteSubscribedUsers; public SubscribedUserViewModel(Application application, RedditDataRoomDatabase redditDataRoomDatabase, String accountName) { super(application); mSubscribedUserRepository = new SubscribedUserRepository(redditDataRoomDatabase, accountName); mAllSubscribedUsers = mSubscribedUserRepository.getAllSubscribedSubreddits(); + mAllFavoriteSubscribedUsers = mSubscribedUserRepository.getAllFavoriteSubscribedSubreddits(); } public LiveData<List<SubscribedUserData>> getAllSubscribedUsers() { return mAllSubscribedUsers; } + public LiveData<List<SubscribedUserData>> getAllFavoriteSubscribedUsers() { + return mAllFavoriteSubscribedUsers; + } + public void insert(SubscribedUserData subscribedUserData) { mSubscribedUserRepository.insert(subscribedUserData); } diff --git a/app/src/main/res/layout/item_favorite_subreddits_divider.xml b/app/src/main/res/layout/item_favorite_thing_divider.xml index 7b3f34a8..80add13d 100644 --- a/app/src/main/res/layout/item_favorite_subreddits_divider.xml +++ b/app/src/main/res/layout/item_favorite_thing_divider.xml @@ -5,7 +5,7 @@ android:padding="16dp"> <TextView - android:id="@+id/divider_text_view_item_favorite_subreddits_divider" + android:id="@+id/divider_text_view_item_favorite_thing_divider" android:layout_width="wrap_content" android:layout_height="wrap_content" /> diff --git a/app/src/main/res/layout/item_subscribed_thing.xml b/app/src/main/res/layout/item_subscribed_thing.xml index bb76a5fb..79622979 100644 --- a/app/src/main/res/layout/item_subscribed_thing.xml +++ b/app/src/main/res/layout/item_subscribed_thing.xml @@ -9,14 +9,14 @@ android:background="?attr/selectableItemBackground"> <pl.droidsonroids.gif.GifImageView - android:id="@+id/subreddit_icon_gif_image_view_item_subscribed_subreddit" + android:id="@+id/thing_icon_gif_image_view_item_subscribed_thing" android:layout_width="24dp" android:layout_height="24dp" android:layout_gravity="center_vertical" android:layout_marginEnd="32dp" /> <TextView - android:id="@+id/subreddit_name_text_view_item_subscribed_subreddit" + android:id="@+id/thing_name_text_view_item_subscribed_thing" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" @@ -25,7 +25,7 @@ android:layout_gravity="center_vertical" /> <ImageView - android:id="@+id/favorite_image_view_item_subscribed_subreddit" + android:id="@+id/favorite_image_view_item_subscribed_thing" android:layout_width="24dp" android:layout_height="24dp" android:layout_gravity="end|center_vertical" |