From 5d74aa454860c2e0a04d3d8a6c96a14a53e66ad3 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 1 Feb 2020 22:36:43 +0800 Subject: [PATCH] Showing top-level comments at first is available. --- .../Activity/ViewPostDetailActivity.java | 14 ++++++----- .../CommentAndPostRecyclerViewAdapter.java | 7 ++++-- .../infinityforreddit/FetchComment.java | 11 +++++---- .../infinityforreddit/ParseComment.java | 23 +++++++++++++------ .../Utils/SharedPreferencesUtils.java | 1 + app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/main_preferences.xml | 7 +++++- 7 files changed, 43 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java index e0c48770..e516cdfc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java @@ -175,6 +175,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS private boolean mIsSmoothScrolling = false; private boolean mLockFab; private boolean mSwipeUpToHideFab; + private boolean mExpandChildren; private LinearLayoutManager mLinearLayoutManager; private CommentAndPostRecyclerViewAdapter mAdapter; private RecyclerView.SmoothScroller mSmoothScroller; @@ -257,6 +258,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mVolumeKeysNavigateComments = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_COMMENTS, false); mLockFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); mSwipeUpToHideFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false); + mExpandChildren = !mSharedPreferences.getBoolean(SharedPreferencesUtils.SHOW_TOP_LEVEL_COMMENTS_FIRST, false); mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; @@ -494,7 +496,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler, - mVoteButtonsOnTheRight, mShowElapsedTime, + mVoteButtonsOnTheRight, mShowElapsedTime, mExpandChildren, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -617,7 +619,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler, - mVoteButtonsOnTheRight, mShowElapsedTime, + mVoteButtonsOnTheRight, mShowElapsedTime, mExpandChildren, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -640,7 +642,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS mRecyclerView.setAdapter(mAdapter); ParseComment.parseComment(response.body(), new ArrayList<>(), mLocale, - new ParseComment.ParseCommentListener() { + mExpandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList expandedComments, String parentId, ArrayList moreChildrenFullnames) { ViewPostDetailActivity.this.children = moreChildrenFullnames; @@ -725,8 +727,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS } Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; - FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, sortType, mLocale, - new FetchComment.FetchCommentListener() { + FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, sortType, mExpandChildren, + mLocale, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children) { @@ -826,7 +828,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; FetchComment.fetchMoreComment(retrofit, mAccessToken, children, mChildrenStartingIndex, - 0, mLocale, new FetchComment.FetchMoreCommentListener() { + 0, mExpandChildren, mLocale, new FetchComment.FetchMoreCommentListener() { @Override public void onFetchMoreCommentSuccess(ArrayList expandedComments, int childrenStartingIndex) { hasMoreChildren = childrenStartingIndex < children.size(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index e001e632..a506ceba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -119,6 +119,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter expandedComments, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index f40f2e1d..8fb9a851 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -14,8 +14,8 @@ import retrofit2.Retrofit; public class FetchComment { public static void fetchComments(Retrofit retrofit, @Nullable String accessToken, String article, - String commentId, String sortType, Locale locale, - FetchCommentListener fetchCommentListener) { + String commentId, String sortType, boolean expandChildren, + Locale locale, FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call comments; if (accessToken == null) { @@ -38,7 +38,7 @@ public class FetchComment { public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { ParseComment.parseComment(response.body(), new ArrayList<>(), - locale, new ParseComment.ParseCommentListener() { + locale, expandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList expandedComments, String parentId, ArrayList moreChildrenFullnames) { @@ -65,7 +65,8 @@ public class FetchComment { public static void fetchMoreComment(Retrofit retrofit, @Nullable String accessToken, ArrayList allChildren, int startingIndex, - int depth, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { + int depth, boolean expandChildren, Locale locale, + FetchMoreCommentListener fetchMoreCommentListener) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 100; i++) { if (allChildren.size() <= startingIndex + i) { @@ -93,7 +94,7 @@ public class FetchComment { public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale, - depth, new ParseComment.ParseCommentListener() { + depth, expandChildren, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(ArrayList expandedComments, String parentId, ArrayList moreChildrenFullnames) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index d5c86143..54101eba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -23,14 +23,14 @@ import static ml.docilealligator.infinityforreddit.CommentData.VOTE_TYPE_UPVOTE; public class ParseComment { public static void parseComment(String response, ArrayList commentData, Locale locale, - ParseCommentListener parseCommentListener) { + boolean expandChildren, 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); childrenArray = childrenArray.getJSONObject(1).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, 0, parseCommentListener).execute(); + new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, 0, expandChildren, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); parseCommentListener.onParseCommentFailed(); @@ -38,10 +38,10 @@ public class ParseComment { } static void parseMoreComment(String response, ArrayList commentData, Locale locale, - int depth, ParseCommentListener parseCommentListener) { + int depth, boolean expandChildren, ParseCommentListener parseCommentListener) { try { JSONArray childrenArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); - new ParseCommentAsyncTask(childrenArray, commentData, locale, null, depth, parseCommentListener).execute(); + new ParseCommentAsyncTask(childrenArray, commentData, locale, null, depth, expandChildren, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); parseCommentListener.onParseCommentFailed(); @@ -215,11 +215,13 @@ public class ParseComment { private Locale locale; private String parentId; private int depth; + private boolean expandChildren; private ParseCommentListener parseCommentListener; private boolean parseFailed; ParseCommentAsyncTask(JSONArray commentsJSONArray, ArrayList comments, Locale locale, - @Nullable String parentId, int depth, ParseCommentListener parseCommentListener) { + @Nullable String parentId, int depth, boolean expandChildren, + ParseCommentListener parseCommentListener) { this.commentsJSONArray = commentsJSONArray; this.comments = comments; newComments = new ArrayList<>(); @@ -228,6 +230,7 @@ public class ParseComment { this.locale = locale; this.parentId = parentId; this.depth = depth; + this.expandChildren = expandChildren; parseFailed = false; this.parseCommentListener = parseCommentListener; } @@ -236,7 +239,9 @@ public class ParseComment { protected Void doInBackground(Void... voids) { try { parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale); - expandChildren(newComments, expandedNewComments); + if (expandChildren) { + expandChildren(newComments, expandedNewComments); + } } catch (JSONException e) { parseFailed = true; } @@ -246,7 +251,11 @@ public class ParseComment { @Override protected void onPostExecute(Void aVoid) { if (!parseFailed) { - comments.addAll(expandedNewComments); + if (expandChildren) { + comments.addAll(expandedNewComments); + } else { + comments.addAll(newComments); + } parseCommentListener.onParseCommentSuccess(comments, parentId, moreChildrenFullnames); } else { parseCommentListener.onParseCommentFailed(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java index 6f065d5e..5ae81003 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Utils/SharedPreferencesUtils.java @@ -71,4 +71,5 @@ public class SharedPreferencesUtils { public static final String OPEN_LINK_IN_APP = "open_link_in_app"; public static final String LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "lock_jump_to_next_top_level_comment_button"; public static final String SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON = "swipe_up_to_hide_jump_to_next_top_level_comments_button"; + public static final String SHOW_TOP_LEVEL_COMMENTS_FIRST = "show_top_level_comments_first"; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d95e7be9..b1b6b636 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -302,6 +302,7 @@ Use Volume Keys to Navigate Comments in Posts Use Volume Keys to Navigate Posts Mute Video + Show Top-level Comments First Show Elapsed Time in Posts and Comments Default Post Layout Show Divider in Compact Layout diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index f79b8234..4f238327 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -39,9 +39,14 @@ + +