diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-02-20 14:13:26 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-02-20 14:13:26 +0000 |
commit | c00aaf04b9c93201a6555f9756de33862a70485e (patch) | |
tree | 7fc327675683942e5bc3e8ec0113af957e8dc5ae /app/src/main/java | |
parent | 0d6296c1a4d3a17cf077eea371d91093d766b6cf (diff) | |
download | infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.tar infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.tar.gz infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.tar.bz2 infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.tar.lz infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.tar.xz infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.tar.zst infinity-for-reddit-c00aaf04b9c93201a6555f9756de33862a70485e.zip |
Temporarily use the old way to load comments instead of Paging library. Load more parent-level comments once. Loading comments of comments is now working.
Diffstat (limited to '')
6 files changed, 236 insertions, 136 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java index 0f7e191c..44bc7fe8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java @@ -21,7 +21,6 @@ import android.widget.Toast; import com.multilevelview.models.RecyclerViewItem; -import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -77,7 +76,7 @@ public class CommentAdapter extends PagedListAdapter<CommentData, RecyclerView.V @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) { - return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_comment, parent, false)); + return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false)); } @Override @@ -122,16 +121,22 @@ public class CommentAdapter extends PagedListAdapter<CommentData, RecyclerView.V ((CommentViewHolder) viewHolder).expandButton.setOnClickListener(view -> { if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) { - //mRecyclerView.toggleItemsGroup(viewHolder.getAdapterPosition()); setExpandButton(((CommentViewHolder) viewHolder).expandButton, commentItem.isExpanded()); } else { ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar.setVisibility(View.VISIBLE); FetchComment.fetchComment(mRetrofit, subredditNamePrefixed, article, commentItem.getId(), - new FetchComment.FetchCommentListener() { + locale, false, commentItem.getDepth(), new FetchComment.FetchCommentListener() { @Override - public void onFetchCommentSuccess(String response) { - ParseComment.parseComment(response, new ArrayList<CommentData>(), - locale, false, commentItem.getDepth(), 1, + public void onFetchCommentSuccess(List<?> commentData, + String parentId, String commaSeparatedChildren) { + commentItem.addChildren((List<RecyclerViewItem>) commentData); + ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar + .setVisibility(View.GONE); + ((CommentViewHolder) viewHolder).expandButton + .setImageResource(R.drawable.ic_expand_less_black_20dp); + + /*ParseComment.parseComment(response, new ArrayList<>(), + locale, false, commentItem.getDepth(), new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(List<?> commentData, @@ -139,7 +144,6 @@ public class CommentAdapter extends PagedListAdapter<CommentData, RecyclerView.V commentItem.addChildren((List<RecyclerViewItem>) commentData); ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar .setVisibility(View.GONE); - //mRecyclerView.toggleItemsGroup(viewHolder.getAdapterPosition()); ((CommentViewHolder) viewHolder).expandButton .setImageResource(R.drawable.ic_expand_less_black_20dp); } @@ -149,12 +153,13 @@ public class CommentAdapter extends PagedListAdapter<CommentData, RecyclerView.V ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar .setVisibility(View.GONE); } - }); + });*/ } @Override - public void onFetchCommentFail() { - + public void onFetchCommentFailed() { + ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar + .setVisibility(View.GONE); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java index 1c06ba04..9e98f29a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java @@ -74,7 +74,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Post> { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if(response.isSuccessful()) { ParseComment.parseComment(response.body(), new ArrayList<>(), locale, isPost, - 0, 1, new ParseComment.ParseCommentListener() { + 0, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(List<?> commentData, String parentId, String commaSeparatedChildren) { @@ -138,8 +138,8 @@ public class CommentDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(Call<String> call, Response<String> response) { if(response.isSuccessful()) { - ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale, isPost, - 0, 0, new ParseComment.ParseCommentListener() { + ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale, + 0, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(List<?> commentData, String parentId, String commaSeparatedChildren) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java index 719bd833..77adf5f1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java @@ -9,6 +9,7 @@ import android.support.annotation.NonNull; import android.support.customtabs.CustomTabsIntent; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -61,7 +62,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_comment, parent, false)); + return new CommentViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false)); } @Override @@ -78,11 +79,11 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { ((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime()); SpannableConfiguration spannableConfiguration = SpannableConfiguration.builder(mContext).linkResolver((view, link) -> { - if(link.startsWith("/u/")) { + if (link.startsWith("/u/")) { Intent intent = new Intent(mContext, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3)); mContext.startActivity(intent); - } else if(link.startsWith("/r/")) { + } else if (link.startsWith("/r/")) { Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3)); mContext.startActivity(intent); @@ -100,45 +101,36 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore())); ((CommentViewHolder) holder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16; - if(commentItem.hasReply()) { + if (commentItem.hasReply()) { setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded()); } ((CommentViewHolder) holder).expandButton.setOnClickListener(view -> { - if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) { + if (commentItem.hasChildren() && commentItem.getChildren().size() > 0) { mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded()); } else { ((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.VISIBLE); FetchComment.fetchComment(mRetrofit, subredditNamePrefixed, article, commentItem.getId(), - new FetchComment.FetchCommentListener() { + locale, false, commentItem.getDepth(), new FetchComment.FetchCommentListener() { @Override - public void onFetchCommentSuccess(String response) { - ParseComment.parseComment(response, new ArrayList<CommentData>(), - locale, false, commentItem.getDepth(), 1, - new ParseComment.ParseCommentListener() { - @Override - public void onParseCommentSuccess(List<?> commentData, - String parentId, String commaSeparatedChildren) { - commentItem.addChildren((List<RecyclerViewItem>) commentData); - ((CommentViewHolder) holder).loadMoreCommentsProgressBar - .setVisibility(View.GONE); - mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); - ((CommentViewHolder) holder).expandButton - .setImageResource(R.drawable.ic_expand_less_black_20dp); - } - - @Override - public void onParseCommentFailed() { - ((CommentViewHolder) holder).loadMoreCommentsProgressBar - .setVisibility(View.GONE); - } - }); + public void onFetchCommentSuccess(List<?> commentData, + String parentId, String commaSeparatedChildren) { + commentItem.addChildren((List<RecyclerViewItem>) commentData); + for (RecyclerViewItem r : (List<RecyclerViewItem>) commentData) { + Log.i("asdfasdfasd", Integer.toString(r.getLevel())); + } + ((CommentViewHolder) holder).loadMoreCommentsProgressBar + .setVisibility(View.GONE); + mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); + ((CommentViewHolder) holder).expandButton + .setImageResource(R.drawable.ic_expand_less_black_20dp); } @Override - public void onFetchCommentFail() { - + public void onFetchCommentFailed() { + ((CommentViewHolder) holder).loadMoreCommentsProgressBar + .setVisibility(View.GONE); } }); } @@ -162,17 +154,17 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) { ((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - if(isDownvotedBefore) { + if (isDownvotedBefore) { ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2)); } else { ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); } - VoteThing.voteThing(mOauthRetrofit,mSharedPreferences, new VoteThing.VoteThingListener() { + VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { @Override public void onVoteThingSuccess(int position1) { commentItem.setVoteType(1); - if(isDownvotedBefore) { + if (isDownvotedBefore) { commentItem.setScore(commentItem.getScore() + 2); } else { commentItem.setScore(commentItem.getScore() + 1); @@ -228,7 +220,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @Override public void onVoteThingSuccess(int position12) { commentItem.setVoteType(-1); - if(isUpvotedBefore) { + if (isUpvotedBefore) { commentItem.setScore(commentItem.getScore() - 2); } else { commentItem.setScore(commentItem.getScore() - 1); @@ -269,8 +261,16 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @Override public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { - ((CommentViewHolder) holder).expandButton.setVisibility(View.GONE); - ((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.GONE); + if (holder instanceof CommentViewHolder) { + ((CommentViewHolder) holder).expandButton.setVisibility(View.GONE); + ((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.GONE); + } + } + + void addComments(ArrayList<CommentData> comments) { + int sizeBefore = mCommentData.size(); + mCommentData.addAll(comments); + notifyItemRangeInserted(sizeBefore, mCommentData.size() - sizeBefore); } private void setExpandButton(ImageView expandButton, boolean isExpanded) { @@ -280,16 +280,26 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { } class CommentViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.author_text_view_item_post_comment) TextView authorTextView; - @BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView; - @BindView(R.id.comment_markdown_view_item_post_comment) MarkwonView commentMarkdownView; - @BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton; - @BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView; - @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; - @BindView(R.id.expand_button_item_post_comment) ImageView expandButton; - @BindView(R.id.load_more_comments_progress_bar) ProgressBar loadMoreCommentsProgressBar; - @BindView(R.id.reply_button_item_post_comment) ImageView replyButton; - @BindView(R.id.vertical_block_item_post_comment) View verticalBlock; + @BindView(R.id.author_text_view_item_post_comment) + TextView authorTextView; + @BindView(R.id.comment_time_text_view_item_post_comment) + TextView commentTimeTextView; + @BindView(R.id.comment_markdown_view_item_post_comment) + MarkwonView commentMarkdownView; + @BindView(R.id.plus_button_item_post_comment) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_comment) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_comment) + ImageView downvoteButton; + @BindView(R.id.expand_button_item_post_comment) + ImageView expandButton; + @BindView(R.id.load_more_comments_progress_bar) + ProgressBar loadMoreCommentsProgressBar; + @BindView(R.id.reply_button_item_post_comment) + ImageView replyButton; + @BindView(R.id.vertical_block_item_post_comment) + View verticalBlock; CommentViewHolder(View itemView) { super(itemView); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index c0844122..7faf76fb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -3,6 +3,10 @@ package ml.docilealligator.infinityforreddit; import android.support.annotation.NonNull; import android.util.Log; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -10,30 +14,115 @@ import retrofit2.Retrofit; class FetchComment { interface FetchCommentListener { - void onFetchCommentSuccess(String response); - void onFetchCommentFail(); + void onFetchCommentSuccess(List<?> commentData, + String parentId, String commaSeparatedChildren); + void onFetchCommentFailed(); + } + + interface FetchMoreCommentListener { + void onFetchMoreCommentSuccess(List<?> commentData); + void onFetchMoreCommentFailed(); } - static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article, String comment, + static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article, + String comment, Locale locale, boolean isPost, int parentDepth, final FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> comments = api.getComments(subredditNamePrefixed, article, comment); comments.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if(response.isSuccessful()) { - fetchCommentListener.onFetchCommentSuccess(response.body()); + ParseComment.parseComment(response.body(), new ArrayList<>(), + locale, isPost, parentDepth, + new ParseComment.ParseCommentListener() { + @Override + public void onParseCommentSuccess(List<?> commentData, + String parentId, String commaSeparatedChildren) { + fetchCommentListener.onFetchCommentSuccess(commentData, parentId, + commaSeparatedChildren); + } + + @Override + public void onParseCommentFailed() { + Log.i("parse failed", "parse failed"); + fetchCommentListener.onFetchCommentFailed(); + } + }); } else { Log.i("call failed", response.message()); - fetchCommentListener.onFetchCommentFail(); + fetchCommentListener.onFetchCommentFailed(); } } @Override public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { Log.i("call failed", t.getMessage()); - fetchCommentListener.onFetchCommentFail(); + fetchCommentListener.onFetchCommentFailed(); + } + }); + } + + static void fetchMoreComment(Retrofit retrofit, String subredditNamePrefixed, String mParentId, + String children, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { + RedditAPI api = retrofit.create(RedditAPI.class); + Call<String> moreChildrenBasicInfo = api.getMoreChildren(mParentId, children); + moreChildrenBasicInfo.enqueue(new Callback<String>() { + @Override + public void onResponse(Call<String> call, Response<String> response) { + if(response.isSuccessful()) { + ParseComment.parseMoreCommentBasicInfo(response.body(), new ParseComment.ParseMoreCommentBasicInfoListener() { + @Override + public void onParseMoreCommentBasicInfoSuccess(String commaSeparatedChildrenId) { + Call<String> moreComments = api.getInfo(subredditNamePrefixed, commaSeparatedChildrenId); + moreComments.enqueue(new Callback<String>() { + @Override + public void onResponse(Call<String> call, Response<String> response) { + if(response.isSuccessful()) { + ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale, + 0, new ParseComment.ParseCommentListener() { + @Override + public void onParseCommentSuccess(List<?> commentData, String parentId, + String commaSeparatedChildren) { + fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData); + } + + @Override + public void onParseCommentFailed() { + fetchMoreCommentListener.onFetchMoreCommentFailed(); + Log.i("comment parse failed", "comment parse failed"); + } + }); + } else { + Log.i("more comment failed", response.message()); + fetchMoreCommentListener.onFetchMoreCommentFailed(); + } + } + + @Override + public void onFailure(Call<String> call, Throwable t) { + Log.i("more comment failed", t.getMessage()); + fetchMoreCommentListener.onFetchMoreCommentFailed(); + } + }); + } + + @Override + public void onParseMoreCommentBasicInfoFailed() { + Log.i("comment parse failed", "comment parse failed"); + fetchMoreCommentListener.onFetchMoreCommentFailed(); + } + }); + } else { + Log.i("basic info failed", response.message()); + fetchMoreCommentListener.onFetchMoreCommentFailed(); + } + } + + @Override + public void onFailure(Call<String> call, Throwable t) { + Log.i("basic info failed", t.getMessage()); + fetchMoreCommentListener.onFetchMoreCommentFailed(); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 54b4af1d..33b83084 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -25,18 +25,18 @@ class ParseComment { } static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale, - boolean isPost, int parentDepth, int childrenStartIndex, ParseCommentListener parseCommentListener) { + boolean isPost, int parentDepth, ParseCommentListener parseCommentListener) { try { JSONArray childrenArray = new JSONArray(response); if(isPost) { - childrenArray = childrenArray.getJSONObject(childrenStartIndex).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); + childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); } else { - childrenArray = childrenArray.getJSONObject(childrenStartIndex).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY) + childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY) .getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONObject(JSONUtils.REPLIES_KEY) .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); } - new ParseCommentAsyncTask(childrenArray, commentData, locale, isPost, parentDepth, childrenStartIndex, parseCommentListener).execute(); + new ParseCommentAsyncTask(childrenArray, commentData, locale, parentDepth, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); Log.i("comment json error", e.getMessage()); @@ -49,10 +49,10 @@ class ParseComment { } static void parseMoreComment(String response, ArrayList<CommentData> commentData, Locale locale, - boolean isPost, int parentDepth, int childrenStartIndex, ParseCommentListener parseCommentListener) { + int parentDepth, ParseCommentListener parseCommentListener) { try { JSONArray childrenArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - new ParseCommentAsyncTask(childrenArray, commentData, locale, isPost, parentDepth, childrenStartIndex, parseCommentListener).execute(); + new ParseCommentAsyncTask(childrenArray, commentData, locale, parentDepth, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); Log.i("comment json error", e.getMessage()); @@ -66,23 +66,19 @@ class ParseComment { private ArrayList<CommentData> newcommentData; private StringBuilder commaSeparatedChildren; private Locale locale; - private boolean isPost; private int parentDepth; - private int childrenStartIndex; private ParseCommentListener parseCommentListener; private boolean parseFailed; private String parentId; ParseCommentAsyncTask(JSONArray comments, ArrayList<CommentData> commentData, Locale locale, - boolean isPost, int parentDepth, int childrenStartIndex, ParseCommentListener parseCommentListener){ + int parentDepth, ParseCommentListener parseCommentListener){ this.comments = comments; this.commentData = commentData; newcommentData = new ArrayList<>(); commaSeparatedChildren = new StringBuilder(); this.locale = locale; - this.isPost = isPost; this.parentDepth = parentDepth; - this.childrenStartIndex = childrenStartIndex; parseFailed = false; this.parseCommentListener = parseCommentListener; } @@ -93,13 +89,6 @@ class ParseComment { int actualCommentLength; ArrayList<String> children = new ArrayList<>(); - /*if(isPost) { - allComments = comments.getJSONObject(childrenStartIndex).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - } else { - allComments = comments.getJSONObject(childrenStartIndex).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY) - .getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONObject(JSONUtils.REPLIES_KEY) - .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - }*/ if(comments.length() == 0) { return null; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 854a75dd..181700cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.arch.lifecycle.ViewModelProviders; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.ColorFilter; @@ -19,7 +18,6 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.CardView; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; @@ -38,8 +36,12 @@ import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; +import com.multilevelview.MultiLevelRecyclerView; import com.santalu.aspectratioimageview.AspectRatioImageView; +import java.util.ArrayList; +import java.util.List; + import javax.inject.Inject; import javax.inject.Named; @@ -65,6 +67,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { private Post mPost; + private String mCommaSeparatedChildren; + @BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.subreddit_icon_name_linear_layout_view_post_detail) LinearLayout mSubredditIconNameLinearLayout; @BindView(R.id.subreddit_icon_circle_image_view_view_post_detail) AspectRatioGifImageView mSubredditIconGifImageView; @@ -91,11 +95,13 @@ public class ViewPostDetailActivity extends AppCompatActivity { @BindView(R.id.comment_progress_bar_view_post_detail) CircleProgressBar mCommentProgressbar; @BindView(R.id.comment_card_view_view_post_detail) CardView mCommentCardView; - @BindView(R.id.recycler_view_view_post_detail) RecyclerView mRecyclerView; + @BindView(R.id.recycler_view_view_post_detail) MultiLevelRecyclerView mRecyclerView; @BindView(R.id.no_comment_wrapper_linear_layout_view_post_detail) LinearLayout mNoCommentWrapperLinearLayout; @BindView(R.id.no_comment_image_view_view_post_detail) ImageView mNoCommentImageView; + private CommentMultiLevelRecyclerViewAdapter mAdapter; + private LinearLayoutManager mLayoutManager; private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; @Inject @Named("no_oauth") @@ -208,8 +214,9 @@ public class ViewPostDetailActivity extends AppCompatActivity { mCrosspostImageView.setVisibility(View.VISIBLE); } + mLayoutManager = new LinearLayoutManager(this); mRecyclerView.setNestedScrollingEnabled(false); - mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); mSubredditTextView.setText(mPost.getSubredditNamePrefixed()); @@ -327,9 +334,10 @@ public class ViewPostDetailActivity extends AppCompatActivity { } break; } - //queryComment(); - CommentAdapter adapter = new CommentAdapter(this, mRetrofit, mOauthRetrofit, mSharedPreferences, + fetchComment(); + + /*CommentAdapter mAdapter = new CommentAdapter(this, mRetrofit, mOauthRetrofit, mSharedPreferences, mRecyclerView, mPost.getSubredditNamePrefixed(), mPost.getId(), getResources().getConfiguration().locale); CommentViewModel.Factory factory = new CommentViewModel.Factory(mRetrofit, getResources().getConfiguration().locale, @@ -337,10 +345,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { @Override public void hasComment() { mCommentProgressbar.setVisibility(View.GONE); - /*mRecyclerView.removeItemClickListeners(); - mRecyclerView.setToggleItemOnClick(false); - mRecyclerView.setAccordion(false);*/ - mRecyclerView.setAdapter(adapter); + mRecyclerView.setAdapter(mAdapter); mCommentCardView.setVisibility(View.VISIBLE); } @@ -351,7 +356,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); CommentViewModel commentViewModel = ViewModelProviders.of(this, factory).get(CommentViewModel.class); - commentViewModel.getComments().observe(this, posts -> adapter.submitList(posts)); + commentViewModel.getComments().observe(this, posts -> mAdapter.submitList(posts));*/ mUpvoteButton.setOnClickListener(view -> { @@ -467,52 +472,59 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); } - /*private void queryComment() { + private void fetchComment() { mCommentProgressbar.setVisibility(View.VISIBLE); mNoCommentWrapperLinearLayout.setVisibility(View.GONE); FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), - null, new FetchComment.FetchCommentListener() { + null, getResources().getConfiguration().locale, true, 0, new FetchComment.FetchCommentListener() { @Override - public void onFetchCommentSuccess(String response) { - ParseComment.parseComment(response, new ArrayList<CommentData>(), - getResources().getConfiguration().locale, true, 0, 1, - new ParseComment.ParseCommentListener() { - @Override - public void onParseCommentSuccess(List<?> commentData, - String parentId, String commaSeparatedChildren) { - mCommentProgressbar.setVisibility(View.GONE); - if (commentData.size() > 0) { - CommentMultiLevelRecyclerViewAdapter adapter = new CommentMultiLevelRecyclerViewAdapter( - ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, - mSharedPreferences, (ArrayList<CommentData>) commentData, - mRecyclerView, mPost.getSubredditNamePrefixed(), - mPost.getId(), getResources().getConfiguration().locale); - mRecyclerView.removeItemClickListeners(); - mRecyclerView.setToggleItemOnClick(false); - mRecyclerView.setAccordion(false); - mRecyclerView.setAdapter(adapter); - mCommentCardView.setVisibility(View.VISIBLE); - } else { - mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); - glide.load(R.drawable.no_comment_indicator).into(mNoCommentImageView); - } - } - - @Override - public void onParseCommentFailed() { - mCommentProgressbar.setVisibility(View.GONE); - showRetrySnackbar(); - } - }); + public void onFetchCommentSuccess(List<?> commentData, + String parentId, String commaSeparatedChildren) { + mCommaSeparatedChildren = commaSeparatedChildren; + mCommentProgressbar.setVisibility(View.GONE); + if (commentData.size() > 0) { + mAdapter = new CommentMultiLevelRecyclerViewAdapter( + ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, + mSharedPreferences, (ArrayList<CommentData>) commentData, + mRecyclerView, mPost.getSubredditNamePrefixed(), + mPost.getId(), getResources().getConfiguration().locale); + mRecyclerView.removeItemClickListeners(); + mRecyclerView.setToggleItemOnClick(false); + mRecyclerView.setAccordion(false); + mRecyclerView.setAdapter(mAdapter); + mCommentCardView.setVisibility(View.VISIBLE); + + fetchMoreComment(); + } else { + mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); + glide.load(R.drawable.no_comment_indicator).into(mNoCommentImageView); + } } @Override - public void onFetchCommentFail() { + public void onFetchCommentFailed() { mCommentProgressbar.setVisibility(View.GONE); showRetrySnackbar(); } }); - }*/ + } + + private void fetchMoreComment() { + FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getFullName(), + mCommaSeparatedChildren, getResources().getConfiguration().locale, new FetchComment.FetchMoreCommentListener() { + @Override + public void onFetchMoreCommentSuccess(List<?> commentData) { + mAdapter.addComments((ArrayList<CommentData>) commentData); + } + + @Override + public void onFetchMoreCommentFailed() { + Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.load_more_comment_failed, Snackbar.LENGTH_INDEFINITE); + snackbar.setAction(R.string.retry, view -> fetchMoreComment()); + snackbar.show(); + } + }); + } private void loadImage() { RequestBuilder imageRequestBuilder = glide.load(mPost.getPreviewUrl()) @@ -568,12 +580,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private void showRetrySnackbar() { Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.load_comment_failed, Snackbar.LENGTH_INDEFINITE); - snackbar.setAction(R.string.retry, new View.OnClickListener() { - @Override - public void onClick(View view) { - //queryComment(); - } - }); + snackbar.setAction(R.string.retry, view -> fetchComment()); snackbar.show(); } |