From dd3b72bc1fa8ba6d88c8aaa548f3be010e5ab58d Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Sat, 29 Jul 2023 21:42:36 +0200 Subject: [PATCH] Basic single comment view Closes #27 --- .../activities/ViewPostDetailActivity.java | 6 +- .../CommentsListingRecyclerViewAdapter.java | 3 +- .../comment/ParseComment.java | 14 +- .../fragments/ViewPostDetailFragment.java | 219 ++++++------------ 4 files changed, 90 insertions(+), 152 deletions(-) diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java index c9869dfa..cedd3c84 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java @@ -869,7 +869,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele if (postListPosition == position && post != null) { bundle.putParcelable(ViewPostDetailFragment.EXTRA_POST_DATA, post); bundle.putInt(ViewPostDetailFragment.EXTRA_POST_LIST_POSITION, position); - bundle.putString(ViewPostDetailFragment.EXTRA_SINGLE_COMMENT_ID, getIntent().getStringExtra(EXTRA_SINGLE_COMMENT_ID)); + bundle.putInt(ViewPostDetailFragment.EXTRA_SINGLE_COMMENT_ID, getIntent().getIntExtra(EXTRA_SINGLE_COMMENT_ID, 0)); bundle.putString(ViewPostDetailFragment.EXTRA_CONTEXT_NUMBER, getIntent().getStringExtra(EXTRA_CONTEXT_NUMBER)); bundle.putString(ViewPostDetailFragment.EXTRA_MESSAGE_FULLNAME, getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME)); } else { @@ -885,12 +885,12 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } } else { if (post == null) { - bundle.putString(ViewPostDetailFragment.EXTRA_POST_ID, getIntent().getStringExtra(EXTRA_POST_ID)); + bundle.putInt(ViewPostDetailFragment.EXTRA_POST_ID, getIntent().getIntExtra(EXTRA_POST_ID, 0)); } else { bundle.putParcelable(ViewPostDetailFragment.EXTRA_POST_DATA, post); bundle.putInt(ViewPostDetailFragment.EXTRA_POST_LIST_POSITION, postListPosition); } - bundle.putString(ViewPostDetailFragment.EXTRA_SINGLE_COMMENT_ID, getIntent().getStringExtra(EXTRA_SINGLE_COMMENT_ID)); + bundle.putInt(ViewPostDetailFragment.EXTRA_SINGLE_COMMENT_ID, getIntent().getIntExtra(EXTRA_SINGLE_COMMENT_ID, 0)); bundle.putString(ViewPostDetailFragment.EXTRA_CONTEXT_NUMBER, getIntent().getStringExtra(EXTRA_CONTEXT_NUMBER)); bundle.putString(ViewPostDetailFragment.EXTRA_MESSAGE_FULLNAME, getIntent().getStringExtra(EXTRA_MESSAGE_FULLNAME)); } diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java index c8902798..098c54d4 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java @@ -437,6 +437,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter expandedNewComments = new ArrayList<>(); ArrayList moreChildrenIds = new ArrayList<>(); - ArrayList newComments = new ArrayList<>(); Map parsedComments = new HashMap<>(); @@ -51,8 +50,13 @@ public class ParseComment { } } Comment parentComment = (commentId != null) ? parsedComments.get(commentId) : null; - if (parentComment != null && parentComment.getDepth() == 0) - parentComment = null; + if (parentComment != null) { + if (parentComment.getDepth() == 0) { + parentComment = null; + } else { + expandedNewComments.add(parentComment); + } + } for (int i = orderedComments.size() - 1; i >= 0; i--) { Comment c = orderedComments.get(i); @@ -66,9 +70,7 @@ public class ParseComment { } //Add all comments to newComments - for (int i = 0; i < topLevelComments.size(); i++) { - newComments.add(topLevelComments.get(i)); - } + ArrayList newComments = new ArrayList<>(topLevelComments); expandChildren(newComments, expandedNewComments, expandChildren); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java index 263de60c..a0df5d15 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java @@ -85,7 +85,6 @@ import eu.toldi.infinityforlemmy.activities.SubmitCrosspostActivity; import eu.toldi.infinityforlemmy.activities.ViewPostDetailActivity; import eu.toldi.infinityforlemmy.adapters.CommentsRecyclerViewAdapter; import eu.toldi.infinityforlemmy.adapters.PostDetailRecyclerViewAdapter; -import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.apis.RedditAPI; import eu.toldi.infinityforlemmy.apis.StreamableAPI; import eu.toldi.infinityforlemmy.asynctasks.LoadUserData; @@ -95,7 +94,6 @@ import eu.toldi.infinityforlemmy.comment.Comment; import eu.toldi.infinityforlemmy.comment.FetchComment; import eu.toldi.infinityforlemmy.comment.FetchRemovedComment; import eu.toldi.infinityforlemmy.comment.FetchRemovedCommentReveddit; -import eu.toldi.infinityforlemmy.comment.ParseComment; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.customviews.CustomToroContainer; import eu.toldi.infinityforlemmy.customviews.LinearLayoutManagerBugFixed; @@ -109,7 +107,6 @@ import eu.toldi.infinityforlemmy.message.ReadMessage; import eu.toldi.infinityforlemmy.post.FetchPost; import eu.toldi.infinityforlemmy.post.FetchRemovedPost; import eu.toldi.infinityforlemmy.post.HidePost; -import eu.toldi.infinityforlemmy.post.ParsePost; import eu.toldi.infinityforlemmy.post.Post; import eu.toldi.infinityforlemmy.readpost.InsertReadPost; import eu.toldi.infinityforlemmy.subreddit.FetchSubredditData; @@ -546,11 +543,11 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } }; - mSingleCommentId = (getArguments().getString(EXTRA_SINGLE_COMMENT_ID) == null) ? null : Integer.valueOf(getArguments().getString(EXTRA_SINGLE_COMMENT_ID)); + mSingleCommentId = getArguments().getInt(EXTRA_SINGLE_COMMENT_ID, 0); mContextNumber = getArguments().getString(EXTRA_CONTEXT_NUMBER, "8"); if (savedInstanceState == null) { - if (mSingleCommentId != null) { + if (mSingleCommentId != 0) { isSingleCommentThreadMode = true; } mMessageFullname = getArguments().getString(EXTRA_MESSAGE_FULLNAME); @@ -594,7 +591,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } if (mPost == null) { - fetchPostAndCommentsById(getArguments().getString(EXTRA_POST_ID)); + fetchPostAndCommentsById(getArguments().getInt(EXTRA_POST_ID)); } else { setupMenu(); @@ -1240,153 +1237,91 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic return true; } - private void fetchPostAndCommentsById(String subredditId) { + private void fetchPostAndCommentsById(int subredditId) { mFetchPostInfoLinearLayout.setVisibility(View.GONE); mSwipeRefreshLayout.setRefreshing(true); mGlide.clear(mFetchPostInfoImageView); - - Call postAndComments = mRetrofit.getRetrofit().create(LemmyAPI.class).getComments("All", sortType.value, 5, 1, 25, null, null, Integer.valueOf(subredditId), mSingleCommentId, false, mAccessToken); - postAndComments.enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (!isAdded()) { - return; - } - mSwipeRefreshLayout.setRefreshing(false); - - if (response.isSuccessful()) { - ParsePost.parsePost(mExecutor, new Handler(), response.body(), new ParsePost.ParsePostListener() { - @Override - public void onParsePostSuccess(Post post) { - mPost = post; - tryMarkingPostAsRead(); - - setupMenu(); - - mPostAdapter = new PostDetailRecyclerViewAdapter(activity, - ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, - mRetrofit.getRetrofit(), mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, - mStreamableApiProvider, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, - mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, - mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, - mPostDetailsSharedPreferences, mExoCreator, - post1 -> EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); - - mCommentsAdapter = new CommentsRecyclerViewAdapter(activity, - ViewPostDetailFragment.this, mCustomThemeWrapper, mExecutor, - mRetrofit.getRetrofit(), mOauthRetrofit, mAccessToken, mAccountName, mPost, mLocale, - mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences, - new CommentsRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { - @Override - public void retryFetchingComments() { - fetchCommentsRespectRecommendedSort(false); - } - - @Override - public void retryFetchingMoreComments() { - isLoadingMoreChildren = false; - loadMoreChildrenSuccess = true; - - fetchMoreComments(); - } - - @Override - public SortType.Type getSortType() { - return sortType; - } - }); - if (mCommentsRecyclerView != null) { - mRecyclerView.setAdapter(mPostAdapter); - mCommentsRecyclerView.setAdapter(mCommentsAdapter); - } else { - mConcatAdapter = new ConcatAdapter(mPostAdapter, mCommentsAdapter); - mRecyclerView.setAdapter(mConcatAdapter); - } - - if (mRespectSubredditRecommendedSortType) { - fetchCommentsRespectRecommendedSort(false); - } else { - ParseComment.parseComments(mExecutor, new Handler(), response.body(), null, - mExpandChildren, new ParseComment.ParseCommentListener() { + FetchPost.fetchPost(mExecutor, new Handler(), mRetrofit.getRetrofit(), String.valueOf(subredditId), mAccessToken, + new FetchPost.FetchPostListener() { + @Override + public void fetchPostSuccess(Post post) { + if (!isAdded()) { + return; + } + mPost = post; + mPostAdapter = new PostDetailRecyclerViewAdapter(activity, + ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, + mRetrofit.getRetrofit(), mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, + mStreamableApiProvider, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, + mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, + mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, + mPostDetailsSharedPreferences, mExoCreator, + post1 -> EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); + mSwipeRefreshLayout.setRefreshing(false); + FetchComment.fetchComments(mExecutor, new Handler(), mRetrofit.getRetrofit(), mAccessToken, subredditId, mSingleCommentId, sortType, mExpandChildren, 1, new FetchComment.FetchCommentListener() { + @Override + public void onFetchCommentSuccess(ArrayList expandedComments, Integer parentId, ArrayList children) { + pages_loaded++; + mCommentsAdapter = new CommentsRecyclerViewAdapter(activity, + ViewPostDetailFragment.this, mCustomThemeWrapper, mExecutor, + mRetrofit.getRetrofit(), mOauthRetrofit, mAccessToken, mAccountName, mPost, mLocale, + mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences, + new CommentsRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override - public void onParseCommentSuccess(ArrayList topLevelComments, ArrayList expandedComments, Integer parentId, ArrayList moreChildrenIds) { - ViewPostDetailFragment.this.children = moreChildrenIds; - - hasMoreChildren = children.size() != 0; - mCommentsAdapter.addComments(expandedComments, hasMoreChildren); - - if (children.size() > 0) { - (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).clearOnScrollListeners(); - (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - if (!mIsSmoothScrolling && !mLockFab) { - if (!recyclerView.canScrollVertically(1)) { - activity.hideFab(); - } else { - if (dy > 0) { - if (mSwipeUpToHideFab) { - activity.showFab(); - } else { - activity.hideFab(); - } - } else { - if (mSwipeUpToHideFab) { - activity.hideFab(); - } else { - activity.showFab(); - } - } - } - } - - if (!isLoadingMoreChildren && loadMoreChildrenSuccess) { - int visibleItemCount = (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).getLayoutManager().getChildCount(); - int totalItemCount = (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).getLayoutManager().getItemCount(); - int firstVisibleItemPosition = ((LinearLayoutManagerBugFixed) (mCommentsRecyclerView == null ? mRecyclerView : mCommentsRecyclerView).getLayoutManager()).findFirstVisibleItemPosition(); - - if ((visibleItemCount + firstVisibleItemPosition >= totalItemCount) && firstVisibleItemPosition >= 0) { - fetchMoreComments(); - } - } - } - - @Override - public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) { - if (newState == RecyclerView.SCROLL_STATE_IDLE) { - mIsSmoothScrolling = false; - } - } - }); - } + public void retryFetchingComments() { + fetchCommentsRespectRecommendedSort(false); } @Override - public void onParseCommentFailed() { - mCommentsAdapter.initiallyLoadCommentsFailed(); + public void retryFetchingMoreComments() { + isLoadingMoreChildren = false; + loadMoreChildrenSuccess = true; + + fetchMoreComments(); + } + + @Override + public SortType.Type getSortType() { + return sortType; } }); + if (mCommentsRecyclerView != null) { + mRecyclerView.setAdapter(mPostAdapter); + mCommentsRecyclerView.setAdapter(mCommentsAdapter); + } else { + mConcatAdapter = new ConcatAdapter(mPostAdapter, mCommentsAdapter); + mRecyclerView.setAdapter(mConcatAdapter); + } + + if (mRespectSubredditRecommendedSortType) { + fetchCommentsRespectRecommendedSort(false); + } + ViewPostDetailFragment.this.children = children; + + hasMoreChildren = false; + mCommentsAdapter.addComments(expandedComments, hasMoreChildren); + + } - } - @Override - public void onParsePostFail() { - showErrorView(subredditId); - } - }); - } else { - showErrorView(subredditId); - } - } + @Override + public void onFetchCommentFailed() { + if (isAdded()) { + showErrorView(subredditId); + } + } + }); + } + + @Override + public void fetchPostFailed() { + if (isAdded()) { + showMessage(R.string.refresh_post_failed); + isRefreshing = false; + } + } + }); - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - if (isAdded()) { - showErrorView(subredditId); - } - } - }); } private void fetchCommentsRespectRecommendedSort(boolean changeRefreshState, SortType.Type sortType) { @@ -1598,7 +1533,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } } - private void showErrorView(String subredditId) { + private void showErrorView(int subredditId) { mSwipeRefreshLayout.setRefreshing(false); mFetchPostInfoLinearLayout.setVisibility(View.VISIBLE); mFetchPostInfoLinearLayout.setOnClickListener(view -> fetchPostAndCommentsById(subredditId));