Display subreddit icons in best posts, eventually. Rename some methods.

This commit is contained in:
Alex Ning 2018-08-10 08:22:56 +08:00
parent b0e2e273a7
commit e44a172db0
10 changed files with 164 additions and 93 deletions

View File

@ -1,7 +1,7 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Fragment; import android.support.v4.app.Fragment;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
@ -158,7 +158,6 @@ public class BestPostFragment extends Fragment {
}; };
mPaginationSynchronizer.setPaginationRequestQueueSynchronizer(paginationRequestQueueSynchronizer); mPaginationSynchronizer.setPaginationRequestQueueSynchronizer(paginationRequestQueueSynchronizer);
return rootView; return rootView;
} }

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent; import android.support.customtabs.CustomTabsIntent;
@ -39,12 +40,13 @@ import de.hdodenhof.circleimageview.CircleImageView;
*/ */
class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<BestPostData> bestPostData; private ArrayList<BestPostData> mBestPostData;
private Context mContext; private Context mContext;
private PaginationSynchronizer mPaginationSynchronizer; private PaginationSynchronizer mPaginationSynchronizer;
private RequestQueue mVoteThingRequestQueue; private RequestQueue mVoteThingRequestQueue;
private RequestQueue mAcquireAccessTokenRequestQueue; private RequestQueue mAcquireAccessTokenRequestQueue;
private RequestManager glide; private RequestManager glide;
private SubredditDao subredditDao;
private boolean isLoadingMorePostSuccess; private boolean isLoadingMorePostSuccess;
private boolean canStartActivity; private boolean canStartActivity;
@ -56,13 +58,14 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) { RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) {
if(context != null) { if(context != null) {
mContext = context; mContext = context;
this.bestPostData = bestPostData; mBestPostData = bestPostData;
mPaginationSynchronizer = paginationSynchronizer; mPaginationSynchronizer = paginationSynchronizer;
mVoteThingRequestQueue = voteThingRequestQueue; mVoteThingRequestQueue = voteThingRequestQueue;
mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue; mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue;
isLoadingMorePostSuccess = true; isLoadingMorePostSuccess = true;
canStartActivity = true; canStartActivity = true;
glide = Glide.with(mContext); glide = Glide.with(mContext);
subredditDao = SubredditRoomDatabase.getDatabase(mContext).subredditDao();
} }
} }
@ -85,16 +88,19 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
@Override @Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) { public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
if(holder instanceof DataViewHolder) { if(holder instanceof DataViewHolder) {
if(bestPostData.get(position) == null) { if(mBestPostData.get(position) == null) {
Log.i("is null", Integer.toString(position)); Log.i("is null", Integer.toString(position));
} else { } else {
final String id = bestPostData.get(position).getFullName(); final String id = mBestPostData.get(position).getFullName();
final String subredditName = bestPostData.get(position).getSubredditName(); final String subredditName = mBestPostData.get(position).getSubredditName();
final String postTime = bestPostData.get(position).getPostTime(); final String postTime = mBestPostData.get(position).getPostTime();
final String title = bestPostData.get(position).getTitle(); final String title = mBestPostData.get(position).getTitle();
final String permalink = bestPostData.get(position).getPermalink(); final String permalink = mBestPostData.get(position).getPermalink();
int voteType = bestPostData.get(position).getVoteType(); int voteType = mBestPostData.get(position).getVoteType();
boolean nsfw = bestPostData.get(position).getNSFW(); boolean nsfw = mBestPostData.get(position).getNSFW();
new LoadSubredditIconAsyncTask(mContext, ((DataViewHolder) holder).subredditImageView,
subredditDao, subredditName).execute();
((DataViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() { ((DataViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -103,7 +109,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
canStartActivity = false; canStartActivity = false;
Intent intent = new Intent(mContext, ViewPostDetailActivity.class); Intent intent = new Intent(mContext, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_TITLE, title); intent.putExtra(ViewPostDetailActivity.EXTRA_TITLE, title);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, bestPostData.get(position)); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_DATA, mBestPostData.get(position));
mContext.startActivity(intent); mContext.startActivity(intent);
} }
} }
@ -112,7 +118,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
((DataViewHolder) holder).subredditNameTextView.setText(subredditName); ((DataViewHolder) holder).subredditNameTextView.setText(subredditName);
((DataViewHolder) holder).postTimeTextView.setText(postTime); ((DataViewHolder) holder).postTimeTextView.setText(postTime);
((DataViewHolder) holder).titleTextView.setText(title); ((DataViewHolder) holder).titleTextView.setText(title);
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore())); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore()));
if(nsfw) { if(nsfw) {
((DataViewHolder) holder).nsfwTextView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).nsfwTextView.setVisibility(View.VISIBLE);
@ -129,17 +135,17 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
break; break;
} }
if(bestPostData.get(position).getPostType() != BestPostData.TEXT_TYPE && bestPostData.get(position).getPostType() != BestPostData.NO_PREVIEW_LINK_TYPE) { if(mBestPostData.get(position).getPostType() != BestPostData.TEXT_TYPE && mBestPostData.get(position).getPostType() != BestPostData.NO_PREVIEW_LINK_TYPE) {
((DataViewHolder) holder).relativeLayout.setVisibility(View.VISIBLE); ((DataViewHolder) holder).relativeLayout.setVisibility(View.VISIBLE);
((DataViewHolder) holder).progressBar.setVisibility(View.VISIBLE); ((DataViewHolder) holder).progressBar.setVisibility(View.VISIBLE);
((DataViewHolder) holder).imageView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).imageView.setVisibility(View.VISIBLE);
} }
switch (bestPostData.get(position).getPostType()) { switch (mBestPostData.get(position).getPostType()) {
case BestPostData.IMAGE_TYPE: case BestPostData.IMAGE_TYPE:
((DataViewHolder) holder).typeTextView.setText("IMAGE"); ((DataViewHolder) holder).typeTextView.setText("IMAGE");
final String previewImageUrl = bestPostData.get(position).getPreviewUrl(); final String previewImageUrl = mBestPostData.get(position).getPreviewUrl();
final String imageUrl = bestPostData.get(position).getUrl(); final String imageUrl = mBestPostData.get(position).getUrl();
glide.load(previewImageUrl).listener(new RequestListener<Drawable>() { glide.load(previewImageUrl).listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -166,7 +172,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
break; break;
case BestPostData.LINK_TYPE: case BestPostData.LINK_TYPE:
((DataViewHolder) holder).typeTextView.setText("LINK"); ((DataViewHolder) holder).typeTextView.setText("LINK");
String linkPreviewUrl = bestPostData.get(position).getPreviewUrl(); String linkPreviewUrl = mBestPostData.get(position).getPreviewUrl();
glide.load(linkPreviewUrl).listener(new RequestListener<Drawable>() { glide.load(linkPreviewUrl).listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -179,7 +185,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
return false; return false;
} }
}).into(((DataViewHolder) holder).imageView); }).into(((DataViewHolder) holder).imageView);
final String linkUrl = bestPostData.get(position).getUrl(); final String linkUrl = mBestPostData.get(position).getUrl();
((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@ -194,7 +200,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
break; break;
case BestPostData.GIF_VIDEO_TYPE: case BestPostData.GIF_VIDEO_TYPE:
((DataViewHolder) holder).typeTextView.setText("GIF"); ((DataViewHolder) holder).typeTextView.setText("GIF");
String gifVideoPreviewUrl = bestPostData.get(position).getPreviewUrl(); String gifVideoPreviewUrl = mBestPostData.get(position).getPreviewUrl();
glide.load(gifVideoPreviewUrl).listener(new RequestListener<Drawable>() { glide.load(gifVideoPreviewUrl).listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -208,7 +214,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
} }
}).into(((DataViewHolder) holder).imageView); }).into(((DataViewHolder) holder).imageView);
String gifVideoUrl = bestPostData.get(position).getVideoUrl(); String gifVideoUrl = mBestPostData.get(position).getVideoUrl();
final Uri gifVideoUri = Uri.parse(gifVideoUrl); final Uri gifVideoUri = Uri.parse(gifVideoUrl);
((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
@ -217,10 +223,10 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
Intent intent = new Intent(mContext, ViewVideoActivity.class); Intent intent = new Intent(mContext, ViewVideoActivity.class);
intent.setData(gifVideoUri); intent.setData(gifVideoUri);
intent.putExtra(ViewVideoActivity.TITLE_KEY, title); intent.putExtra(ViewVideoActivity.TITLE_KEY, title);
intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, bestPostData.get(position).isDashVideo()); intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mBestPostData.get(position).isDashVideo());
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, bestPostData.get(position).isDownloadableGifOrVideo()); intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mBestPostData.get(position).isDownloadableGifOrVideo());
if(bestPostData.get(position).isDownloadableGifOrVideo()) { if(mBestPostData.get(position).isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, bestPostData.get(position).getGifOrVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mBestPostData.get(position).getGifOrVideoDownloadUrl());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName);
intent.putExtra(ViewVideoActivity.ID_KEY, id); intent.putExtra(ViewVideoActivity.ID_KEY, id);
} }
@ -230,7 +236,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
break; break;
case BestPostData.VIDEO_TYPE: case BestPostData.VIDEO_TYPE:
((DataViewHolder) holder).typeTextView.setText("VIDEO"); ((DataViewHolder) holder).typeTextView.setText("VIDEO");
String videoPreviewUrl = bestPostData.get(position).getPreviewUrl(); String videoPreviewUrl = mBestPostData.get(position).getPreviewUrl();
glide.load(videoPreviewUrl).listener(new RequestListener<Drawable>() { glide.load(videoPreviewUrl).listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
@ -244,7 +250,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
} }
}).into(((DataViewHolder) holder).imageView); }).into(((DataViewHolder) holder).imageView);
String videoUrl = bestPostData.get(position).getVideoUrl(); String videoUrl = mBestPostData.get(position).getVideoUrl();
final Uri videoUri = Uri.parse(videoUrl); final Uri videoUri = Uri.parse(videoUrl);
((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() { ((DataViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
@ -253,10 +259,10 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
Intent intent = new Intent(mContext, ViewVideoActivity.class); Intent intent = new Intent(mContext, ViewVideoActivity.class);
intent.setData(videoUri); intent.setData(videoUri);
intent.putExtra(ViewVideoActivity.TITLE_KEY, title); intent.putExtra(ViewVideoActivity.TITLE_KEY, title);
intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, bestPostData.get(position).isDashVideo()); intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mBestPostData.get(position).isDashVideo());
intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, bestPostData.get(position).isDownloadableGifOrVideo()); intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mBestPostData.get(position).isDownloadableGifOrVideo());
if(bestPostData.get(position).isDownloadableGifOrVideo()) { if(mBestPostData.get(position).isDownloadableGifOrVideo()) {
intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, bestPostData.get(position).getGifOrVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mBestPostData.get(position).getGifOrVideoDownloadUrl());
intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName);
intent.putExtra(ViewVideoActivity.ID_KEY, id); intent.putExtra(ViewVideoActivity.ID_KEY, id);
} }
@ -266,7 +272,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
break; break;
case BestPostData.NO_PREVIEW_LINK_TYPE: case BestPostData.NO_PREVIEW_LINK_TYPE:
((DataViewHolder) holder).typeTextView.setText("LINK"); ((DataViewHolder) holder).typeTextView.setText("LINK");
final String noPreviewLinkUrl = bestPostData.get(position).getUrl(); final String noPreviewLinkUrl = mBestPostData.get(position).getUrl();
((DataViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).noPreviewLinkImageView.setVisibility(View.VISIBLE);
((DataViewHolder) holder).noPreviewLinkImageView.setOnClickListener(new View.OnClickListener() { ((DataViewHolder) holder).noPreviewLinkImageView.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -293,19 +299,19 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
if (((DataViewHolder) holder).plusButton.getColorFilter() == null) { if (((DataViewHolder) holder).plusButton.getColorFilter() == null) {
((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
if(isDownvotedBefore) { if(isDownvotedBefore) {
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() + 2)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() + 2));
} else { } else {
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() + 1)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() + 1));
} }
new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
bestPostData.get(position).setVoteType(1); mBestPostData.get(position).setVoteType(1);
if(isDownvotedBefore) { if(isDownvotedBefore) {
bestPostData.get(position).setScore(bestPostData.get(position).getScore() + 2); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() + 2);
} else { } else {
bestPostData.get(position).setScore(bestPostData.get(position).getScore() + 1); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() + 1);
} }
} }
@ -313,28 +319,28 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot upvote this post", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot upvote this post", Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).plusButton.clearColorFilter(); ((DataViewHolder) holder).plusButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore())); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore()));
((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
} }
}, id, RedditUtils.DIR_UPVOTE, ((DataViewHolder) holder).getAdapterPosition(), 1); }, id, RedditUtils.DIR_UPVOTE, ((DataViewHolder) holder).getAdapterPosition(), 1);
} else { } else {
//Upvoted before //Upvoted before
((DataViewHolder) holder).plusButton.clearColorFilter(); ((DataViewHolder) holder).plusButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() - 1)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() - 1));
new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
bestPostData.get(position).setVoteType(0); mBestPostData.get(position).setVoteType(0);
bestPostData.get(position).setScore(bestPostData.get(position).getScore() - 1); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() - 1);
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() + 1)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() + 1));
((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
bestPostData.get(position).setScore(bestPostData.get(position).getScore() + 1); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() + 1);
} }
}, id, RedditUtils.DIR_UNVOTE, ((DataViewHolder) holder).getAdapterPosition(), 1); }, id, RedditUtils.DIR_UNVOTE, ((DataViewHolder) holder).getAdapterPosition(), 1);
} }
@ -350,19 +356,19 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
if (((DataViewHolder) holder).minusButton.getColorFilter() == null) { if (((DataViewHolder) holder).minusButton.getColorFilter() == null) {
((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
if (isUpvotedBefore) { if (isUpvotedBefore) {
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() - 2)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() - 2));
} else { } else {
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() - 1)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() - 1));
} }
new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
bestPostData.get(position).setVoteType(-1); mBestPostData.get(position).setVoteType(-1);
if(isUpvotedBefore) { if(isUpvotedBefore) {
bestPostData.get(position).setScore(bestPostData.get(position).getScore() - 2); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() - 2);
} else { } else {
bestPostData.get(position).setScore(bestPostData.get(position).getScore() - 1); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() - 1);
} }
} }
@ -370,28 +376,28 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot downvote this post", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot downvote this post", Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).minusButton.clearColorFilter(); ((DataViewHolder) holder).minusButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore())); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore()));
((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
} }
}, id, RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition(), 1); }, id, RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition(), 1);
} else { } else {
//Down voted before //Down voted before
((DataViewHolder) holder).minusButton.clearColorFilter(); ((DataViewHolder) holder).minusButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore() + 1)); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore() + 1));
new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() { new VoteThing(mContext, mVoteThingRequestQueue, mAcquireAccessTokenRequestQueue).votePost(new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
bestPostData.get(position).setVoteType(0); mBestPostData.get(position).setVoteType(0);
bestPostData.get(position).setScore(bestPostData.get(position).getScore()); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore());
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot unvote this post", Toast.LENGTH_SHORT).show();
((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); ((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore())); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore()));
bestPostData.get(position).setScore(bestPostData.get(position).getScore()); mBestPostData.get(position).setScore(mBestPostData.get(position).getScore());
} }
}, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition(), 1); }, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition(), 1);
} }
@ -444,12 +450,12 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
@Override @Override
public int getItemCount() { public int getItemCount() {
return bestPostData.size() + 1; return mBestPostData.size() + 1;
} }
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
return (position >= bestPostData.size() ? VIEW_TYPE_LOADING : VIEW_TYPE_DATA); return (position >= mBestPostData.size() ? VIEW_TYPE_LOADING : VIEW_TYPE_DATA);
} }
class DataViewHolder extends RecyclerView.ViewHolder { class DataViewHolder extends RecyclerView.ViewHolder {
@ -524,4 +530,37 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View
} }
} }
} }
private static class LoadSubredditIconAsyncTask extends AsyncTask<Void, Void, Void> {
private Context context;
private CircleImageView iconImageView;
private SubredditDao subredditDao;
private String subredditName;
private String iconImageUrl;
LoadSubredditIconAsyncTask(Context context, CircleImageView iconImageView, SubredditDao subredditDao, String subredditName) {
this.context = context;
this.iconImageView = iconImageView;
this.subredditDao = subredditDao;
this.subredditName = subredditName;
}
@Override
protected Void doInBackground(Void... voids) {
if(subredditDao.getSubredditData(subredditName) != null) {
iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if(iconImageUrl != null) {
Glide.with(context).load(iconImageUrl).into(iconImageView);
} else {
Glide.with(context).load(R.drawable.subreddit_default_icon).into(iconImageView);
}
}
}
} }

View File

@ -16,7 +16,8 @@ import java.util.Map;
class FetchSubscribedThing { class FetchSubscribedThing {
interface FetchSubscribedSubredditsListener { interface FetchSubscribedSubredditsListener {
void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData); ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData);
void onFetchSubscribedSubredditsFail(); void onFetchSubscribedSubredditsFail();
} }
@ -25,15 +26,19 @@ class FetchSubscribedThing {
private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener;
private ArrayList<SubscribedSubredditData> mSubscribedSubredditData; private ArrayList<SubscribedSubredditData> mSubscribedSubredditData;
private ArrayList<SubscribedUserData> mSubscribedUserData; private ArrayList<SubscribedUserData> mSubscribedUserData;
private ArrayList<SubredditData> mSubredditData;
private String mLastItem; private String mLastItem;
FetchSubscribedThing(Context context, RequestQueue requestQueue, ArrayList<SubscribedSubredditData> subscribedSubredditData, FetchSubscribedThing(Context context, RequestQueue requestQueue,
ArrayList<SubscribedUserData> subscribedUserData) { ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData) {
this.context = context; this.context = context;
this.requestQueue = requestQueue; this.requestQueue = requestQueue;
mSubscribedSubredditData = subscribedSubredditData; mSubscribedSubredditData = subscribedSubredditData;
mSubscribedUserData = subscribedUserData; mSubscribedUserData = subscribedUserData;
mSubredditData = subredditData;
} }
void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) {
@ -51,19 +56,20 @@ class FetchSubscribedThing {
@Override @Override
public void onResponse(String response) { public void onResponse(String response) {
new ParseSubscribedThing().parseSubscribedSubreddits(response, mSubscribedSubredditData, new ParseSubscribedThing().parseSubscribedSubreddits(response, mSubscribedSubredditData,
mSubscribedUserData, mSubscribedUserData, mSubredditData,
new ParseSubscribedThing.ParseSubscribedSubredditsListener() { new ParseSubscribedThing.ParseSubscribedSubredditsListener() {
@Override @Override
public void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, public void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData, ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData,
String lastItem) { String lastItem) {
mSubscribedSubredditData = subscribedSubredditData; mSubscribedSubredditData = subscribedSubredditData;
mSubscribedUserData = subscribedUserData; mSubscribedUserData = subscribedUserData;
mLastItem = lastItem; mLastItem = lastItem;
if(mLastItem.equals("null")) { if(mLastItem.equals("null")) {
mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData, mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData,
mSubscribedUserData); mSubscribedUserData, mSubredditData);
} else { } else {
fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime);
} }

View File

@ -1,7 +1,7 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.app.Fragment; import android.support.v4.app.Fragment;
import android.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.arch.lifecycle.Observer; import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders; import android.arch.lifecycle.ViewModelProviders;
import android.content.Context; import android.content.Context;
@ -75,12 +75,12 @@ public class MainActivity extends AppCompatActivity {
startActivity(loginIntent); startActivity(loginIntent);
} else { } else {
if(savedInstanceState == null) { if(savedInstanceState == null) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
mFragment = new BestPostFragment(); mFragment = new BestPostFragment();
fragmentTransaction.replace(R.id.frame_layout_content_main, mFragment).commit(); fragmentTransaction.replace(R.id.frame_layout_content_main, mFragment).commit();
} else { } else {
mFragment = getFragmentManager().getFragment(savedInstanceState, "outStateFragment"); mFragment = getSupportFragmentManager().getFragment(savedInstanceState, "outStateFragment");
getFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit();
} }
Calendar now = Calendar.getInstance(); Calendar now = Calendar.getInstance();
@ -214,17 +214,20 @@ public class MainActivity extends AppCompatActivity {
}); });
new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(), new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(),
new ArrayList<SubscribedUserData>()) new ArrayList<SubscribedUserData>(), new ArrayList<SubredditData>())
.fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() {
@Override @Override
public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData) { ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData) {
mIsInserting = true; mIsInserting = true;
new InsertSubscribedThingsAsyncTask( new InsertSubscribedThingsAsyncTask(
SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this),
SubscribedUserRoomDatabase.getDatabase(MainActivity.this), SubscribedUserRoomDatabase.getDatabase(MainActivity.this),
SubredditRoomDatabase.getDatabase(MainActivity.this),
subscribedSubredditData, subscribedSubredditData,
subscribedUserData, subscribedUserData,
subredditData,
new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() { new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() {
@Override @Override
public void insertSuccess() { public void insertSuccess() {
@ -255,7 +258,7 @@ public class MainActivity extends AppCompatActivity {
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
if(mFragment != null) { if(mFragment != null) {
getFragmentManager().putFragment(outState, "outStateFragment", mFragment); getSupportFragmentManager().putFragment(outState, "outStateFragment", mFragment);
} }
outState.putString(nameState, mName); outState.putString(nameState, mName);
outState.putString(profileImageUrlState, mProfileImageUrl); outState.putString(profileImageUrlState, mProfileImageUrl);
@ -286,32 +289,41 @@ public class MainActivity extends AppCompatActivity {
void insertSuccess(); void insertSuccess();
} }
private final SubscribedSubredditDao mSubredditDao; private final SubscribedSubredditDao mSubscribedSubredditDao;
private final SubscribedUserDao mUserDao; private final SubscribedUserDao mUserDao;
private final SubredditDao mSubredditDao;
private List<SubscribedSubredditData> subscribedSubredditData; private List<SubscribedSubredditData> subscribedSubredditData;
private List<SubscribedUserData> subscribedUserData; private List<SubscribedUserData> subscribedUserData;
private List<SubredditData> subredditData;
private InsertSubscribedThingListener insertSubscribedThingListener; private InsertSubscribedThingListener insertSubscribedThingListener;
InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subredditDb, InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subscribedSubredditDb,
SubscribedUserRoomDatabase userDb, SubscribedUserRoomDatabase userDb,
SubredditRoomDatabase subredditDb,
List<SubscribedSubredditData> subscribedSubredditData, List<SubscribedSubredditData> subscribedSubredditData,
List<SubscribedUserData> subscribedUserData, List<SubscribedUserData> subscribedUserData,
List<SubredditData> subredditData,
InsertSubscribedThingListener insertSubscribedThingListener) { InsertSubscribedThingListener insertSubscribedThingListener) {
mSubredditDao = subredditDb.subscribedSubredditDao(); mSubscribedSubredditDao = subscribedSubredditDb.subscribedSubredditDao();
mUserDao = userDb.subscribedUserDao(); mUserDao = userDb.subscribedUserDao();
mSubredditDao = subredditDb.subredditDao();
this.subscribedSubredditData = subscribedSubredditData; this.subscribedSubredditData = subscribedSubredditData;
this.subscribedUserData = subscribedUserData; this.subscribedUserData = subscribedUserData;
this.subredditData = subredditData;
this.insertSubscribedThingListener = insertSubscribedThingListener; this.insertSubscribedThingListener = insertSubscribedThingListener;
} }
@Override @Override
protected Void doInBackground(final Void... params) { protected Void doInBackground(final Void... params) {
for(SubscribedSubredditData s : subscribedSubredditData) { for(SubscribedSubredditData s : subscribedSubredditData) {
mSubredditDao.insert(s); mSubscribedSubredditDao.insert(s);
} }
for(SubscribedUserData s : subscribedUserData) { for(SubscribedUserData s : subscribedUserData) {
mUserDao.insert(s); mUserDao.insert(s);
} }
for(SubredditData s : subredditData) {
mSubredditDao.insert(s);
}
return null; return null;
} }

View File

@ -43,7 +43,7 @@ class ParseSubredditData {
JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY);
String id = data.getString(JSONUtils.NAME_KEY); String id = data.getString(JSONUtils.NAME_KEY);
String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED); String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED);
String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION); String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim();
String bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY); String bannerImageUrl = data.getString(JSONUtils.BANNER_IMG_KEY);
String iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY); String iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY);
int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY);

View File

@ -13,6 +13,7 @@ class ParseSubscribedThing {
interface ParseSubscribedSubredditsListener { interface ParseSubscribedSubredditsListener {
void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData, ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData,
String lastItem); String lastItem);
void onParseSubscribedSubredditsFail(); void onParseSubscribedSubredditsFail();
} }
@ -21,9 +22,10 @@ class ParseSubscribedThing {
void parseSubscribedSubreddits(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData, void parseSubscribedSubreddits(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData, ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData,
ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) {
mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener; mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener;
new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData).execute(); new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData, subredditData).execute();
} }
private class ParseSubscribedSubredditsAsyncTask extends AsyncTask<Void, Void, Void> { private class ParseSubscribedSubredditsAsyncTask extends AsyncTask<Void, Void, Void> {
@ -32,18 +34,23 @@ class ParseSubscribedThing {
private String lastItem; private String lastItem;
private ArrayList<SubscribedSubredditData> subscribedSubredditData; private ArrayList<SubscribedSubredditData> subscribedSubredditData;
private ArrayList<SubscribedUserData> subscribedUserData; private ArrayList<SubscribedUserData> subscribedUserData;
private ArrayList<SubredditData> subredditData;
private ArrayList<SubscribedSubredditData> newSubscribedSubredditData; private ArrayList<SubscribedSubredditData> newSubscribedSubredditData;
private ArrayList<SubscribedUserData> newSubscribedUserData; private ArrayList<SubscribedUserData> newSubscribedUserData;
private ArrayList<SubredditData> newSubredditData;
ParseSubscribedSubredditsAsyncTask(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData, ParseSubscribedSubredditsAsyncTask(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData,
ArrayList<SubscribedUserData> subscribedUserData){ ArrayList<SubscribedUserData> subscribedUserData,
ArrayList<SubredditData> subredditData){
try { try {
jsonResponse = new JSONObject(response); jsonResponse = new JSONObject(response);
parseFailed = false; parseFailed = false;
this.subscribedSubredditData = subscribedSubredditData; this.subscribedSubredditData = subscribedSubredditData;
this.subscribedUserData = subscribedUserData; this.subscribedUserData = subscribedUserData;
this.subredditData = subredditData;
newSubscribedSubredditData = new ArrayList<>(); newSubscribedSubredditData = new ArrayList<>();
newSubscribedUserData = new ArrayList<>(); newSubscribedUserData = new ArrayList<>();
newSubredditData = new ArrayList<>();
} catch (JSONException e) { } catch (JSONException e) {
Log.i("user info json error", e.getMessage()); Log.i("user info json error", e.getMessage());
mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail();
@ -55,8 +62,10 @@ class ParseSubscribedThing {
try { try {
JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
for(int i = 0; i < children.length(); i++) { for(int i = 0; i < children.length(); i++) {
String name = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.DISPLAY_NAME); JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String iconUrl = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ICON_IMG_KEY); String name = data.getString(JSONUtils.DISPLAY_NAME);
String bannerUrl = data.getString(JSONUtils.BANNER_IMG_KEY);
String iconUrl = data.getString(JSONUtils.ICON_IMG_KEY);
String id = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_KEY); String id = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_KEY);
if(iconUrl.equals("null")) { if(iconUrl.equals("null")) {
iconUrl = ""; iconUrl = "";
@ -67,7 +76,11 @@ class ParseSubscribedThing {
//It's a user //It's a user
newSubscribedUserData.add(new SubscribedUserData(id, name.substring(2), iconUrl)); newSubscribedUserData.add(new SubscribedUserData(id, name.substring(2), iconUrl));
} else { } else {
String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME_PREFIXED);
String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION).trim();
int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY);
newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl)); newSubscribedSubredditData.add(new SubscribedSubredditData(id, name, iconUrl));
newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerUrl, description, nSubscribers));
} }
} }
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
@ -83,8 +96,9 @@ class ParseSubscribedThing {
if(!parseFailed) { if(!parseFailed) {
subscribedSubredditData.addAll(newSubscribedSubredditData); subscribedSubredditData.addAll(newSubscribedSubredditData);
subscribedUserData.addAll(newSubscribedUserData); subscribedUserData.addAll(newSubscribedUserData);
subredditData.addAll(newSubredditData);
mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subscribedSubredditData, mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subscribedSubredditData,
subscribedUserData, lastItem); subscribedUserData, subredditData, lastItem);
} else { } else {
mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail();
} }

View File

@ -15,5 +15,8 @@ public interface SubredditDao {
void deleteAllSubreddits(); void deleteAllSubreddits();
@Query("SELECT * from subreddits WHERE id = :id") @Query("SELECT * from subreddits WHERE id = :id")
LiveData<SubredditData> getSubreddit(String id); LiveData<SubredditData> getSubredditLiveData(String id);
@Query("SELECT * from subreddits WHERE name = :namePrefixed")
SubredditData getSubredditData(String namePrefixed);
} }

View File

@ -6,16 +6,16 @@ import android.os.AsyncTask;
public class SubredditRepository { public class SubredditRepository {
private SubredditDao mSubredditDao; private SubredditDao mSubredditDao;
private LiveData<SubredditData> mSubredditData; private LiveData<SubredditData> mSubredditLiveData;
SubredditRepository(Application application, String id) { SubredditRepository(Application application, String id) {
SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application); SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application);
mSubredditDao = db.subredditDao(); mSubredditDao = db.subredditDao();
mSubredditData = mSubredditDao.getSubreddit(id); mSubredditLiveData = mSubredditDao.getSubredditLiveData(id);
} }
LiveData<SubredditData> getSubredditData() { LiveData<SubredditData> getSubredditLiveData() {
return mSubredditData; return mSubredditLiveData;
} }
public void insert(SubredditData subredditData) { public void insert(SubredditData subredditData) {

View File

@ -9,16 +9,16 @@ import android.support.annotation.NonNull;
public class SubredditViewModel extends AndroidViewModel { public class SubredditViewModel extends AndroidViewModel {
private SubredditRepository mSubredditRepository; private SubredditRepository mSubredditRepository;
private LiveData<SubredditData> mSubredditData; private LiveData<SubredditData> mSubredditLiveData;
public SubredditViewModel(Application application, String id) { SubredditViewModel(Application application, String id) {
super(application); super(application);
mSubredditRepository = new SubredditRepository(application, id); mSubredditRepository = new SubredditRepository(application, id);
mSubredditData = mSubredditRepository.getSubredditData(); mSubredditLiveData = mSubredditRepository.getSubredditLiveData();
} }
public LiveData<SubredditData> getSubredditData() { public LiveData<SubredditData> getSubredditLiveData() {
return mSubredditData; return mSubredditLiveData;
} }
public void insert(SubredditData subredditData) { public void insert(SubredditData subredditData) {

View File

@ -9,7 +9,6 @@ import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@ -53,7 +52,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
scrollRange = appBarLayout.getTotalScrollRange(); scrollRange = appBarLayout.getTotalScrollRange();
} }
if (scrollRange + verticalOffset == 0) { if (scrollRange + verticalOffset == 0) {
Log.i("asfasdf", "asdfasdf");
collapsingToolbarLayout.setTitle(title); collapsingToolbarLayout.setTitle(title);
isShow = true; isShow = true;
} else if(isShow) { } else if(isShow) {
@ -73,7 +71,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id); SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id);
mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class); mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class);
mSubredditViewModel.getSubredditData().observe(this, new Observer<SubredditData>() { mSubredditViewModel.getSubredditLiveData().observe(this, new Observer<SubredditData>() {
@Override @Override
public void onChanged(@Nullable SubredditData subredditData) { public void onChanged(@Nullable SubredditData subredditData) {
if(subredditData != null) { if(subredditData != null) {