From 478b85e67b1798ab6b0bdc4392785c40fe1c1307 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 15 Aug 2019 13:35:25 +0800 Subject: Display single comment thread when clicking a comment from ViewUserDetailActivity instead of showing all the comments. --- .../CommentAndPostRecyclerViewAdapter.java | 102 +++++++++++++---- .../CommentsListingRecyclerViewAdapter.java | 1 + .../infinityforreddit/FetchComment.java | 18 +-- .../infinityforreddit/RedditAPI.java | 15 ++- .../infinityforreddit/ViewPostDetailActivity.java | 123 ++++++++++++++------- 5 files changed, 186 insertions(+), 73 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index b5ca248b..5ecc81df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -56,6 +56,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mVisibleComments; private String mSubredditNamePrefixed; private Locale mLocale; + private String mSingleCommentId; + private boolean mIsSingleCommentThreadMode; private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback; private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; private boolean isInitiallyLoading; @@ -82,8 +85,9 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter(); - mSubredditNamePrefixed = subredditNamePrefixed; + mSubredditNamePrefixed = post.getSubredditNamePrefixed(); mLocale = locale; + mSingleCommentId = singleCommentId; + mIsSingleCommentThreadMode = isSingleCommentThreadMode; mLoadSubredditIconAsyncTask = loadSubredditIconAsyncTask; mCommentRecyclerViewAdapterCallback = commentRecyclerViewAdapterCallback; isInitiallyLoading = true; @@ -122,19 +128,40 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter mCommentRecyclerViewAdapterCallback.retryFetchingMoreComments()); } } + + class ViewAllCommentsViewHolder extends RecyclerView.ViewHolder { + + ViewAllCommentsViewHolder(@NonNull View itemView) { + super(itemView); + + itemView.setOnClickListener(view -> { + if(mActivity != null && mActivity instanceof ViewPostDetailActivity) { + ((ViewPostDetailActivity) mActivity).changeToSingleThreadMode(); + } + }); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java index ffcf707b..c20920a9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentsListingRecyclerViewAdapter.java @@ -196,6 +196,7 @@ class CommentsListingRecyclerViewAdapter extends PagedListAdapter { Intent intent = new Intent(mContext, ViewPostDetailActivity.class); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, getItem(getAdapterPosition()).getLinkId()); + intent.putExtra(ViewPostDetailActivity.EXTRA_SINGLE_COMMENT_ID, getItem(getAdapterPosition()).getId()); mContext.startActivity(intent); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index cec412fc..5558a104 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -23,10 +23,15 @@ class FetchComment { void onFetchMoreCommentFailed(); } - static void fetchComments(Retrofit retrofit, String subredditNamePrefixed, String article, + static void fetchComments(Retrofit retrofit, String article, String commentId, Locale locale, FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call comments = api.getComments(subredditNamePrefixed, article); + Call comments; + if(commentId == null) { + comments = api.getPostAndCommentsById(article); + } else { + comments = api.getPostAndCommentsSingleThreadById(article, commentId); + } comments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -60,9 +65,8 @@ class FetchComment { }); } - static void fetchMoreComment(Retrofit retrofit, String subredditNamePrefixed, - ArrayList allChildren, int startingIndex, int depth, - Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { + static void fetchMoreComment(Retrofit retrofit, ArrayList allChildren, int startingIndex, + int depth, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < 100; i++) { if(allChildren.size() <= startingIndex + i) { @@ -79,10 +83,10 @@ class FetchComment { RedditAPI api = retrofit.create(RedditAPI.class); - Call moreComments = api.getInfo(subredditNamePrefixed, stringBuilder.toString()); + Call moreComments = api.getInfo(stringBuilder.toString()); moreComments.enqueue(new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { ParseComment.parseMoreComment(response.body(), new ArrayList<>(), locale, depth, new ParseComment.ParseCommentListener() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index ce351359..a9caccd6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -21,10 +21,6 @@ public interface RedditAPI { @POST("api/v1/access_token") Call getAccessToken(@HeaderMap Map headers, @FieldMap Map params); - @GET("{subredditNamePrefixed}/comments/{article}.json?&raw_json=1") - Call getComments(@Path("subredditNamePrefixed") String subredditNamePrefixed, - @Path("article") String article); - @GET("r/{subredditName}/about.json?raw_json=1") Call getSubredditData(@Path("subredditName") String subredditName); @@ -74,8 +70,8 @@ public interface RedditAPI { @POST("api/subscribe") Call subredditSubscription(@HeaderMap Map headers, @FieldMap Map params); - @GET("{subredditNamePrefixed}/api/info.json?raw_json=1") - Call getInfo(@Path("subredditNamePrefixed") String subredditNamePrefixed, @Query("id") String id); + @GET("/api/info.json?raw_json=1") + Call getInfo(@Query("id") String id); @GET("subreddits/search.json?raw_json=1&include_over_18=on") Call searchSubreddits(@Query("q") String subredditName, @Query("after") String after, @Query("sort") String sort); @@ -120,9 +116,16 @@ public interface RedditAPI { @GET("/r/{subredditName}/about/rules.json?raw_json=1") Call getRules(@Path("subredditName") String subredditName); + @GET("/comments/{id}/placeholder/{singleCommentId}.json?context=8&raw_json=1") + Call getPostAndCommentsSingleThreadByIdOauth(@Path("id") String id, @Path("singleCommentId") String singleCommentId, + @HeaderMap Map headers); + @GET("/comments/{id}.json?raw_json=1") Call getPostAndCommentsByIdOauth(@Path("id") String id, @HeaderMap Map headers); + @GET("/comments/{id}/placeholder/{singleCommentId}.json?context=8&raw_json=1") + Call getPostAndCommentsSingleThreadById(@Path("id") String id, @Path("singleCommentId") String singleCommentId); + @GET("/comments/{id}.json?raw_json=1") Call getPostAndCommentsById(@Path("id") String id); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 0ec34c57..1039fc96 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -59,6 +59,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo static final String EXTRA_POST_DATA = "EPD"; static final String EXTRA_POST_LIST_POSITION = "EPLI"; static final String EXTRA_POST_ID = "EPI"; + static final String EXTRA_SINGLE_COMMENT_ID = "ESCI"; private static final int EDIT_POST_REQUEST_CODE = 2; static final int EDIT_COMMENT_REQUEST_CODE = 3; @@ -69,6 +70,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo private int orientation; private int postListPosition = -1; + private String mSingleCommentId; @State boolean mNullAccessToken = false; @@ -83,6 +85,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @State boolean isRefreshing = false; @State + boolean isSingleCommentThreadMode = false; + @State ArrayList comments; @State ArrayList children; @@ -186,6 +190,11 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mLinearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLinearLayoutManager); + mSingleCommentId = getIntent().hasExtra(EXTRA_SINGLE_COMMENT_ID) ? getIntent().getExtras().getString(EXTRA_SINGLE_COMMENT_ID) : null; + if(savedInstanceState == null && mSingleCommentId != null) { + isSingleCommentThreadMode = true; + } + orientation = getResources().getConfiguration().orientation; if(!mNullAccessToken && mAccessToken == null) { @@ -246,7 +255,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, - mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, + mLocale, mSingleCommentId, isSingleCommentThreadMode, mLoadSubredditIconAsyncTask, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -264,11 +273,11 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mRecyclerView.setAdapter(mAdapter); if(comments == null) { - fetchComments(); + fetchComments(false); } else { if(isRefreshing) { isRefreshing = false; - refresh(false); + refresh(true, true); } else { mAdapter.addComments(comments, hasMoreChildren); if(isLoadingMoreChildren) { @@ -288,9 +297,19 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo Call postAndComments; if(mAccessToken == null) { - postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsById(subredditId); + if(isSingleCommentThreadMode && mSingleCommentId != null) { + postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadById(subredditId, mSingleCommentId); + } else { + postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsById(subredditId); + } } else { - postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, RedditUtils.getOAuthHeader(mAccessToken)); + if(isSingleCommentThreadMode && mSingleCommentId != null) { + postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadByIdOauth(subredditId, + mSingleCommentId, RedditUtils.getOAuthHeader(mAccessToken)); + } else { + postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, + RedditUtils.getOAuthHeader(mAccessToken)); + } } postAndComments.enqueue(new Callback() { @Override @@ -312,7 +331,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, - mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, + mLocale, mSingleCommentId, isSingleCommentThreadMode, mLoadSubredditIconAsyncTask, new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { @@ -382,11 +401,15 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo }); } - private void fetchComments() { + private void fetchComments(boolean changeRefreshState) { + mAdapter.setSingleComment(mSingleCommentId, isSingleCommentThreadMode); mAdapter.initiallyLoading(); + String commentId = null; + if(isSingleCommentThreadMode) { + commentId = mSingleCommentId; + } - FetchComment.fetchComments(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), - mLocale, new FetchComment.FetchCommentListener() { + FetchComment.fetchComments(mRetrofit, mPost.getId(), commentId, mLocale, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children) { @@ -414,11 +437,17 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } }); } + if(changeRefreshState) { + isRefreshing = false; + } } @Override public void onFetchCommentFailed() { mAdapter.initiallyLoadCommentsFailed(); + if(changeRefreshState) { + isRefreshing = false; + } } }); } @@ -429,7 +458,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } isLoadingMoreChildren = true; - FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), children, mChildrenStartingIndex, + FetchComment.fetchMoreComment(mRetrofit, children, mChildrenStartingIndex, 0, mLocale, new FetchComment.FetchMoreCommentListener() { @Override public void onFetchMoreCommentSuccess(ArrayList expandedComments, int childrenStartingIndex) { @@ -449,7 +478,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo }); } - private void refresh(boolean onlyRefreshPost) { + private void refresh(boolean fetchPost, boolean fetchComments) { if(!isRefreshing) { isRefreshing = true; mChildrenStartingIndex = 0; @@ -457,32 +486,38 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mFetchPostInfoLinearLayout.setVisibility(View.GONE); mGlide.clear(mFetchPostInfoImageView); - if(!onlyRefreshPost) { - fetchComments(); + if(fetchComments) { + if(!fetchPost) { + fetchComments(true); + } else { + fetchComments(false); + } } - Retrofit retrofit; - if(mAccessToken == null) { - retrofit = mRetrofit; - } else { - retrofit = mOauthRetrofit; - } - FetchPost.fetchPost(retrofit, mPost.getId(), mAccessToken, mLocale, - new FetchPost.FetchPostListener() { - @Override - public void fetchPostSuccess(Post post) { - mPost = post; - mAdapter.updatePost(mPost); - EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); - isRefreshing = false; - } + if(fetchPost) { + Retrofit retrofit; + if(mAccessToken == null) { + retrofit = mRetrofit; + } else { + retrofit = mOauthRetrofit; + } + FetchPost.fetchPost(retrofit, mPost.getId(), mAccessToken, mLocale, + new FetchPost.FetchPostListener() { + @Override + public void fetchPostSuccess(Post post) { + mPost = post; + mAdapter.updatePost(mPost); + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + isRefreshing = false; + } - @Override - public void fetchPostFailed() { - showMessage(R.string.refresh_post_failed); - isRefreshing = false; - } - }); + @Override + public void fetchPostFailed() { + showMessage(R.string.refresh_post_failed); + isRefreshing = false; + } + }); + } } } @@ -510,7 +545,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { - refresh(true); + refresh(true, false); showMessage(R.string.mark_nsfw_success); } else { showMessage(R.string.mark_nsfw_failed); @@ -532,7 +567,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { - refresh(true); + refresh(true, false); showMessage(R.string.unmark_nsfw_success); } else { showMessage(R.string.unmark_nsfw_failed); @@ -554,7 +589,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { - refresh(true); + refresh(true, false); showMessage(R.string.mark_spoiler_success); } else { showMessage(R.string.mark_spoiler_failed); @@ -576,7 +611,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { - refresh(true); + refresh(true, false); showMessage(R.string.unmark_spoiler_success); } else { showMessage(R.string.unmark_spoiler_failed); @@ -611,6 +646,12 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo .show(); } + void changeToSingleThreadMode() { + isSingleCommentThreadMode = false; + mSingleCommentId = null; + refresh(false, true); + } + @Subscribe public void onPostUpdateEvent(PostUpdateEventToDetailActivity event) { if(mPost.getId().equals(event.postId)) { @@ -654,7 +695,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.action_refresh_view_post_detail_activity: - refresh(false); + refresh(true, true); return true; case R.id.action_comment_view_post_detail_activity: if(mAccessToken == null) { @@ -746,7 +787,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } } else if(requestCode == EDIT_POST_REQUEST_CODE) { if(resultCode == RESULT_OK) { - refresh(true); + refresh(true, false); } } else if(requestCode == EDIT_COMMENT_REQUEST_CODE) { if(resultCode == RESULT_OK) { @@ -794,7 +835,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { - refresh(true); + refresh(true, false); showMessage(R.string.update_flair_success); } else { showMessage(R.string.update_flair_failed); -- cgit v1.2.3