diff options
author | Alex Ning <chineseperson5@gmail.com> | 2018-08-10 00:22:56 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2018-08-10 00:22:56 +0000 |
commit | e44a172db0c2fb35f4d5657226c86ad4af0034dc (patch) | |
tree | 4c1f82065cff3465a151f0533eb8e775db87f163 /app/src | |
parent | b0e2e273a7ed49c2f9d8d6d8b2949ace0ebd5bb2 (diff) | |
download | infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.tar infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.tar.gz infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.tar.bz2 infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.tar.lz infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.tar.xz infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.tar.zst infinity-for-reddit-e44a172db0c2fb35f4d5657226c86ad4af0034dc.zip |
Display subreddit icons in best posts, eventually. Rename some methods.
Diffstat (limited to '')
10 files changed, 164 insertions, 93 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java index de4e6eb0..ab3118c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostFragment.java @@ -1,7 +1,7 @@ package ml.docilealligator.infinityforreddit; -import android.app.Fragment; +import android.support.v4.app.Fragment; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -158,7 +158,6 @@ public class BestPostFragment extends Fragment { }; mPaginationSynchronizer.setPaginationRequestQueueSynchronizer(paginationRequestQueueSynchronizer); - return rootView; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java index 94c9efc5..7b70ca08 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.AsyncTask; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.customtabs.CustomTabsIntent; @@ -39,12 +40,13 @@ import de.hdodenhof.circleimageview.CircleImageView; */ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { - private ArrayList<BestPostData> bestPostData; + private ArrayList<BestPostData> mBestPostData; private Context mContext; private PaginationSynchronizer mPaginationSynchronizer; private RequestQueue mVoteThingRequestQueue; private RequestQueue mAcquireAccessTokenRequestQueue; private RequestManager glide; + private SubredditDao subredditDao; private boolean isLoadingMorePostSuccess; private boolean canStartActivity; @@ -56,13 +58,14 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) { if(context != null) { mContext = context; - this.bestPostData = bestPostData; + mBestPostData = bestPostData; mPaginationSynchronizer = paginationSynchronizer; mVoteThingRequestQueue = voteThingRequestQueue; mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue; isLoadingMorePostSuccess = true; canStartActivity = true; glide = Glide.with(mContext); + subredditDao = SubredditRoomDatabase.getDatabase(mContext).subredditDao(); } } @@ -85,16 +88,19 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View @Override public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) { if(holder instanceof DataViewHolder) { - if(bestPostData.get(position) == null) { + if(mBestPostData.get(position) == null) { Log.i("is null", Integer.toString(position)); } else { - final String id = bestPostData.get(position).getFullName(); - final String subredditName = bestPostData.get(position).getSubredditName(); - final String postTime = bestPostData.get(position).getPostTime(); - final String title = bestPostData.get(position).getTitle(); - final String permalink = bestPostData.get(position).getPermalink(); - int voteType = bestPostData.get(position).getVoteType(); - boolean nsfw = bestPostData.get(position).getNSFW(); + final String id = mBestPostData.get(position).getFullName(); + final String subredditName = mBestPostData.get(position).getSubredditName(); + final String postTime = mBestPostData.get(position).getPostTime(); + final String title = mBestPostData.get(position).getTitle(); + final String permalink = mBestPostData.get(position).getPermalink(); + int voteType = mBestPostData.get(position).getVoteType(); + boolean nsfw = mBestPostData.get(position).getNSFW(); + + new LoadSubredditIconAsyncTask(mContext, ((DataViewHolder) holder).subredditImageView, + subredditDao, subredditName).execute(); ((DataViewHolder) holder).cardView.setOnClickListener(new View.OnClickListener() { @Override @@ -103,7 +109,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View canStartActivity = false; Intent intent = new Intent(mContext, ViewPostDetailActivity.class); 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); } } @@ -112,7 +118,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View ((DataViewHolder) holder).subredditNameTextView.setText(subredditName); ((DataViewHolder) holder).postTimeTextView.setText(postTime); ((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) { ((DataViewHolder) holder).nsfwTextView.setVisibility(View.VISIBLE); @@ -129,17 +135,17 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View 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).progressBar.setVisibility(View.VISIBLE); ((DataViewHolder) holder).imageView.setVisibility(View.VISIBLE); } - switch (bestPostData.get(position).getPostType()) { + switch (mBestPostData.get(position).getPostType()) { case BestPostData.IMAGE_TYPE: ((DataViewHolder) holder).typeTextView.setText("IMAGE"); - final String previewImageUrl = bestPostData.get(position).getPreviewUrl(); - final String imageUrl = bestPostData.get(position).getUrl(); + final String previewImageUrl = mBestPostData.get(position).getPreviewUrl(); + final String imageUrl = mBestPostData.get(position).getUrl(); glide.load(previewImageUrl).listener(new RequestListener<Drawable>() { @Override 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; case BestPostData.LINK_TYPE: ((DataViewHolder) holder).typeTextView.setText("LINK"); - String linkPreviewUrl = bestPostData.get(position).getPreviewUrl(); + String linkPreviewUrl = mBestPostData.get(position).getPreviewUrl(); glide.load(linkPreviewUrl).listener(new RequestListener<Drawable>() { @Override 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; } }).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() { @Override public void onClick(View view) { @@ -194,7 +200,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View break; case BestPostData.GIF_VIDEO_TYPE: ((DataViewHolder) holder).typeTextView.setText("GIF"); - String gifVideoPreviewUrl = bestPostData.get(position).getPreviewUrl(); + String gifVideoPreviewUrl = mBestPostData.get(position).getPreviewUrl(); glide.load(gifVideoPreviewUrl).listener(new RequestListener<Drawable>() { @Override 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); - String gifVideoUrl = bestPostData.get(position).getVideoUrl(); + String gifVideoUrl = mBestPostData.get(position).getVideoUrl(); final Uri gifVideoUri = Uri.parse(gifVideoUrl); ((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.setData(gifVideoUri); intent.putExtra(ViewVideoActivity.TITLE_KEY, title); - intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, bestPostData.get(position).isDashVideo()); - intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, bestPostData.get(position).isDownloadableGifOrVideo()); - if(bestPostData.get(position).isDownloadableGifOrVideo()) { - intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, bestPostData.get(position).getGifOrVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mBestPostData.get(position).isDashVideo()); + intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mBestPostData.get(position).isDownloadableGifOrVideo()); + if(mBestPostData.get(position).isDownloadableGifOrVideo()) { + intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mBestPostData.get(position).getGifOrVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); intent.putExtra(ViewVideoActivity.ID_KEY, id); } @@ -230,7 +236,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View break; case BestPostData.VIDEO_TYPE: ((DataViewHolder) holder).typeTextView.setText("VIDEO"); - String videoPreviewUrl = bestPostData.get(position).getPreviewUrl(); + String videoPreviewUrl = mBestPostData.get(position).getPreviewUrl(); glide.load(videoPreviewUrl).listener(new RequestListener<Drawable>() { @Override 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); - String videoUrl = bestPostData.get(position).getVideoUrl(); + String videoUrl = mBestPostData.get(position).getVideoUrl(); final Uri videoUri = Uri.parse(videoUrl); ((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.setData(videoUri); intent.putExtra(ViewVideoActivity.TITLE_KEY, title); - intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, bestPostData.get(position).isDashVideo()); - intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, bestPostData.get(position).isDownloadableGifOrVideo()); - if(bestPostData.get(position).isDownloadableGifOrVideo()) { - intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, bestPostData.get(position).getGifOrVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mBestPostData.get(position).isDashVideo()); + intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mBestPostData.get(position).isDownloadableGifOrVideo()); + if(mBestPostData.get(position).isDownloadableGifOrVideo()) { + intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mBestPostData.get(position).getGifOrVideoDownloadUrl()); intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, subredditName); intent.putExtra(ViewVideoActivity.ID_KEY, id); } @@ -266,7 +272,7 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View break; case BestPostData.NO_PREVIEW_LINK_TYPE: ((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.setOnClickListener(new View.OnClickListener() { @Override @@ -293,19 +299,19 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View if (((DataViewHolder) holder).plusButton.getColorFilter() == null) { ((DataViewHolder) holder).plusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); 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 { - ((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() { @Override public void onVoteThingSuccess(int position) { - bestPostData.get(position).setVoteType(1); + mBestPostData.get(position).setVoteType(1); if(isDownvotedBefore) { - bestPostData.get(position).setScore(bestPostData.get(position).getScore() + 2); + mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() + 2); } 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) { Toast.makeText(mContext, "Cannot upvote this post", Toast.LENGTH_SHORT).show(); ((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); } }, id, RedditUtils.DIR_UPVOTE, ((DataViewHolder) holder).getAdapterPosition(), 1); } else { //Upvoted before ((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() { @Override public void onVoteThingSuccess(int position) { - bestPostData.get(position).setVoteType(0); - bestPostData.get(position).setScore(bestPostData.get(position).getScore() - 1); + mBestPostData.get(position).setVoteType(0); + mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() - 1); } @Override public void onVoteThingFail(int position) { 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); - 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); } @@ -350,19 +356,19 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View if (((DataViewHolder) holder).minusButton.getColorFilter() == null) { ((DataViewHolder) holder).minusButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); 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 { - ((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() { @Override public void onVoteThingSuccess(int position) { - bestPostData.get(position).setVoteType(-1); + mBestPostData.get(position).setVoteType(-1); if(isUpvotedBefore) { - bestPostData.get(position).setScore(bestPostData.get(position).getScore() - 2); + mBestPostData.get(position).setScore(mBestPostData.get(position).getScore() - 2); } 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) { Toast.makeText(mContext, "Cannot downvote this post", Toast.LENGTH_SHORT).show(); ((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); } }, id, RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition(), 1); } else { //Down voted before ((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() { @Override public void onVoteThingSuccess(int position) { - bestPostData.get(position).setVoteType(0); - bestPostData.get(position).setScore(bestPostData.get(position).getScore()); + mBestPostData.get(position).setVoteType(0); + mBestPostData.get(position).setScore(mBestPostData.get(position).getScore()); } @Override public void onVoteThingFail(int position) { 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).scoreTextView.setText(Integer.toString(bestPostData.get(position).getScore())); - bestPostData.get(position).setScore(bestPostData.get(position).getScore()); + ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mBestPostData.get(position).getScore())); + mBestPostData.get(position).setScore(mBestPostData.get(position).getScore()); } }, id, RedditUtils.DIR_UNVOTE, holder.getAdapterPosition(), 1); } @@ -444,12 +450,12 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.View @Override public int getItemCount() { - return bestPostData.size() + 1; + return mBestPostData.size() + 1; } @Override 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 { @@ -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); + } + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java index 4a902c16..d30bfa7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchSubscribedThing.java @@ -16,7 +16,8 @@ import java.util.Map; class FetchSubscribedThing { interface FetchSubscribedSubredditsListener { void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData); + ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData); void onFetchSubscribedSubredditsFail(); } @@ -25,15 +26,19 @@ class FetchSubscribedThing { private FetchSubscribedSubredditsListener mFetchSubscribedSubredditsListener; private ArrayList<SubscribedSubredditData> mSubscribedSubredditData; private ArrayList<SubscribedUserData> mSubscribedUserData; + private ArrayList<SubredditData> mSubredditData; private String mLastItem; - FetchSubscribedThing(Context context, RequestQueue requestQueue, ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData) { + FetchSubscribedThing(Context context, RequestQueue requestQueue, + ArrayList<SubscribedSubredditData> subscribedSubredditData, + ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData) { this.context = context; this.requestQueue = requestQueue; mSubscribedSubredditData = subscribedSubredditData; mSubscribedUserData = subscribedUserData; + mSubredditData = subredditData; } void fetchSubscribedSubreddits(FetchSubscribedSubredditsListener fetchUserInfoListener, final int refreshTime) { @@ -51,19 +56,20 @@ class FetchSubscribedThing { @Override public void onResponse(String response) { new ParseSubscribedThing().parseSubscribedSubreddits(response, mSubscribedSubredditData, - mSubscribedUserData, + mSubscribedUserData, mSubredditData, new ParseSubscribedThing.ParseSubscribedSubredditsListener() { @Override public void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData, String lastItem) { mSubscribedSubredditData = subscribedSubredditData; mSubscribedUserData = subscribedUserData; mLastItem = lastItem; if(mLastItem.equals("null")) { mFetchSubscribedSubredditsListener.onFetchSubscribedSubredditsSuccess(mSubscribedSubredditData, - mSubscribedUserData); + mSubscribedUserData, mSubredditData); } else { fetchSubscribedSubreddits(mFetchSubscribedSubredditsListener, refreshTime); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 664ccb63..87de57a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -1,7 +1,7 @@ package ml.docilealligator.infinityforreddit; -import android.app.Fragment; -import android.app.FragmentTransaction; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentTransaction; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.content.Context; @@ -75,12 +75,12 @@ public class MainActivity extends AppCompatActivity { startActivity(loginIntent); } else { if(savedInstanceState == null) { - FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); + FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); mFragment = new BestPostFragment(); fragmentTransaction.replace(R.id.frame_layout_content_main, mFragment).commit(); } else { - mFragment = getFragmentManager().getFragment(savedInstanceState, "outStateFragment"); - getFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); + mFragment = getSupportFragmentManager().getFragment(savedInstanceState, "outStateFragment"); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_content_main, mFragment).commit(); } Calendar now = Calendar.getInstance(); @@ -214,17 +214,20 @@ public class MainActivity extends AppCompatActivity { }); new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList<SubscribedSubredditData>(), - new ArrayList<SubscribedUserData>()) + new ArrayList<SubscribedUserData>(), new ArrayList<SubredditData>()) .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { @Override public void onFetchSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData) { + ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData) { mIsInserting = true; new InsertSubscribedThingsAsyncTask( SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), SubscribedUserRoomDatabase.getDatabase(MainActivity.this), + SubredditRoomDatabase.getDatabase(MainActivity.this), subscribedSubredditData, subscribedUserData, + subredditData, new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() { @Override public void insertSuccess() { @@ -255,7 +258,7 @@ public class MainActivity extends AppCompatActivity { protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if(mFragment != null) { - getFragmentManager().putFragment(outState, "outStateFragment", mFragment); + getSupportFragmentManager().putFragment(outState, "outStateFragment", mFragment); } outState.putString(nameState, mName); outState.putString(profileImageUrlState, mProfileImageUrl); @@ -286,32 +289,41 @@ public class MainActivity extends AppCompatActivity { void insertSuccess(); } - private final SubscribedSubredditDao mSubredditDao; + private final SubscribedSubredditDao mSubscribedSubredditDao; private final SubscribedUserDao mUserDao; + private final SubredditDao mSubredditDao; private List<SubscribedSubredditData> subscribedSubredditData; private List<SubscribedUserData> subscribedUserData; + private List<SubredditData> subredditData; private InsertSubscribedThingListener insertSubscribedThingListener; - InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subredditDb, + InsertSubscribedThingsAsyncTask(SubscribedSubredditRoomDatabase subscribedSubredditDb, SubscribedUserRoomDatabase userDb, + SubredditRoomDatabase subredditDb, List<SubscribedSubredditData> subscribedSubredditData, List<SubscribedUserData> subscribedUserData, + List<SubredditData> subredditData, InsertSubscribedThingListener insertSubscribedThingListener) { - mSubredditDao = subredditDb.subscribedSubredditDao(); + mSubscribedSubredditDao = subscribedSubredditDb.subscribedSubredditDao(); mUserDao = userDb.subscribedUserDao(); + mSubredditDao = subredditDb.subredditDao(); this.subscribedSubredditData = subscribedSubredditData; this.subscribedUserData = subscribedUserData; + this.subredditData = subredditData; this.insertSubscribedThingListener = insertSubscribedThingListener; } @Override protected Void doInBackground(final Void... params) { for(SubscribedSubredditData s : subscribedSubredditData) { - mSubredditDao.insert(s); + mSubscribedSubredditDao.insert(s); } for(SubscribedUserData s : subscribedUserData) { mUserDao.insert(s); } + for(SubredditData s : subredditData) { + mSubredditDao.insert(s); + } return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java index 8f89cc0e..3c31be30 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubredditData.java @@ -43,7 +43,7 @@ class ParseSubredditData { JSONObject data = jsonResponse.getJSONObject(JSONUtils.DATA_KEY); String id = data.getString(JSONUtils.NAME_KEY); 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 iconImageUrl = data.getString(JSONUtils.ICON_IMG_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java index 4aed5743..cc1754ac 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseSubscribedThing.java @@ -13,6 +13,7 @@ class ParseSubscribedThing { interface ParseSubscribedSubredditsListener { void onParseSubscribedSubredditsSuccess(ArrayList<SubscribedSubredditData> subscribedSubredditData, ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData, String lastItem); void onParseSubscribedSubredditsFail(); } @@ -21,9 +22,10 @@ class ParseSubscribedThing { void parseSubscribedSubreddits(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData, ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData, ParseSubscribedSubredditsListener parseSubscribedSubredditsListener) { mParseSubscribedSubredditsListener = parseSubscribedSubredditsListener; - new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData).execute(); + new ParseSubscribedSubredditsAsyncTask(response, subscribedSubredditData, subscribedUserData, subredditData).execute(); } private class ParseSubscribedSubredditsAsyncTask extends AsyncTask<Void, Void, Void> { @@ -32,18 +34,23 @@ class ParseSubscribedThing { private String lastItem; private ArrayList<SubscribedSubredditData> subscribedSubredditData; private ArrayList<SubscribedUserData> subscribedUserData; + private ArrayList<SubredditData> subredditData; private ArrayList<SubscribedSubredditData> newSubscribedSubredditData; private ArrayList<SubscribedUserData> newSubscribedUserData; + private ArrayList<SubredditData> newSubredditData; ParseSubscribedSubredditsAsyncTask(String response, ArrayList<SubscribedSubredditData> subscribedSubredditData, - ArrayList<SubscribedUserData> subscribedUserData){ + ArrayList<SubscribedUserData> subscribedUserData, + ArrayList<SubredditData> subredditData){ try { jsonResponse = new JSONObject(response); parseFailed = false; this.subscribedSubredditData = subscribedSubredditData; this.subscribedUserData = subscribedUserData; + this.subredditData = subredditData; newSubscribedSubredditData = new ArrayList<>(); newSubscribedUserData = new ArrayList<>(); + newSubredditData = new ArrayList<>(); } catch (JSONException e) { Log.i("user info json error", e.getMessage()); mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); @@ -55,8 +62,10 @@ class ParseSubscribedThing { try { JSONArray children = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); for(int i = 0; i < children.length(); i++) { - String name = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.DISPLAY_NAME); - String iconUrl = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ICON_IMG_KEY); + JSONObject data = children.getJSONObject(i).getJSONObject(JSONUtils.DATA_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); if(iconUrl.equals("null")) { iconUrl = ""; @@ -67,7 +76,11 @@ class ParseSubscribedThing { //It's a user newSubscribedUserData.add(new SubscribedUserData(id, name.substring(2), iconUrl)); } 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)); + newSubredditData.add(new SubredditData(id, subredditFullName, iconUrl, bannerUrl, description, nSubscribers)); } } lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); @@ -83,8 +96,9 @@ class ParseSubscribedThing { if(!parseFailed) { subscribedSubredditData.addAll(newSubscribedSubredditData); subscribedUserData.addAll(newSubscribedUserData); + subredditData.addAll(newSubredditData); mParseSubscribedSubredditsListener.onParseSubscribedSubredditsSuccess(subscribedSubredditData, - subscribedUserData, lastItem); + subscribedUserData, subredditData, lastItem); } else { mParseSubscribedSubredditsListener.onParseSubscribedSubredditsFail(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java index 7fb49f62..8fb1593c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java @@ -15,5 +15,8 @@ public interface SubredditDao { void deleteAllSubreddits(); @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); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java index 01320b3d..a104fed7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java @@ -6,16 +6,16 @@ import android.os.AsyncTask; public class SubredditRepository { private SubredditDao mSubredditDao; - private LiveData<SubredditData> mSubredditData; + private LiveData<SubredditData> mSubredditLiveData; SubredditRepository(Application application, String id) { SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application); mSubredditDao = db.subredditDao(); - mSubredditData = mSubredditDao.getSubreddit(id); + mSubredditLiveData = mSubredditDao.getSubredditLiveData(id); } - LiveData<SubredditData> getSubredditData() { - return mSubredditData; + LiveData<SubredditData> getSubredditLiveData() { + return mSubredditLiveData; } public void insert(SubredditData subredditData) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java index b3110347..6d9e20fc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java @@ -9,16 +9,16 @@ import android.support.annotation.NonNull; public class SubredditViewModel extends AndroidViewModel { private SubredditRepository mSubredditRepository; - private LiveData<SubredditData> mSubredditData; + private LiveData<SubredditData> mSubredditLiveData; - public SubredditViewModel(Application application, String id) { + SubredditViewModel(Application application, String id) { super(application); mSubredditRepository = new SubredditRepository(application, id); - mSubredditData = mSubredditRepository.getSubredditData(); + mSubredditLiveData = mSubredditRepository.getSubredditLiveData(); } - public LiveData<SubredditData> getSubredditData() { - return mSubredditData; + public LiveData<SubredditData> getSubredditLiveData() { + return mSubredditLiveData; } public void insert(SubredditData subredditData) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index c7edcb93..887beb01 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -9,7 +9,6 @@ import android.support.design.widget.AppBarLayout; import android.support.design.widget.CollapsingToolbarLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.MenuItem; import android.widget.ImageView; import android.widget.TextView; @@ -53,7 +52,6 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { scrollRange = appBarLayout.getTotalScrollRange(); } if (scrollRange + verticalOffset == 0) { - Log.i("asfasdf", "asdfasdf"); collapsingToolbarLayout.setTitle(title); isShow = true; } else if(isShow) { @@ -73,7 +71,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity { SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id); mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class); - mSubredditViewModel.getSubredditData().observe(this, new Observer<SubredditData>() { + mSubredditViewModel.getSubredditLiveData().observe(this, new Observer<SubredditData>() { @Override public void onChanged(@Nullable SubredditData subredditData) { if(subredditData != null) { |