mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-24 00:44:43 +01:00
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:
parent
724d3181e2
commit
9ddf15b8fb
@ -5,7 +5,6 @@ import android.content.Intent;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.graphics.ColorFilter;
|
import android.graphics.ColorFilter;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Handler;
|
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@ -38,48 +37,21 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
private Retrofit mRetrofit;
|
private Retrofit mRetrofit;
|
||||||
private Retrofit mOauthRetrofit;
|
private Retrofit mOauthRetrofit;
|
||||||
private SharedPreferences mSharedPreferences;
|
private SharedPreferences mSharedPreferences;
|
||||||
private ArrayList<CommentData> mCommentData;
|
|
||||||
private RecyclerView mRecyclerView;
|
|
||||||
private String mSubredditNamePrefixed;
|
private String mSubredditNamePrefixed;
|
||||||
private String mArticle;
|
|
||||||
private Locale mLocale;
|
private Locale mLocale;
|
||||||
|
|
||||||
private ArrayList<CommentData> mVisibleComments;
|
private ArrayList<CommentData> mVisibleComments;
|
||||||
|
|
||||||
CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit,
|
CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit,
|
||||||
SharedPreferences sharedPreferences, ArrayList<CommentData> commentData,
|
SharedPreferences sharedPreferences, ArrayList<CommentData> expandedComments,
|
||||||
RecyclerView recyclerView,
|
String subredditNamePrefixed, Locale locale) {
|
||||||
String subredditNamePrefixed, String article, Locale locale) {
|
|
||||||
mActivity = activity;
|
mActivity = activity;
|
||||||
mRetrofit = retrofit;
|
mRetrofit = retrofit;
|
||||||
mOauthRetrofit = oauthRetrofit;
|
mOauthRetrofit = oauthRetrofit;
|
||||||
mSharedPreferences = sharedPreferences;
|
mSharedPreferences = sharedPreferences;
|
||||||
mCommentData = commentData;
|
|
||||||
mRecyclerView = recyclerView;
|
|
||||||
mSubredditNamePrefixed = subredditNamePrefixed;
|
mSubredditNamePrefixed = subredditNamePrefixed;
|
||||||
mArticle = article;
|
|
||||||
mLocale = locale;
|
mLocale = locale;
|
||||||
mVisibleComments = new ArrayList<>();
|
mVisibleComments = expandedComments;
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -331,7 +303,8 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale,
|
parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale,
|
||||||
new FetchComment.FetchMoreCommentListener() {
|
new FetchComment.FetchMoreCommentListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchMoreCommentSuccess(ArrayList<CommentData> commentsData, int childrenStartingIndex) {
|
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments,
|
||||||
|
int childrenStartingIndex) {
|
||||||
if(mVisibleComments.size() > parentPosition
|
if(mVisibleComments.size() > parentPosition
|
||||||
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
|
&& parentComment.getFullName().equals(mVisibleComments.get(parentPosition).getFullName())) {
|
||||||
if(mVisibleComments.get(parentPosition).isExpanded()) {
|
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)
|
mVisibleComments.get(parentPosition).getChildren().get(mVisibleComments.get(parentPosition).getChildren().size() - 1)
|
||||||
.setLoadMoreChildrenFailed(false);
|
.setLoadMoreChildrenFailed(false);
|
||||||
|
|
||||||
mVisibleComments.addAll(parentPosition
|
mVisibleComments.addAll(position, expandedComments);
|
||||||
+ mVisibleComments.get(parentPosition).getChildren().size(),
|
notifyItemRangeInserted(position, expandedComments.size());
|
||||||
commentsData);
|
|
||||||
notifyItemRangeInserted(parentPosition + mVisibleComments.get(parentPosition).getChildren().size(),
|
|
||||||
commentsData.size());
|
|
||||||
} else {
|
} else {
|
||||||
mVisibleComments.get(parentPosition).getChildren()
|
mVisibleComments.get(parentPosition).getChildren()
|
||||||
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
|
.remove(mVisibleComments.get(parentPosition).getChildren().size() - 1);
|
||||||
@ -358,15 +328,12 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
notifyItemRemoved(position);
|
notifyItemRemoved(position);
|
||||||
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
|
mVisibleComments.get(parentPosition).removeMoreChildrenFullnames();
|
||||||
|
|
||||||
mVisibleComments.addAll(parentPosition
|
mVisibleComments.addAll(position, expandedComments);
|
||||||
+ mVisibleComments.get(parentPosition).getChildren().size() + 1,
|
notifyItemRangeInserted(position, expandedComments.size());
|
||||||
commentsData);
|
|
||||||
notifyItemRangeInserted(parentPosition + mVisibleComments.get(parentPosition).getChildren().size() + 1,
|
|
||||||
commentsData.size());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mVisibleComments.get(parentPosition).addChildren(commentsData);
|
mVisibleComments.get(parentPosition).addChildren(expandedComments);
|
||||||
} else {
|
} else {
|
||||||
for(int i = 0; i < mVisibleComments.size(); i++) {
|
for(int i = 0; i < mVisibleComments.size(); i++) {
|
||||||
if(mVisibleComments.get(i).getFullName().equals(parentComment.getFullName())) {
|
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());
|
notifyItemChanged(i + mVisibleComments.get(i).getChildren().size());
|
||||||
|
|
||||||
mVisibleComments.addAll(i + mVisibleComments.get(i).getChildren().size(),
|
mVisibleComments.addAll(i + mVisibleComments.get(i).getChildren().size(),
|
||||||
commentsData);
|
expandedComments);
|
||||||
notifyItemRangeInserted(i + mVisibleComments.get(i).getChildren().size(),
|
notifyItemRangeInserted(i + mVisibleComments.get(i).getChildren().size(),
|
||||||
commentsData.size());
|
expandedComments.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
|
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
|
||||||
.setLoadingMoreChildren(false);
|
.setLoadingMoreChildren(false);
|
||||||
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
|
mVisibleComments.get(i).getChildren().get(mVisibleComments.get(i).getChildren().size() - 1)
|
||||||
.setLoadMoreChildrenFailed(false);
|
.setLoadMoreChildrenFailed(false);
|
||||||
mVisibleComments.get(i).addChildren(commentsData);
|
mVisibleComments.get(i).addChildren(expandedComments);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -445,10 +412,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
comment.setExpanded(true);
|
comment.setExpanded(true);
|
||||||
ArrayList<CommentData> children = comment.getChildren();
|
ArrayList<CommentData> children = comment.getChildren();
|
||||||
if(children != null && children.size() > 0) {
|
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);
|
mVisibleComments.addAll(position + 1, children);
|
||||||
for(int i = position + 1; i <= position + children.size(); i++) {
|
for(int i = position + 1; i <= position + children.size(); i++) {
|
||||||
mVisibleComments.get(i).setExpanded(false);
|
mVisibleComments.get(i).setExpanded(false);
|
||||||
@ -481,7 +444,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addComment(CommentData comment) {
|
void addComment(CommentData comment) {
|
||||||
mCommentData.add(0, comment);
|
|
||||||
mVisibleComments.add(0, comment);
|
mVisibleComments.add(0, comment);
|
||||||
notifyItemInserted(0);
|
notifyItemInserted(0);
|
||||||
}
|
}
|
||||||
@ -508,7 +470,6 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clearData() {
|
void clearData() {
|
||||||
mCommentData.clear();
|
|
||||||
mVisibleComments.clear();
|
mVisibleComments.clear();
|
||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
|
@ -14,32 +14,29 @@ import retrofit2.Retrofit;
|
|||||||
|
|
||||||
class FetchComment {
|
class FetchComment {
|
||||||
interface FetchCommentListener {
|
interface FetchCommentListener {
|
||||||
void onFetchCommentSuccess(ArrayList<CommentData> commentsData,
|
void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, String parentId, ArrayList<String> children);
|
||||||
String parentId, ArrayList<String> children);
|
|
||||||
void onFetchCommentFailed();
|
void onFetchCommentFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FetchMoreCommentListener {
|
interface FetchMoreCommentListener {
|
||||||
void onFetchMoreCommentSuccess(ArrayList<CommentData> commentsData, int childrenStartingIndex);
|
void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex);
|
||||||
void onFetchMoreCommentFailed();
|
void onFetchMoreCommentFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article,
|
static void fetchComment(Retrofit retrofit, String subredditNamePrefixed, String article,
|
||||||
String comment, Locale locale, boolean isPost, int depth,
|
Locale locale, FetchCommentListener fetchCommentListener) {
|
||||||
final FetchCommentListener fetchCommentListener) {
|
|
||||||
RedditAPI api = retrofit.create(RedditAPI.class);
|
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>() {
|
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) {
|
||||||
if(response.isSuccessful()) {
|
if(response.isSuccessful()) {
|
||||||
ParseComment.parseComment(response.body(), new ArrayList<>(),
|
ParseComment.parseComment(response.body(), new ArrayList<>(),
|
||||||
locale, isPost, depth,
|
locale, new ParseComment.ParseCommentListener() {
|
||||||
new ParseComment.ParseCommentListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onParseCommentSuccess(ArrayList<CommentData> commentData,
|
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments,
|
||||||
String parentId, ArrayList<String> moreChildrenFullnames) {
|
String parentId, ArrayList<String> moreChildrenFullnames) {
|
||||||
fetchCommentListener.onFetchCommentSuccess(commentData, parentId,
|
fetchCommentListener.onFetchCommentSuccess(expandedComments, parentId,
|
||||||
moreChildrenFullnames);
|
moreChildrenFullnames);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,9 +87,10 @@ class FetchComment {
|
|||||||
ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale,
|
ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale,
|
||||||
depth, new ParseComment.ParseCommentListener() {
|
depth, new ParseComment.ParseCommentListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onParseCommentSuccess(ArrayList<CommentData> commentData, String parentId,
|
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments,
|
||||||
ArrayList<String> moreChildrenFullnames) {
|
String parentId, ArrayList<String> moreChildrenFullnames) {
|
||||||
fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData, startingIndex + 100);
|
fetchMoreCommentListener.onFetchMoreCommentSuccess(expandedComments,
|
||||||
|
startingIndex + 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -16,35 +16,25 @@ import java.util.Locale;
|
|||||||
|
|
||||||
class ParseComment {
|
class ParseComment {
|
||||||
interface ParseCommentListener {
|
interface ParseCommentListener {
|
||||||
void onParseCommentSuccess(ArrayList<CommentData> commentData, String parentId, ArrayList<String> moreChildrenFullnames);
|
void onParseCommentSuccess(ArrayList<CommentData> expandedComments, String parentId,
|
||||||
|
ArrayList<String> moreChildrenFullnames);
|
||||||
void onParseCommentFailed();
|
void onParseCommentFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ParseMoreCommentBasicInfoListener {
|
|
||||||
void onParseMoreCommentBasicInfoSuccess(String commaSeparatedChildrenId);
|
|
||||||
void onParseMoreCommentBasicInfoFailed();
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ParseSentCommentListener {
|
interface ParseSentCommentListener {
|
||||||
void onParseSentCommentSuccess(CommentData commentData);
|
void onParseSentCommentSuccess(CommentData commentData);
|
||||||
void onParseSentCommentFailed();
|
void onParseSentCommentFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale,
|
static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale,
|
||||||
boolean isPost, int depth, ParseCommentListener parseCommentListener) {
|
ParseCommentListener parseCommentListener) {
|
||||||
try {
|
try {
|
||||||
JSONArray childrenArray = new JSONArray(response);
|
JSONArray childrenArray = new JSONArray(response);
|
||||||
String parentId = childrenArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY)
|
String parentId = childrenArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY)
|
||||||
.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.NAME_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);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, depth, parseCommentListener).execute();
|
new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, 0, parseCommentListener).execute();
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
if(e.getMessage() != null) {
|
if(e.getMessage() != null) {
|
||||||
@ -74,9 +64,10 @@ class ParseComment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> {
|
private static class ParseCommentAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
private JSONArray comments;
|
private JSONArray commentsJSONArray;
|
||||||
private ArrayList<CommentData> commentData;
|
private ArrayList<CommentData> comments;
|
||||||
private ArrayList<CommentData> newcommentData;
|
private ArrayList<CommentData> newComments;
|
||||||
|
private ArrayList<CommentData> expandedNewComments;
|
||||||
private ArrayList<String> moreChildrenFullnames;
|
private ArrayList<String> moreChildrenFullnames;
|
||||||
private Locale locale;
|
private Locale locale;
|
||||||
private String parentId;
|
private String parentId;
|
||||||
@ -84,11 +75,12 @@ class ParseComment {
|
|||||||
private ParseCommentListener parseCommentListener;
|
private ParseCommentListener parseCommentListener;
|
||||||
private boolean parseFailed;
|
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){
|
@Nullable String parentId, int depth, ParseCommentListener parseCommentListener){
|
||||||
|
this.commentsJSONArray = commentsJSONArray;
|
||||||
this.comments = comments;
|
this.comments = comments;
|
||||||
this.commentData = commentData;
|
newComments = new ArrayList<>();
|
||||||
newcommentData = new ArrayList<>();
|
expandedNewComments = new ArrayList<>();
|
||||||
moreChildrenFullnames = new ArrayList<>();
|
moreChildrenFullnames = new ArrayList<>();
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.parentId = parentId;
|
this.parentId = parentId;
|
||||||
@ -100,7 +92,8 @@ class ParseComment {
|
|||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... voids) {
|
protected Void doInBackground(Void... voids) {
|
||||||
try {
|
try {
|
||||||
parseCommentRecursion(comments, newcommentData, moreChildrenFullnames, depth, locale);
|
parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale);
|
||||||
|
makeChildrenVisible(newComments, expandedNewComments);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
parseFailed = true;
|
parseFailed = true;
|
||||||
if(e.getMessage() != null) {
|
if(e.getMessage() != null) {
|
||||||
@ -113,8 +106,8 @@ class ParseComment {
|
|||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(Void aVoid) {
|
protected void onPostExecute(Void aVoid) {
|
||||||
if(!parseFailed) {
|
if(!parseFailed) {
|
||||||
commentData.addAll(newcommentData);
|
comments.addAll(expandedNewComments);
|
||||||
parseCommentListener.onParseCommentSuccess(commentData, parentId, moreChildrenFullnames);
|
parseCommentListener.onParseCommentSuccess(comments, parentId, moreChildrenFullnames);
|
||||||
} else {
|
} else {
|
||||||
parseCommentListener.onParseCommentFailed();
|
parseCommentListener.onParseCommentFailed();
|
||||||
}
|
}
|
||||||
@ -131,7 +124,7 @@ class ParseComment {
|
|||||||
|
|
||||||
JSONObject more = comments.getJSONObject(comments.length() - 1).getJSONObject(JSONUtils.DATA_KEY);
|
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)) {
|
if(more.has(JSONUtils.COUNT_KEY)) {
|
||||||
JSONArray childrenArray = more.getJSONArray(JSONUtils.CHILDREN_KEY);
|
JSONArray childrenArray = more.getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||||
|
|
||||||
@ -163,47 +156,17 @@ class ParseComment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ParseMoreCommentBasicInfoAsyncTask extends AsyncTask<Void, Void, Void> {
|
private static void makeChildrenVisible(ArrayList<CommentData> comments, ArrayList<CommentData> visibleComments) {
|
||||||
private JSONArray children;
|
for(CommentData c : comments) {
|
||||||
private StringBuilder commaSeparatedChildren;
|
visibleComments.add(c);
|
||||||
private ParseMoreCommentBasicInfoListener parseMoreCommentBasicInfoListener;
|
if(c.hasReply()) {
|
||||||
private boolean parseFailed;
|
c.setExpanded(true);
|
||||||
|
makeChildrenVisible(c.getChildren(), visibleComments);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
if(c.hasMoreChildrenFullnames() && c.getMoreChildrenFullnames().size() > c.getMoreChildrenStartingIndex()) {
|
||||||
|
//Add a load more placeholder
|
||||||
@Override
|
visibleComments.add(new CommentData(c.getDepth() + 1));
|
||||||
protected Void doInBackground(Void... voids) {
|
c.addChild(new CommentData(c.getDepth() + 1), c.getChildren().size());
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -246,6 +209,7 @@ class ParseComment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException {
|
private static CommentData parseSingleComment(JSONObject singleCommentData, int depth, Locale locale) throws JSONException {
|
||||||
String id = singleCommentData.getString(JSONUtils.ID_KEY);
|
String id = singleCommentData.getString(JSONUtils.ID_KEY);
|
||||||
String fullName = singleCommentData.getString(JSONUtils.NAME_KEY);
|
String fullName = singleCommentData.getString(JSONUtils.NAME_KEY);
|
||||||
|
@ -18,7 +18,7 @@ public interface RedditAPI {
|
|||||||
|
|
||||||
@GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1")
|
@GET("{subredditNamePrefixed}/comments/{article}.json?raw_json=1")
|
||||||
Call<String> getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed,
|
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")
|
@GET("r/{subredditName}/about.json?raw_json=1")
|
||||||
Call<String> getSubredditData(@Path("subredditName") String subredditName);
|
Call<String> getSubredditData(@Path("subredditName") String subredditName);
|
||||||
|
@ -498,14 +498,14 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
mNoCommentWrapperLinearLayout.setVisibility(View.GONE);
|
mNoCommentWrapperLinearLayout.setVisibility(View.GONE);
|
||||||
|
|
||||||
FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(),
|
FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(),
|
||||||
null, mLocale, true, 0, new FetchComment.FetchCommentListener() {
|
mLocale, new FetchComment.FetchCommentListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchCommentSuccess(ArrayList<CommentData> commentsData,
|
public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments,
|
||||||
String parentId, ArrayList<String> children) {
|
String parentId, ArrayList<String> children) {
|
||||||
ViewPostDetailActivity.this.children = children;
|
ViewPostDetailActivity.this.children = children;
|
||||||
mCommentProgressbar.setVisibility(View.GONE);
|
mCommentProgressbar.setVisibility(View.GONE);
|
||||||
|
|
||||||
if (commentsData.size() > 0) {
|
if (expandedComments.size() > 0) {
|
||||||
if(mAdapter == null) {
|
if(mAdapter == null) {
|
||||||
mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
|
mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
|
||||||
if(!isLoadingMoreChildren) {
|
if(!isLoadingMoreChildren) {
|
||||||
@ -520,8 +520,8 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
|
mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
|
||||||
mOauthRetrofit, mSharedPreferences, commentsData, mRecyclerView,
|
mOauthRetrofit, mSharedPreferences, expandedComments,
|
||||||
mPost.getSubredditNamePrefixed(), mPost.getId(), mLocale);
|
mPost.getSubredditNamePrefixed(), mLocale);
|
||||||
mRecyclerView.setAdapter(mAdapter);
|
mRecyclerView.setAdapter(mAdapter);
|
||||||
|
|
||||||
mCommentCardView.setVisibility(View.VISIBLE);
|
mCommentCardView.setVisibility(View.VISIBLE);
|
||||||
@ -544,8 +544,8 @@ public class ViewPostDetailActivity extends AppCompatActivity {
|
|||||||
FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), children, startingIndex,
|
FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), children, startingIndex,
|
||||||
0, mLocale, new FetchComment.FetchMoreCommentListener() {
|
0, mLocale, new FetchComment.FetchMoreCommentListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchMoreCommentSuccess(ArrayList<CommentData> commentsData, int childrenStartingIndex) {
|
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex) {
|
||||||
mAdapter.addComments(commentsData);
|
mAdapter.addComments(expandedComments);
|
||||||
mChildrenStartingIndex = childrenStartingIndex;
|
mChildrenStartingIndex = childrenStartingIndex;
|
||||||
isLoadingMoreChildren = false;
|
isLoadingMoreChildren = false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user