Showing top-level comments at first is available.

This commit is contained in:
Alex Ning 2020-02-01 22:36:43 +08:00
parent 9dc596b907
commit 5d74aa4548
7 changed files with 43 additions and 21 deletions

View File

@ -175,6 +175,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
private boolean mIsSmoothScrolling = false; private boolean mIsSmoothScrolling = false;
private boolean mLockFab; private boolean mLockFab;
private boolean mSwipeUpToHideFab; private boolean mSwipeUpToHideFab;
private boolean mExpandChildren;
private LinearLayoutManager mLinearLayoutManager; private LinearLayoutManager mLinearLayoutManager;
private CommentAndPostRecyclerViewAdapter mAdapter; private CommentAndPostRecyclerViewAdapter mAdapter;
private RecyclerView.SmoothScroller mSmoothScroller; private RecyclerView.SmoothScroller mSmoothScroller;
@ -257,6 +258,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mVolumeKeysNavigateComments = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_COMMENTS, false); mVolumeKeysNavigateComments = mSharedPreferences.getBoolean(SharedPreferencesUtils.VOLUME_KEYS_NAVIGATE_COMMENTS, false);
mLockFab = mSharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, 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); 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); mGlide = Glide.with(this);
mLocale = getResources().getConfiguration().locale; mLocale = getResources().getConfiguration().locale;
@ -494,7 +496,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit,
mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost,
mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler, mLocale, mSingleCommentId, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler,
mVoteButtonsOnTheRight, mShowElapsedTime, mVoteButtonsOnTheRight, mShowElapsedTime, mExpandChildren,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override @Override
public void updatePost(Post post) { public void updatePost(Post post) {
@ -617,7 +619,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide,
mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId,
isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler, isSingleCommentThreadMode, mNeedBlurNsfw, mNeedBlurSpoiler,
mVoteButtonsOnTheRight, mShowElapsedTime, mVoteButtonsOnTheRight, mShowElapsedTime, mExpandChildren,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override @Override
public void updatePost(Post post) { public void updatePost(Post post) {
@ -640,7 +642,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
ParseComment.parseComment(response.body(), new ArrayList<>(), mLocale, ParseComment.parseComment(response.body(), new ArrayList<>(), mLocale,
new ParseComment.ParseCommentListener() { mExpandChildren, new ParseComment.ParseCommentListener() {
@Override @Override
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments, String parentId, ArrayList<String> moreChildrenFullnames) { public void onParseCommentSuccess(ArrayList<CommentData> expandedComments, String parentId, ArrayList<String> moreChildrenFullnames) {
ViewPostDetailActivity.this.children = moreChildrenFullnames; ViewPostDetailActivity.this.children = moreChildrenFullnames;
@ -725,8 +727,8 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
} }
Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit;
FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, sortType, mLocale, FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, sortType, mExpandChildren,
new FetchComment.FetchCommentListener() { mLocale, new FetchComment.FetchCommentListener() {
@Override @Override
public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> children) { String parentId, ArrayList<String> children) {
@ -826,7 +828,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit;
FetchComment.fetchMoreComment(retrofit, mAccessToken, children, mChildrenStartingIndex, FetchComment.fetchMoreComment(retrofit, mAccessToken, children, mChildrenStartingIndex,
0, mLocale, new FetchComment.FetchMoreCommentListener() { 0, mExpandChildren, mLocale, new FetchComment.FetchMoreCommentListener() {
@Override @Override
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex) { public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex) {
hasMoreChildren = childrenStartingIndex < children.size(); hasMoreChildren = childrenStartingIndex < children.size();

View File

@ -119,6 +119,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
private boolean mNeedBlurSpoiler; private boolean mNeedBlurSpoiler;
private boolean mVoteButtonsOnTheRight; private boolean mVoteButtonsOnTheRight;
private boolean mShowElapsedTime; private boolean mShowElapsedTime;
private boolean mExpandChildren;
private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback; private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback;
private boolean isInitiallyLoading; private boolean isInitiallyLoading;
private boolean isInitiallyLoadingFailed; private boolean isInitiallyLoadingFailed;
@ -134,7 +135,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
String accessToken, String accountName, Post post, Locale locale, String accessToken, String accountName, Post post, Locale locale,
String singleCommentId, boolean isSingleCommentThreadMode, String singleCommentId, boolean isSingleCommentThreadMode,
boolean needBlurNSFW, boolean needBlurSpoiler, boolean voteButtonsOnTheRight, boolean needBlurNSFW, boolean needBlurSpoiler, boolean voteButtonsOnTheRight,
boolean showElapsedTime, boolean showElapsedTime, boolean expandChildren,
CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) {
mActivity = activity; mActivity = activity;
mRetrofit = retrofit; mRetrofit = retrofit;
@ -224,6 +225,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
mNeedBlurSpoiler = needBlurSpoiler; mNeedBlurSpoiler = needBlurSpoiler;
mVoteButtonsOnTheRight = voteButtonsOnTheRight; mVoteButtonsOnTheRight = voteButtonsOnTheRight;
mShowElapsedTime = showElapsedTime; mShowElapsedTime = showElapsedTime;
mExpandChildren = expandChildren;
mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback; mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback;
isInitiallyLoading = true; isInitiallyLoading = true;
isInitiallyLoadingFailed = false; isInitiallyLoadingFailed = false;
@ -1107,7 +1109,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy
Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit;
FetchComment.fetchMoreComment(retrofit, mAccessToken, parentComment.getMoreChildrenFullnames(), FetchComment.fetchMoreComment(retrofit, mAccessToken, parentComment.getMoreChildrenFullnames(),
parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale, parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1,
mExpandChildren, mLocale,
new FetchComment.FetchMoreCommentListener() { new FetchComment.FetchMoreCommentListener() {
@Override @Override
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments,

View File

@ -14,8 +14,8 @@ import retrofit2.Retrofit;
public class FetchComment { public class FetchComment {
public static void fetchComments(Retrofit retrofit, @Nullable String accessToken, String article, public static void fetchComments(Retrofit retrofit, @Nullable String accessToken, String article,
String commentId, String sortType, Locale locale, String commentId, String sortType, boolean expandChildren,
FetchCommentListener fetchCommentListener) { Locale locale, FetchCommentListener fetchCommentListener) {
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> comments; Call<String> comments;
if (accessToken == null) { if (accessToken == null) {
@ -38,7 +38,7 @@ public class FetchComment {
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, new ParseComment.ParseCommentListener() { locale, expandChildren, new ParseComment.ParseCommentListener() {
@Override @Override
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments, public void onParseCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> moreChildrenFullnames) { String parentId, ArrayList<String> moreChildrenFullnames) {
@ -65,7 +65,8 @@ public class FetchComment {
public static void fetchMoreComment(Retrofit retrofit, @Nullable String accessToken, public static void fetchMoreComment(Retrofit retrofit, @Nullable String accessToken,
ArrayList<String> allChildren, int startingIndex, ArrayList<String> allChildren, int startingIndex,
int depth, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { int depth, boolean expandChildren, Locale locale,
FetchMoreCommentListener fetchMoreCommentListener) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
if (allChildren.size() <= startingIndex + i) { if (allChildren.size() <= startingIndex + i) {
@ -93,7 +94,7 @@ public class FetchComment {
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.parseMoreComment(response.body(), new ArrayList<>(), locale, ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale,
depth, new ParseComment.ParseCommentListener() { depth, expandChildren, new ParseComment.ParseCommentListener() {
@Override @Override
public void onParseCommentSuccess(ArrayList<CommentData> expandedComments, public void onParseCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> moreChildrenFullnames) { String parentId, ArrayList<String> moreChildrenFullnames) {

View File

@ -23,14 +23,14 @@ import static ml.docilealligator.infinityforreddit.CommentData.VOTE_TYPE_UPVOTE;
public class ParseComment { public class ParseComment {
public static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale, public static void parseComment(String response, ArrayList<CommentData> commentData, Locale locale,
ParseCommentListener parseCommentListener) { boolean expandChildren, 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);
childrenArray = childrenArray.getJSONObject(1).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, 0, parseCommentListener).execute(); new ParseCommentAsyncTask(childrenArray, commentData, locale, parentId, 0, expandChildren, parseCommentListener).execute();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
parseCommentListener.onParseCommentFailed(); parseCommentListener.onParseCommentFailed();
@ -38,10 +38,10 @@ public class ParseComment {
} }
static void parseMoreComment(String response, ArrayList<CommentData> commentData, Locale locale, static void parseMoreComment(String response, ArrayList<CommentData> commentData, Locale locale,
int depth, ParseCommentListener parseCommentListener) { int depth, boolean expandChildren, ParseCommentListener parseCommentListener) {
try { try {
JSONArray childrenArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); 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) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
parseCommentListener.onParseCommentFailed(); parseCommentListener.onParseCommentFailed();
@ -215,11 +215,13 @@ public class ParseComment {
private Locale locale; private Locale locale;
private String parentId; private String parentId;
private int depth; private int depth;
private boolean expandChildren;
private ParseCommentListener parseCommentListener; private ParseCommentListener parseCommentListener;
private boolean parseFailed; private boolean parseFailed;
ParseCommentAsyncTask(JSONArray commentsJSONArray, ArrayList<CommentData> comments, Locale locale, ParseCommentAsyncTask(JSONArray commentsJSONArray, ArrayList<CommentData> comments, Locale locale,
@Nullable String parentId, int depth, ParseCommentListener parseCommentListener) { @Nullable String parentId, int depth, boolean expandChildren,
ParseCommentListener parseCommentListener) {
this.commentsJSONArray = commentsJSONArray; this.commentsJSONArray = commentsJSONArray;
this.comments = comments; this.comments = comments;
newComments = new ArrayList<>(); newComments = new ArrayList<>();
@ -228,6 +230,7 @@ public class ParseComment {
this.locale = locale; this.locale = locale;
this.parentId = parentId; this.parentId = parentId;
this.depth = depth; this.depth = depth;
this.expandChildren = expandChildren;
parseFailed = false; parseFailed = false;
this.parseCommentListener = parseCommentListener; this.parseCommentListener = parseCommentListener;
} }
@ -236,7 +239,9 @@ public class ParseComment {
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
try { try {
parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale); parseCommentRecursion(commentsJSONArray, newComments, moreChildrenFullnames, depth, locale);
expandChildren(newComments, expandedNewComments); if (expandChildren) {
expandChildren(newComments, expandedNewComments);
}
} catch (JSONException e) { } catch (JSONException e) {
parseFailed = true; parseFailed = true;
} }
@ -246,7 +251,11 @@ public class ParseComment {
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
if (!parseFailed) { if (!parseFailed) {
comments.addAll(expandedNewComments); if (expandChildren) {
comments.addAll(expandedNewComments);
} else {
comments.addAll(newComments);
}
parseCommentListener.onParseCommentSuccess(comments, parentId, moreChildrenFullnames); parseCommentListener.onParseCommentSuccess(comments, parentId, moreChildrenFullnames);
} else { } else {
parseCommentListener.onParseCommentFailed(); parseCommentListener.onParseCommentFailed();

View File

@ -71,4 +71,5 @@ public class SharedPreferencesUtils {
public static final String OPEN_LINK_IN_APP = "open_link_in_app"; 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 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 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";
} }

View File

@ -302,6 +302,7 @@
<string name="settings_volume_keys_navigate_comments_title">Use Volume Keys to Navigate Comments in Posts</string> <string name="settings_volume_keys_navigate_comments_title">Use Volume Keys to Navigate Comments in Posts</string>
<string name="settings_volume_keys_navigate_posts_title">Use Volume Keys to Navigate Posts</string> <string name="settings_volume_keys_navigate_posts_title">Use Volume Keys to Navigate Posts</string>
<string name="settings_mute_video_title">Mute Video</string> <string name="settings_mute_video_title">Mute Video</string>
<string name="settings_show_top_level_comments_first_title">Show Top-level Comments First</string>
<string name="settings_show_elapsed_time">Show Elapsed Time in Posts and Comments</string> <string name="settings_show_elapsed_time">Show Elapsed Time in Posts and Comments</string>
<string name="settings_default_post_layout">Default Post Layout</string> <string name="settings_default_post_layout">Default Post Layout</string>
<string name="settings_show_divider_in_compact_layout">Show Divider in Compact Layout</string> <string name="settings_show_divider_in_compact_layout">Show Divider in Compact Layout</string>

View File

@ -39,9 +39,14 @@
<SwitchPreference <SwitchPreference
app:defaultValue="false" app:defaultValue="false"
app:key="mute_video" app:key="mute_video"
android:icon="@drawable/ic_mute_preferences_24dp" app:icon="@drawable/ic_mute_preferences_24dp"
app:title="@string/settings_mute_video_title" /> app:title="@string/settings_mute_video_title" />
<SwitchPreference
app:defaultValue="false"
app:key="show_top_level_comments_first"
app:title="@string/settings_show_top_level_comments_first_title" />
<ListPreference <ListPreference
app:defaultValue="2.5" app:defaultValue="2.5"
android:entries="@array/settings_lazy_mode_interval" android:entries="@array/settings_lazy_mode_interval"