Replace the RecyclerView for comments to MultiLevelRecyclerView in order to display the comment trees easily. Click the expand more button to expand the comment tree. Click the expand less button to hide the comment tree. Rewrite some code.

This commit is contained in:
Alex Ning 2018-09-03 00:05:42 +08:00
parent 6637cefc05
commit 06a692130c
15 changed files with 303 additions and 156 deletions

Binary file not shown.

View File

@ -59,4 +59,5 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.4.0' implementation 'com.squareup.retrofit2:converter-scalars:2.4.0'
implementation 'jp.wasabeef:glide-transformations:3.3.0' implementation 'jp.wasabeef:glide-transformations:3.3.0'
implementation 'com.muditsen.multilevelrecyclerview:multilevelview:1.0.0'
} }

View File

@ -1,6 +1,9 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
class CommentData { import com.multilevelview.models.RecyclerViewItem;
class CommentData extends RecyclerViewItem {
private String id;
private String fullName; private String fullName;
private String author; private String author;
private String commentTime; private String commentTime;
@ -14,9 +17,11 @@ class CommentData {
private boolean hasReply; private boolean hasReply;
private boolean scoreHidden; private boolean scoreHidden;
CommentData(String fullName, String author, String commentTime, String commentContent, int score, CommentData(String id, String fullName, String author, String commentTime, String commentContent,
boolean isSubmitter, String permalink, int depth, boolean collapsed, boolean hasReply, int score, boolean isSubmitter, String permalink, int depth, boolean collapsed,
boolean scoreHidden) { boolean hasReply, boolean scoreHidden) {
super(depth);
this.id = id;
this.fullName = fullName; this.fullName = fullName;
this.author = author; this.author = author;
this.commentTime = commentTime; this.commentTime = commentTime;
@ -30,6 +35,9 @@ class CommentData {
this.scoreHidden = scoreHidden; this.scoreHidden = scoreHidden;
} }
public String getId() {
return id;
}
public String getFullName() { public String getFullName() {
return fullName; return fullName;
@ -71,7 +79,7 @@ class CommentData {
return collapsed; return collapsed;
} }
public boolean isHasReply() { public boolean hasReply() {
return hasReply; return hasReply;
} }

View File

@ -9,36 +9,100 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.multilevelview.MultiLevelAdapter;
import com.multilevelview.MultiLevelRecyclerView;
import com.multilevelview.models.RecyclerViewItem;
import org.sufficientlysecure.htmltextview.HtmlTextView; import org.sufficientlysecure.htmltextview.HtmlTextView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
private Context mContext; private Context mContext;
private ArrayList<CommentData> mCommentData; private ArrayList<CommentData> mCommentData;
private MultiLevelRecyclerView mMultiLevelRecyclerView;
private String subredditNamePrefixed;
private String article;
private Locale locale;
CommentRecyclerViewAdapter(Context context, ArrayList<CommentData> commentData) { CommentMultiLevelRecyclerViewAdapter(Context context, ArrayList<CommentData> commentData,
MultiLevelRecyclerView multiLevelRecyclerView,
String subredditNamePrefixed, String article, Locale locale) {
super(commentData);
mContext = context; mContext = context;
mCommentData = commentData; mCommentData = commentData;
mMultiLevelRecyclerView = multiLevelRecyclerView;
this.subredditNamePrefixed = subredditNamePrefixed;
this.article = article;
this.locale = locale;
} }
@NonNull @NonNull
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 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_post_comment, parent, false));
} }
@Override @Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) { public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
((CommentViewHolder) holder).authorTextView.setText(mCommentData.get(position).getAuthor()); final CommentData commentItem = mCommentData.get(position);
((CommentViewHolder) holder).commentTimeTextView.setText(mCommentData.get(position).getCommentTime());
((CommentViewHolder) holder).commentHtmlTextView.setHtml(mCommentData.get(position).getCommentContent());
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore()));
switch (mCommentData.get(position).getVoteType()) { ((CommentViewHolder) holder).authorTextView.setText(commentItem.getAuthor());
((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime());
((CommentViewHolder) holder).commentHtmlTextView.setHtml(commentItem.getCommentContent());
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16;
if(commentItem.hasReply()) {
setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded());
}
((CommentViewHolder) holder).expandButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) {
setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded());
mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition());
} else {
((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.VISIBLE);
FetchComment.fetchComment(subredditNamePrefixed, article, commentItem.getId(),
new FetchComment.FetchCommentListener() {
@Override
public void onFetchCommentSuccess(String response) {
ParseComment.parseComment(response, new ArrayList<CommentData>(),
locale, false, commentItem.getDepth(), new ParseComment.ParseCommentListener() {
@Override
public void onParseCommentSuccess(List<?> commentData, int moreCommentCount) {
commentItem.addChildren((List<RecyclerViewItem>) commentData);
((CommentViewHolder) holder).loadMoreCommentsProgressBar
.setVisibility(View.GONE);
mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition());
}
@Override
public void onParseCommentFail() {
((CommentViewHolder) holder).loadMoreCommentsProgressBar
.setVisibility(View.GONE);
}
});
}
@Override
public void onFetchCommentFail() {
}
});
}
}
});
switch (commentItem.getVoteType()) {
case 1: case 1:
((CommentViewHolder) holder).upvoteButton ((CommentViewHolder) holder).upvoteButton
.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); .setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
@ -59,19 +123,19 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) { if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) {
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((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(mCommentData.get(position).getScore() + 2)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2));
} else { } else {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
} }
VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
mCommentData.get(position).setVoteType(1); commentItem.setVoteType(1);
if(isDownvotedBefore) { if(isDownvotedBefore) {
mCommentData.get(position).setScore(mCommentData.get(position).getScore() + 2); commentItem.setScore(commentItem.getScore() + 2);
} else { } else {
mCommentData.get(position).setScore(mCommentData.get(position).getScore() + 1); commentItem.setScore(commentItem.getScore() + 1);
} }
} }
@ -79,30 +143,30 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).downvoteButton.setColorFilter(minusButtonColorFilter); ((CommentViewHolder) holder).downvoteButton.setColorFilter(minusButtonColorFilter);
} }
}, mCommentData.get(position).getFullName(), RedditUtils.DIR_UPVOTE, ((CommentViewHolder) holder).getAdapterPosition(), 1); }, commentItem.getFullName(), RedditUtils.DIR_UPVOTE, ((CommentViewHolder) holder).getAdapterPosition(), 1);
} else { } else {
//Upvoted before //Upvoted before
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() - 1)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1));
VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
mCommentData.get(position).setVoteType(0); commentItem.setVoteType(0);
mCommentData.get(position).setScore(mCommentData.get(position).getScore() - 1); commentItem.setScore(commentItem.getScore() - 1);
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
mCommentData.get(position).setScore(mCommentData.get(position).getScore() + 1); commentItem.setScore(commentItem.getScore() + 1);
} }
}, mCommentData.get(position).getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition(), 1); }, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition(), 1);
} }
} }
}); });
@ -118,19 +182,19 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
if (((CommentViewHolder) holder).downvoteButton.getColorFilter() == null) { if (((CommentViewHolder) holder).downvoteButton.getColorFilter() == null) {
((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
if (isUpvotedBefore) { if (isUpvotedBefore) {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() - 2)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 2));
} else { } else {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() - 1)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1));
} }
VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
mCommentData.get(position).setVoteType(-1); commentItem.setVoteType(-1);
if(isUpvotedBefore) { if(isUpvotedBefore) {
mCommentData.get(position).setScore(mCommentData.get(position).getScore() - 2); commentItem.setScore(commentItem.getScore() - 2);
} else { } else {
mCommentData.get(position).setScore(mCommentData.get(position).getScore() - 1); commentItem.setScore(commentItem.getScore() - 1);
} }
} }
@ -138,38 +202,45 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).downvoteButton.clearColorFilter(); ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); ((CommentViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
} }
}, mCommentData.get(position).getFullName(), RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition(), 1); }, commentItem.getFullName(), RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition(), 1);
} else { } else {
//Down voted before //Down voted before
((CommentViewHolder) holder).downvoteButton.clearColorFilter(); ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore() + 1)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() { VoteThing.voteThing(mContext, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position) {
mCommentData.get(position).setVoteType(0); commentItem.setVoteType(0);
mCommentData.get(position).setScore(mCommentData.get(position).getScore()); commentItem.setScore(commentItem.getScore());
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(mCommentData.get(position).getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
mCommentData.get(position).setScore(mCommentData.get(position).getScore()); commentItem.setScore(commentItem.getScore());
} }
}, mCommentData.get(position).getFullName(), RedditUtils.DIR_UNVOTE, holder.getAdapterPosition(), 1); }, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, holder.getAdapterPosition(), 1);
} }
} }
}); });
} }
@Override @Override
public int getItemCount() { public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
return mCommentData.size(); ((CommentViewHolder) holder).expandButton.setVisibility(View.GONE);
((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.GONE);
}
private void setExpandButton(ImageView expandButton, boolean isExpanded) {
// set the icon based on the current state
expandButton.setVisibility(View.VISIBLE);
expandButton.setImageResource(isExpanded ? R.drawable.ic_expand_less_black_20dp : R.drawable.ic_expand_more_black_20dp);
} }
private class CommentViewHolder extends RecyclerView.ViewHolder { private class CommentViewHolder extends RecyclerView.ViewHolder {
@ -179,7 +250,10 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
private ImageView upvoteButton; private ImageView upvoteButton;
private ImageView downvoteButton; private ImageView downvoteButton;
private TextView scoreTextView; private TextView scoreTextView;
private ImageView expandButton;
private ProgressBar loadMoreCommentsProgressBar;
private ImageView replyButton; private ImageView replyButton;
private View verticalBlock;
public CommentViewHolder(View itemView) { public CommentViewHolder(View itemView) {
super(itemView); super(itemView);
@ -189,7 +263,10 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
upvoteButton = itemView.findViewById(R.id.plus_button_item_post_comment); upvoteButton = itemView.findViewById(R.id.plus_button_item_post_comment);
downvoteButton = itemView.findViewById(R.id.minus_button_item_post_comment); downvoteButton = itemView.findViewById(R.id.minus_button_item_post_comment);
scoreTextView = itemView.findViewById(R.id.score_text_view_item_post_comment); scoreTextView = itemView.findViewById(R.id.score_text_view_item_post_comment);
loadMoreCommentsProgressBar = itemView.findViewById(R.id.load_more_comments_progress_bar);
expandButton = itemView.findViewById(R.id.expand_button_item_post_comment);
replyButton = itemView.findViewById(R.id.reply_button_item_post_comment); replyButton = itemView.findViewById(R.id.reply_button_item_post_comment);
verticalBlock = itemView.findViewById(R.id.vertical_block_item_post_comment);
} }
} }
} }

View File

@ -15,7 +15,8 @@ class FetchComment {
void onFetchCommentFail(); void onFetchCommentFail();
} }
static void queryComment(String subredditNamePrefixed, String article, final FetchCommentListener fetchCommentListener) { static void fetchComment(String subredditNamePrefixed, String article, String comment,
final FetchCommentListener fetchCommentListener) {
Retrofit retrofit = new Retrofit.Builder() Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RedditUtils.API_BASE_URI) .baseUrl(RedditUtils.API_BASE_URI)
.addConverterFactory(ScalarsConverterFactory.create()) .addConverterFactory(ScalarsConverterFactory.create())
@ -23,7 +24,7 @@ class FetchComment {
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> comments = api.getComments(subredditNamePrefixed, article); Call<String> comments = api.getComments(subredditNamePrefixed, article, comment);
comments.enqueue(new Callback<String>() { comments.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {

View File

@ -10,16 +10,18 @@ import org.json.JSONObject;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.Locale; import java.util.Locale;
class ParseComment { class ParseComment {
interface ParseCommentListener { interface ParseCommentListener {
void onParseCommentSuccess(ArrayList<CommentData> commentData, int moreCommentCount); void onParseCommentSuccess(List<?> commentData, int moreCommentCount);
void onParseCommentFail(); void onParseCommentFail();
} }
static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale, ParseCommentListener parseCommentListener) { static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale,
new ParseCommentAsyncTask(response, commentData, locale, parseCommentListener).execute(); boolean isPost, int parentDepth, ParseCommentListener parseCommentListener) {
new ParseCommentAsyncTask(response, commentData, locale, isPost, parentDepth, parseCommentListener).execute();
} }
private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> { private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> {
@ -27,16 +29,21 @@ class ParseComment {
private ArrayList<CommentData> commentData; private ArrayList<CommentData> commentData;
private ArrayList<CommentData> newcommentData; private ArrayList<CommentData> newcommentData;
private Locale locale; private Locale locale;
private boolean isPost;
private int parentDepth;
private ParseCommentListener parseCommentListener; private ParseCommentListener parseCommentListener;
private boolean parseFailed; private boolean parseFailed;
private int moreCommentCount; private int moreCommentCount;
ParseCommentAsyncTask(String response, ArrayList<CommentData> commentData, Locale locale, ParseCommentListener parseCommentListener){ ParseCommentAsyncTask(String response, ArrayList<CommentData> commentData, Locale locale,
boolean isPost, int parentDepth, ParseCommentListener parseCommentListener){
try { try {
jsonResponse = new JSONArray(response); jsonResponse = new JSONArray(response);
this.commentData = commentData; this.commentData = commentData;
newcommentData = new ArrayList<>(); newcommentData = new ArrayList<>();
this.locale = locale; this.locale = locale;
this.isPost = isPost;
this.parentDepth = parentDepth;
this.parseCommentListener = parseCommentListener; this.parseCommentListener = parseCommentListener;
parseFailed = false; parseFailed = false;
} catch (JSONException e) { } catch (JSONException e) {
@ -50,8 +57,15 @@ class ParseComment {
try { try {
moreCommentCount = 0; moreCommentCount = 0;
int actualCommentLength; int actualCommentLength;
JSONArray allComments;
JSONArray allComments = jsonResponse.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); if(isPost) {
allComments = jsonResponse.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
} else {
allComments = jsonResponse.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);
}
if(allComments.length() == 0) { if(allComments.length() == 0) {
return null; return null;
} }
@ -68,6 +82,7 @@ class ParseComment {
for (int i = 0; i < actualCommentLength; i++) { for (int i = 0; i < actualCommentLength; i++) {
JSONObject data = allComments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); JSONObject data = allComments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String id = data.getString(JSONUtils.ID_KEY);
String fullName = data.getString(JSONUtils.LINK_ID_KEY); String fullName = data.getString(JSONUtils.LINK_ID_KEY);
String author = data.getString(JSONUtils.AUTHOR_KEY); String author = data.getString(JSONUtils.AUTHOR_KEY);
boolean isSubmitter = data.getBoolean(JSONUtils.IS_SUBMITTER_KEY); boolean isSubmitter = data.getBoolean(JSONUtils.IS_SUBMITTER_KEY);
@ -85,11 +100,11 @@ class ParseComment {
String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", String formattedSubmitTime = new SimpleDateFormat("MMM d, YYYY, HH:mm",
locale).format(submitTimeCalendar.getTime()); locale).format(submitTimeCalendar.getTime());
int depth = data.getInt(JSONUtils.DEPTH_KEY); int depth = data.getInt(JSONUtils.DEPTH_KEY) + parentDepth;
boolean collapsed = data.getBoolean(JSONUtils.COLLAPSED_KEY); boolean collapsed = data.getBoolean(JSONUtils.COLLAPSED_KEY);
boolean hasReply = !(data.get(JSONUtils.REPLIES_KEY) instanceof String); boolean hasReply = !(data.get(JSONUtils.REPLIES_KEY) instanceof String);
newcommentData.add(new CommentData(fullName, author, formattedSubmitTime, commentContent, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden)); newcommentData.add(new CommentData(id, fullName, author, formattedSubmitTime, commentContent, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden));
} }
} catch (JSONException e) { } catch (JSONException e) {
parseFailed = true; parseFailed = true;

View File

@ -17,7 +17,8 @@ public interface RedditAPI {
Call<String> getAccessToken(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); Call<String> getAccessToken(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1") @GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1")
Call<String> getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Path("article") String article); Call<String> getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed,
@Path("article") String article, @Query("comment") String comment);
@GET("r/{subredditName}/about.json?raw_json=1") @GET("r/{subredditName}/about.json?raw_json=1")
Call<String> getSubredditData(@Path("subredditName") String subredditName); Call<String> getSubredditData(@Path("subredditName") String subredditName);

View File

@ -14,7 +14,6 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView; import android.support.v7.widget.CardView;
import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
@ -31,10 +30,12 @@ import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.Target;
import com.multilevelview.MultiLevelRecyclerView;
import org.sufficientlysecure.htmltextview.HtmlTextView; import org.sufficientlysecure.htmltextview.HtmlTextView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView; import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
@ -53,7 +54,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
private CoordinatorLayout mCoordinatorLayout; private CoordinatorLayout mCoordinatorLayout;
private ProgressBar mCommentProgressbar; private ProgressBar mCommentProgressbar;
private CardView mCommentCardView; private CardView mCommentCardView;
private RecyclerView mRecyclerView; private MultiLevelRecyclerView mRecyclerView;
private LinearLayout mNoCommentWrapperLinearLayout; private LinearLayout mNoCommentWrapperLinearLayout;
private ImageView mNoCommentImageView; private ImageView mNoCommentImageView;
@ -454,19 +455,25 @@ public class ViewPostDetailActivity extends AppCompatActivity {
private void queryComment() { private void queryComment() {
mCommentProgressbar.setVisibility(View.VISIBLE); mCommentProgressbar.setVisibility(View.VISIBLE);
mNoCommentWrapperLinearLayout.setVisibility(View.GONE); mNoCommentWrapperLinearLayout.setVisibility(View.GONE);
FetchComment.queryComment(mPostData.getSubredditNamePrefixed(), mPostData.getId(), FetchComment.fetchComment(mPostData.getSubredditNamePrefixed(), mPostData.getId(),
new FetchComment.FetchCommentListener() { null, new FetchComment.FetchCommentListener() {
@Override @Override
public void onFetchCommentSuccess(String response) { public void onFetchCommentSuccess(String response) {
ParseComment.parseComment(response, new ArrayList<CommentData>(), ParseComment.parseComment(response, new ArrayList<CommentData>(),
getResources().getConfiguration().locale, new ParseComment.ParseCommentListener() { getResources().getConfiguration().locale, true, 0,
new ParseComment.ParseCommentListener() {
@Override @Override
public void onParseCommentSuccess(ArrayList<CommentData> commentData, int moreCommentCount) { public void onParseCommentSuccess(List<?> commentData, int moreCommentCount) {
mCommentProgressbar.setVisibility(View.GONE); mCommentProgressbar.setVisibility(View.GONE);
mMoreCommentCount = moreCommentCount; mMoreCommentCount = moreCommentCount;
if (commentData.size() > 0) { if (commentData.size() > 0) {
CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter( CommentMultiLevelRecyclerViewAdapter adapter = new CommentMultiLevelRecyclerViewAdapter(
ViewPostDetailActivity.this, commentData); ViewPostDetailActivity.this, (ArrayList<CommentData>) commentData,
mRecyclerView, mPostData.getSubredditNamePrefixed(),
mPostData.getId(), getResources().getConfiguration().locale);
mRecyclerView.removeItemClickListeners();
mRecyclerView.setToggleItemOnClick(false);
mRecyclerView.setAccordion(false);
mRecyclerView.setAdapter(adapter); mRecyclerView.setAdapter(adapter);
mCommentCardView.setVisibility(View.VISIBLE); mCommentCardView.setVisibility(View.VISIBLE);
} else { } else {

View File

@ -1,4 +1,4 @@
<vector android:height="12dp" android:viewportHeight="24.0" <vector android:height="20dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="12dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:viewportWidth="24.0" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#010101" android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"/> <path android:fillColor="#010101" android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"/>
</vector> </vector>

View File

@ -1,4 +1,4 @@
<vector android:height="12dp" android:viewportHeight="24.0" <vector android:height="20dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="12dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:viewportWidth="24.0" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M4,12l1.41,1.41L11,7.83V20h2V7.83l5.58,5.59L20,12l-8,-8 -8,8z"/> <path android:fillColor="#FF000000" android:pathData="M4,12l1.41,1.41L11,7.83V20h2V7.83l5.58,5.59L20,12l-8,-8 -8,8z"/>
</vector> </vector>

View File

@ -0,0 +1,4 @@
<vector android:height="20dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,8l-6,6 1.41,1.41L12,10.83l4.59,4.58L18,14z"/>
</vector>

View File

@ -0,0 +1,4 @@
<vector android:height="20dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M16.59,8.59L12,13.17 7.41,8.59 6,10l6,6 6,-6z"/>
</vector>

View File

@ -1,4 +1,4 @@
<vector android:height="12dp" android:viewportHeight="24.0" <vector android:height="20dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="12dp" xmlns:android="http://schemas.android.com/apk/res/android"> android:viewportWidth="24.0" android:width="20dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M10,9V5l-7,7 7,7v-4.1c5,0 8.5,1.6 11,5.1 -1,-5 -4,-10 -11,-11z"/> <path android:fillColor="#FF000000" android:pathData="M10,9V5l-7,7 7,7v-4.1c5,0 8.5,1.6 11,5.1 -1,-5 -4,-10 -11,-11z"/>
</vector> </vector>

View File

@ -258,7 +258,12 @@
android:textColor="#000000" android:textColor="#000000"
android:textSize="18sp" /> android:textSize="18sp" />
<android.support.v7.widget.RecyclerView <!--<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_view_post_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />-->
<com.multilevelview.MultiLevelRecyclerView
android:id="@+id/recycler_view_view_post_detail" android:id="@+id/recycler_view_view_post_detail"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />

View File

@ -2,23 +2,32 @@
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/linear_layout_item_post_comment" android:id="@+id/linear_layout_item_post_comment">
android:orientation="vertical"
android:layout_marginTop="12dp" <View
android:layout_marginBottom="8dp"> android:id="@+id/vertical_block_item_post_comment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/colorPrimary"/>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">
<TextView <TextView
android:id="@+id/author_text_view_item_post_comment" android:id="@+id/author_text_view_item_post_comment"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:textColor="@color/colorPrimary"/> android:textColor="@color/colorPrimary"/>
@ -27,8 +36,7 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:gravity="end" android:gravity="end" />
android:layout_marginEnd="16dp" />
</LinearLayout> </LinearLayout>
@ -36,29 +44,25 @@
android:id="@+id/comment_html_text_view_item_post_comment" android:id="@+id/comment_html_text_view_item_post_comment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="6dp" android:layout_marginTop="8dp"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" android:layout_marginStart="32dp"
android:layout_marginRight="32dp"
android:layout_marginEnd="32dp" android:layout_marginEnd="32dp"
android:layout_marginBottom="8dp"
android:textAppearance="@android:style/TextAppearance.Small"/> android:textAppearance="@android:style/TextAppearance.Small"/>
<RelativeLayout <RelativeLayout
android:id="@+id/relative_layout_item_post_comment" android:id="@+id/relative_layout_item_post_comment"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="8dp" android:layout_marginStart="16dp"
android:layout_marginStart="8dp" android:layout_marginEnd="16dp">
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp">
<ImageView <ImageView
android:id="@+id/plus_button_item_post_comment" android:id="@+id/plus_button_item_post_comment"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:padding="8dp" android:src="@drawable/ic_arrow_upward_black_20dp"
android:src="@drawable/ic_arrow_upward_black_12dp"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:tint="@android:color/tab_indicator_text" android:tint="@android:color/tab_indicator_text"
android:background="?actionBarItemBackground" android:background="?actionBarItemBackground"
@ -79,22 +83,40 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_toEndOf="@id/score_text_view_item_post_comment" android:layout_toEndOf="@id/score_text_view_item_post_comment"
android:padding="8dp" android:src="@drawable/ic_arrow_downward_black_20dp"
android:src="@drawable/ic_arrow_downward_black_12dp"
android:tint="@android:color/tab_indicator_text" android:tint="@android:color/tab_indicator_text"
android:background="?actionBarItemBackground" android:background="?actionBarItemBackground"
android:clickable="true" android:clickable="true"
android:focusable="true"/> android:focusable="true"/>
<ProgressBar
android:id="@+id/load_more_comments_progress_bar"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginEnd="16dp"
android:layout_toStartOf="@id/expand_button_item_post_comment"
android:visibility="gone"/>
<ImageView
android:id="@+id/expand_button_item_post_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@id/reply_button_item_post_comment"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:tint="@android:color/tab_indicator_text"
android:background="?actionBarItemBackground"
android:clickable="true"
android:focusable="true"
android:visibility="gone"/>
<ImageView <ImageView
android:id="@+id/reply_button_item_post_comment" android:id="@+id/reply_button_item_post_comment"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:padding="8dp"
android:layout_marginEnd="16dp"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:src="@drawable/ic_reply_black_12dp" android:src="@drawable/ic_reply_black_20dp"
android:tint="@android:color/tab_indicator_text" android:tint="@android:color/tab_indicator_text"
android:background="?actionBarItemBackground" android:background="?actionBarItemBackground"
android:clickable="true" android:clickable="true"
@ -102,4 +124,6 @@
</RelativeLayout> </RelativeLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>