Fixed adding new comments to the wrong position. Expand all children of comments after parsing. Delete useless parameters of some methods.

This commit is contained in:
Alex Ning 2019-06-22 22:20:37 +08:00
parent 724d3181e2
commit 9ddf15b8fb
5 changed files with 61 additions and 138 deletions

View File

@ -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<RecyclerView.ViewH
private Retrofit mRetrofit;
private Retrofit mOauthRetrofit;
private SharedPreferences mSharedPreferences;
private ArrayList<CommentData> mCommentData;
private RecyclerView mRecyclerView;
private String mSubredditNamePrefixed;
private String mArticle;
private Locale mLocale;
private ArrayList<CommentData> mVisibleComments;
CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit,
SharedPreferences sharedPreferences, ArrayList<CommentData> commentData,
RecyclerView recyclerView,
String subredditNamePrefixed, String article, Locale locale) {
SharedPreferences sharedPreferences, ArrayList<CommentData> 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<CommentData> comments, ArrayList<CommentData> 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<RecyclerView.ViewH
parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale,
new FetchComment.FetchMoreCommentListener() {
@Override
public void onFetchMoreCommentSuccess(ArrayList<CommentData> commentsData, int childrenStartingIndex) {
public void onFetchMoreCommentSuccess(ArrayList<CommentData> 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<RecyclerView.ViewH
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
mVisibleComments.addAll(parentPosition
+ mVisibleComments.get(parentPosition).getChildren().size(),
commentsData);
notifyItemRangeInserted(parentPosition + mVisibleComments.get(parentPosition).getChildren().size(),
commentsData.size());
mVisibleComments.addAll(position, expandedComments);
notifyItemRangeInserted(position, expandedComments.size());
} else {
mVisibleComments.get(parentPosition).getChildren()
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
@ -358,15 +328,12 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
notifyItemRemoved(position);
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
mVisibleComments.addAll(parentPosition
+ mVisibleComments.get(parentPosition).getChildren().size() + 1,
commentsData);
notifyItemRangeInserted(parentPosition + mVisibleComments.get(parentPosition).getChildren().size() + 1,
commentsData.size());
mVisibleComments.addAll(position, expandedComments);
notifyItemRangeInserted(position, expandedComments.size());
}
}
mVisibleComments.get(parentPosition).addChildren(commentsData);
mVisibleComments.get(parentPosition).addChildren(expandedComments);
} else {
for(int i = 0; i < mVisibleComments.size(); i++) {
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
@ -378,16 +345,16 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
notifyItemChanged(i + mVisibleComments.get(i).getChildren().size());
mVisibleComments.addAll(i + mVisibleComments.get(i).getChildren().size(),
commentsData);
expandedComments);
notifyItemRangeInserted(i + mVisibleComments.get(i).getChildren().size(),
commentsData.size());
expandedComments.size());
}
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadingMoreChildren(false);
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
.setLoadMoreChildrenFailed(false);
mVisibleComments.get(i).addChildren(commentsData);
mVisibleComments.get(i).addChildren(expandedComments);
break;
}
@ -445,10 +412,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
comment.setExpanded(true);
ArrayList<CommentData> 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<RecyclerView.ViewH
}
void addComment(CommentData comment) {
mCommentData.add(0, comment);
mVisibleComments.add(0, comment);
notifyItemInserted(0);
}
@ -508,7 +470,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
}
void clearData() {
mCommentData.clear();
mVisibleComments.clear();
notifyDataSetChanged();
}

View File

@ -14,32 +14,29 @@ import retrofit2.Retrofit;
class FetchComment {
interface FetchCommentListener {
void onFetchCommentSuccess(ArrayList<CommentData> commentsData,
String parentId, ArrayList<String> children);
void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, String parentId, ArrayList<String> children);
void onFetchCommentFailed();
}
interface FetchMoreCommentListener {
void onFetchMoreCommentSuccess(ArrayList<CommentData> commentsData, int childrenStartingIndex);
void onFetchMoreCommentSuccess(ArrayList<CommentData> 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<String> comments = api.getComments(subredditNamePrefixed, article, comment);
Call<String> comments = api.getComments(subredditNamePrefixed, article);
comments.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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> commentData,
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> 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> commentData, String parentId,
ArrayList<String> moreChildrenFullnames) {
fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData, startingIndex + 100);
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> moreChildrenFullnames) {
fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments,
startingIndex + 100);
}
@Override

View File

@ -16,35 +16,25 @@ import java.util.Locale;
class ParseComment {
interface ParseCommentListener {
void onParseCommentSuccess(ArrayList<CommentData> commentData, String parentId, ArrayList<String> moreChildrenFullnames);
void onParseCommentSuccess(ArrayList<CommentData> expandedComments, String parentId,
ArrayList<String> 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> 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<Void, Void, Void> {
private JSONArray comments;
private ArrayList<CommentData> commentData;
private ArrayList<CommentData> newcommentData;
private JSONArray commentsJSONArray;
private ArrayList<CommentData> comments;
private ArrayList<CommentData> newComments;
private ArrayList<CommentData> expandedNewComments;
private ArrayList<String> moreChildrenFullnames;
private Locale locale;
private String parentId;
@ -84,11 +75,12 @@ class ParseComment {
private ParseCommentListener parseCommentListener;
private boolean parseFailed;
ParseCommentAsyncTask(JSONArray comments, ArrayList<CommentData> commentData, Locale locale,
ParseCommentAsyncTask(JSONArray commentsJSONArray, ArrayList<CommentData> 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<Void, Void, Void> {
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<CommentData> comments, ArrayList<CommentData> 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);

View File

@ -18,7 +18,7 @@ public interface RedditAPI {
@GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1")
Call<String> 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<String> getSubredditData(@Path("subredditName") String subredditName);

View File

@ -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<CommentData> commentsData,
public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> 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<CommentData> commentsData, int childrenStartingIndex) {
mAdapter.addComments(commentsData);
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex) {
mAdapter.addComments(expandedComments);
mChildrenStartingIndex = childrenStartingIndex;
isLoadingMoreChildren = false;
}