From 9ddf15b8fb20bb99ed94a2f2cb5e0a50780961d5 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 22 Jun 2019 22:20:37 +0800 Subject: Fixed adding new comments to the wrong position. Expand all children of comments after parsing. Delete useless parameters of some methods. --- .../CommentRecyclerViewAdapter.java | 65 +++------------ .../infinityforreddit/FetchComment.java | 24 +++--- .../infinityforreddit/ParseComment.java | 94 +++++++--------------- .../infinityforreddit/RedditAPI.java | 2 +- .../infinityforreddit/ViewPostDetailActivity.java | 14 ++-- 5 files changed, 61 insertions(+), 138 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java index f845bec0..5b704cdb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java @@ -5,7 +5,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.graphics.ColorFilter; import android.net.Uri; -import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -38,48 +37,21 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter mCommentData; - private RecyclerView mRecyclerView; private String mSubredditNamePrefixed; - private String mArticle; private Locale mLocale; private ArrayList mVisibleComments; CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, - SharedPreferences sharedPreferences, ArrayList commentData, - RecyclerView recyclerView, - String subredditNamePrefixed, String article, Locale locale) { + SharedPreferences sharedPreferences, ArrayList expandedComments, + String subredditNamePrefixed, Locale locale) { mActivity = activity; mRetrofit = retrofit; mOauthRetrofit = oauthRetrofit; mSharedPreferences = sharedPreferences; - mCommentData = commentData; - mRecyclerView = recyclerView; mSubredditNamePrefixed = subredditNamePrefixed; - mArticle = article; mLocale = locale; - mVisibleComments = new ArrayList<>(); - - new Handler().post(() -> { - makeChildrenVisible(commentData, mVisibleComments); - notifyDataSetChanged(); - }); - } - - private void makeChildrenVisible(ArrayList comments, ArrayList visibleComments) { - for(CommentData c : comments) { - visibleComments.add(c); - if(c.hasReply()) { - c.setExpanded(true); - makeChildrenVisible(c.getChildren(), visibleComments); - } - if(c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) { - //Add a load more placeholder - visibleComments.add(new CommentData(c.getDepth() + 1)); - c.addChild(new CommentData(c.getDepth() + 1), c.getChildren().size()); - } - } + mVisibleComments = expandedComments; } @Override @@ -331,7 +303,8 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter commentsData, int childrenStartingIndex) { + public void onFetchMoreCommentSuccess(ArrayList expandedComments, + int childrenStartingIndex) { if(mVisibleComments.size() > parentPosition && parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) { if(mVisibleComments.get(parentPosition).isExpanded()) { @@ -346,11 +319,8 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter children = comment.getChildren(); if(children != null && children.size() > 0) { - /*if(comment.hasMoreChildrenFullnames() && comment.getMoreChildrenFullnames().size() > comment.getMoreChildrenStartingIndex() - && !children.get(children.size() - 1).isPlaceHolder()) { - children.add(children.size(), new CommentData(children.get(0).getDepth())); - }*/ mVisibleComments.addAll(position + 1, children); for(int i = position + 1; i <= position + children.size(); i++) { mVisibleComments.get(i).setExpanded(false); @@ -481,7 +444,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter commentsData, - String parentId, ArrayList children); + void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children); void onFetchCommentFailed(); } interface FetchMoreCommentListener { - void onFetchMoreCommentSuccess(ArrayList commentsData, int childrenStartingIndex); + void onFetchMoreCommentSuccess(ArrayList expandedComments, int childrenStartingIndex); void onFetchMoreCommentFailed(); } static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article, - String comment, Locale locale, boolean isPost, int depth, - final FetchCommentListener fetchCommentListener) { + Locale locale, FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call comments = api.getComments(subredditNamePrefixed, article, comment); + Call comments = api.getComments(subredditNamePrefixed, article); comments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { ParseComment.parseComment(response.body(), new ArrayList<>(), - locale, isPost, depth, - new ParseComment.ParseCommentListener() { + locale, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList commentData, + public void onParseCommentSuccess(ArrayList expandedComments, String parentId, ArrayList moreChildrenFullnames) { - fetchCommentListener.onFetchCommentSuccess(commentData, parentId, + fetchCommentListener.onFetchCommentSuccess(expandedComments, parentId, moreChildrenFullnames); } @@ -90,9 +87,10 @@ class FetchComment { ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale, depth, new ParseComment.ParseCommentListener() { @Override - public void onParseCommentSuccess(ArrayList commentData, String parentId, - ArrayList moreChildrenFullnames) { - fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData, startingIndex + 100); + public void onParseCommentSuccess(ArrayList expandedComments, + String parentId, ArrayList moreChildrenFullnames) { + fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments, + startingIndex + 100); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index d6ff6697..61ca80a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -16,35 +16,25 @@ import java.util.Locale; class ParseComment { interface ParseCommentListener { - void onParseCommentSuccess(ArrayList commentData, String parentId, ArrayList moreChildrenFullnames); + void onParseCommentSuccess(ArrayList expandedComments, String parentId, + ArrayList moreChildrenFullnames); void onParseCommentFailed(); } - interface ParseMoreCommentBasicInfoListener { - void onParseMoreCommentBasicInfoSuccess(String commaSeparatedChildrenId); - void onParseMoreCommentBasicInfoFailed(); - } - interface ParseSentCommentListener { void onParseSentCommentSuccess(CommentData commentData); void onParseSentCommentFailed(); } static void parseComment(String response, ArrayList commentData, Locale locale, - boolean isPost, int depth, ParseCommentListener parseCommentListener) { + ParseCommentListener parseCommentListener) { try { JSONArray childrenArray = new JSONArray(response); String parentId = childrenArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY) .getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_KEY); - if(isPost) { - childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - } else { - 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); - } + childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, depth, parseCommentListener).execute(); + new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, 0, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); if(e.getMessage() != null) { @@ -74,9 +64,10 @@ class ParseComment { } private static class ParseCommentAsyncTask extends AsyncTask { - private JSONArray comments; - private ArrayList commentData; - private ArrayList newcommentData; + private JSONArray commentsJSONArray; + private ArrayList comments; + private ArrayList newComments; + private ArrayList expandedNewComments; private ArrayList moreChildrenFullnames; private Locale locale; private String parentId; @@ -84,11 +75,12 @@ class ParseComment { private ParseCommentListener parseCommentListener; private boolean parseFailed; - ParseCommentAsyncTask(JSONArray comments, ArrayList commentData, Locale locale, + ParseCommentAsyncTask(JSONArray commentsJSONArray, ArrayList comments, Locale locale, @Nullable String parentId, int depth, ParseCommentListener parseCommentListener){ + this.commentsJSONArray = commentsJSONArray; this.comments = comments; - this.commentData = commentData; - newcommentData = new ArrayList<>(); + newComments = new ArrayList<>(); + expandedNewComments = new ArrayList<>(); moreChildrenFullnames = new ArrayList<>(); this.locale = locale; this.parentId = parentId; @@ -100,7 +92,8 @@ class ParseComment { @Override protected Void doInBackground(Void... voids) { try { - parseCommentRecursion(comments, newcommentData, moreChildrenFullnames, depth, locale); + parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale); + makeChildrenVisible(newComments, expandedNewComments); } catch (JSONException e) { parseFailed = true; if(e.getMessage() != null) { @@ -113,8 +106,8 @@ class ParseComment { @Override protected void onPostExecute(Void aVoid) { if(!parseFailed) { - commentData.addAll(newcommentData); - parseCommentListener.onParseCommentSuccess(commentData, parentId, moreChildrenFullnames); + comments.addAll(expandedNewComments); + parseCommentListener.onParseCommentSuccess(comments, parentId, moreChildrenFullnames); } else { parseCommentListener.onParseCommentFailed(); } @@ -131,7 +124,7 @@ class ParseComment { JSONObject more = comments.getJSONObject(comments.length() - 1).getJSONObject(JSONUtils.DATA_KEY); - //Maybe moreChildrenFullnames contain only comments and no more info + //Maybe moreChildrenFullnames contain only commentsJSONArray and no more info if(more.has(JSONUtils.COUNT_KEY)) { JSONArray childrenArray = more.getJSONArray(JSONUtils.CHILDREN_KEY); @@ -163,47 +156,17 @@ class ParseComment { } } - private static class ParseMoreCommentBasicInfoAsyncTask extends AsyncTask { - private JSONArray children; - private StringBuilder commaSeparatedChildren; - private ParseMoreCommentBasicInfoListener parseMoreCommentBasicInfoListener; - private boolean parseFailed; - - ParseMoreCommentBasicInfoAsyncTask(String response, ParseMoreCommentBasicInfoListener parseMoreCommentBasicInfoListener) { - this.parseMoreCommentBasicInfoListener = parseMoreCommentBasicInfoListener; - try { - children = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY) - .getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.THINGS_KEY); - commaSeparatedChildren = new StringBuilder(); - } catch (JSONException e) { - parseMoreCommentBasicInfoListener.onParseMoreCommentBasicInfoFailed(); - e.printStackTrace(); + private static void makeChildrenVisible(ArrayList comments, ArrayList visibleComments) { + for(CommentData c : comments) { + visibleComments.add(c); + if(c.hasReply()) { + c.setExpanded(true); + makeChildrenVisible(c.getChildren(), visibleComments); } - } - - @Override - protected Void doInBackground(Void... voids) { - try { - for(int i = 0; i < children.length(); i++) { - commaSeparatedChildren.append(children.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.ID_KEY)); - commaSeparatedChildren.append(","); - } - commaSeparatedChildren.deleteCharAt(commaSeparatedChildren.length() - 1); - parseFailed = false; - } catch (JSONException e) { - parseFailed = true; - e.printStackTrace(); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - if(!parseFailed) { - parseMoreCommentBasicInfoListener.onParseMoreCommentBasicInfoSuccess(commaSeparatedChildren.toString()); - } else { - parseMoreCommentBasicInfoListener.onParseMoreCommentBasicInfoFailed(); + if(c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) { + //Add a load more placeholder + visibleComments.add(new CommentData(c.getDepth() + 1)); + c.addChild(new CommentData(c.getDepth() + 1), c.getChildren().size()); } } } @@ -246,6 +209,7 @@ class ParseComment { } } } + private static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException { String id = singleCommentData.getString(JSONUtils.ID_KEY); String fullName = singleCommentData.getString(JSONUtils.NAME_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index bced4f3f..72f8c4a3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -18,7 +18,7 @@ public interface RedditAPI { @GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1") Call getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed, - @Path("article") String article, @Query("comment") String comment); + @Path("article") String article); @GET("r/{subredditName}/about.json?raw_json=1") Call getSubredditData(@Path("subredditName") String subredditName); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 94480f98..8ab0b3f5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -498,14 +498,14 @@ public class ViewPostDetailActivity extends AppCompatActivity { mNoCommentWrapperLinearLayout.setVisibility(View.GONE); FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), - null, mLocale, true, 0, new FetchComment.FetchCommentListener() { + mLocale, new FetchComment.FetchCommentListener() { @Override - public void onFetchCommentSuccess(ArrayList commentsData, + public void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children) { ViewPostDetailActivity.this.children = children; mCommentProgressbar.setVisibility(View.GONE); - if (commentsData.size() > 0) { + if (expandedComments.size() > 0) { if(mAdapter == null) { mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> { if(!isLoadingMoreChildren) { @@ -520,8 +520,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { } mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, - mOauthRetrofit, mSharedPreferences, commentsData, mRecyclerView, - mPost.getSubredditNamePrefixed(), mPost.getId(), mLocale); + mOauthRetrofit, mSharedPreferences, expandedComments, + mPost.getSubredditNamePrefixed(), mLocale); mRecyclerView.setAdapter(mAdapter); mCommentCardView.setVisibility(View.VISIBLE); @@ -544,8 +544,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), children, startingIndex, 0, mLocale, new FetchComment.FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(ArrayList commentsData, int childrenStartingIndex) { - mAdapter.addComments(commentsData); + public void onFetchMoreCommentSuccess(ArrayList expandedComments, int childrenStartingIndex) { + mAdapter.addComments(expandedComments); mChildrenStartingIndex = childrenStartingIndex; isLoadingMoreChildren = false; } -- cgit v1.2.3