Separate CommentAndPostRecyclerViewAdapter to PostDetailRecyclerViewAdapter and CommentsRecyclerViewAdapter and combine them using ConcatAdapter.

This commit is contained in:
Alex Ning 2021-06-21 13:29:07 +08:00
parent 92a1c1d1ae
commit e222fbc5b1
3 changed files with 1829 additions and 1331 deletions

View File

@ -29,6 +29,7 @@ import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
@ -75,7 +76,8 @@ import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActiv
import ml.docilealligator.infinityforreddit.activities.ReportActivity;
import ml.docilealligator.infinityforreddit.activities.SubmitCrosspostActivity;
import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity;
import ml.docilealligator.infinityforreddit.adapters.CommentAndPostRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.adapters.CommentsRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.adapters.PostDetailRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostCommentSortTypeBottomSheetFragment;
@ -212,7 +214,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
private boolean mExpandChildren;
private int mWindowWidth;
private LinearLayoutManager mLinearLayoutManager;
private CommentAndPostRecyclerViewAdapter mAdapter;
private ConcatAdapter mConcatAdapter;
private PostDetailRecyclerViewAdapter mPostAdapter;
private CommentsRecyclerViewAdapter mCommentAdapter;
private RecyclerView.SmoothScroller mSmoothScroller;
private Drawable mSavedIcon;
private Drawable mUnsavedIcon;
@ -365,7 +369,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
if (!(viewHolder instanceof CommentAndPostRecyclerViewAdapter.CommentViewHolder)) {
if (!(viewHolder instanceof CommentsRecyclerViewAdapter.CommentViewHolder)) {
return makeMovementFlags(0, 0);
}
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
@ -387,8 +391,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
if (touchHelper != null) {
touchHelper.attachToRecyclerView(null);
touchHelper.attachToRecyclerView(mRecyclerView);
if (mAdapter != null) {
mAdapter.onItemSwipe(viewHolder, direction, swipeLeftAction, swipeRightAction);
if (mCommentAdapter != null) {
mCommentAdapter.onItemSwipe(viewHolder, direction, swipeLeftAction, swipeRightAction);
}
}
}
@ -517,17 +521,21 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
} else {
setupMenu();
mAdapter = new CommentAndPostRecyclerViewAdapter(activity,
mPostAdapter = new PostDetailRecyclerViewAdapter(activity,
this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit,
mRedgifsRetrofit, mRedditDataRoomDatabase, mGlide,
mWindowWidth, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId,
isSingleCommentThreadMode, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mExoCreator,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
isSingleCommentThreadMode, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mExoCreator, new PostDetailRecyclerViewAdapter.PostDetailRecyclerViewAdapterCallback() {
@Override
public void updatePost(Post post) {
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
}
});
mCommentAdapter = new CommentsRecyclerViewAdapter(activity,
this, mCustomThemeWrapper, mRetrofit, mOauthRetrofit,
mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId,
isSingleCommentThreadMode, mSharedPreferences,
new CommentsRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override
public void retryFetchingComments() {
fetchCommentsRespectRecommendedSort(false);
@ -541,7 +549,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
fetchMoreComments();
}
});
mRecyclerView.setAdapter(mAdapter);
mConcatAdapter = new ConcatAdapter(mPostAdapter, mCommentAdapter);
mRecyclerView.setAdapter(mConcatAdapter);
if (comments == null) {
fetchCommentsRespectRecommendedSort(false);
@ -552,7 +561,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
} else if (isFetchingComments) {
fetchCommentsRespectRecommendedSort(false);
} else {
mAdapter.addComments(comments, hasMoreChildren);
mCommentAdapter.addComments(comments, hasMoreChildren);
if (isLoadingMoreChildren) {
isLoadingMoreChildren = false;
fetchMoreComments();
@ -561,7 +570,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
}
}
mRecyclerView.setCacheManager(mAdapter);
mRecyclerView.setCacheManager(mPostAdapter);
mRecyclerView.setPlayerInitializer(order -> {
VolumeInfo volumeInfo = new VolumeInfo(true, 0f);
return new PlaybackInfo(INDEX_UNSET, TIME_UNSET, volumeInfo);
@ -661,28 +670,28 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
}
public void addComment(Comment comment) {
if (mAdapter != null) {
mAdapter.addComment(comment);
if (mCommentAdapter != null) {
mCommentAdapter.addComment(comment);
}
}
public void addChildComment(Comment comment, String parentFullname, int parentPosition) {
if (mAdapter != null) {
mAdapter.addChildComment(comment, parentFullname, parentPosition);
if (mCommentAdapter != null) {
mCommentAdapter.addChildComment(comment, parentFullname, parentPosition);
}
}
public void editComment(String commentAuthor, String commentContentMarkdown, int position) {
if (mAdapter != null) {
mAdapter.editComment(commentAuthor,
if (mCommentAdapter != null) {
mCommentAdapter.editComment(commentAuthor,
commentContentMarkdown,
position);
}
}
public void awardGiven(String awardsHTML, int awardCount, int position) {
if (mAdapter != null) {
mAdapter.giveAward(awardsHTML, awardCount, position);
if (mCommentAdapter != null) {
mCommentAdapter.giveAward(awardsHTML, awardCount, position);
}
}
@ -734,8 +743,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
}
public void saveComment(int position, boolean isSaved) {
if (mAdapter != null) {
mAdapter.setSaveComment(position, isSaved);
if (mCommentAdapter != null) {
mCommentAdapter.setSaveComment(position, isSaved);
}
}
@ -1000,7 +1009,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void onResume() {
super.onResume();
if (mAdapter != null && mRecyclerView != null) {
if (mCommentAdapter != null && mRecyclerView != null) {
mRecyclerView.onWindowVisibilityChanged(View.VISIBLE);
}
}
@ -1008,7 +1017,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void onPause() {
super.onPause();
if (mAdapter != null && mRecyclerView != null) {
if (mCommentAdapter != null && mRecyclerView != null) {
mRecyclerView.onWindowVisibilityChanged(View.GONE);
}
}
@ -1016,7 +1025,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
comments = mAdapter == null ? null : mAdapter.getVisibleComments();
comments = mCommentAdapter == null ? null : mCommentAdapter.getVisibleComments();
Bridge.saveInstanceState(this, outState);
}
@ -1086,18 +1095,23 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
setupMenu();
mAdapter = new CommentAndPostRecyclerViewAdapter(activity,
mPostAdapter = new PostDetailRecyclerViewAdapter(activity,
ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, mRetrofit, mOauthRetrofit, mGfycatRetrofit,
mRedgifsRetrofit, mRedditDataRoomDatabase, mGlide,
mWindowWidth, mAccessToken, mAccountName, mPost, mLocale,
mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences,
mNsfwAndSpoilerSharedPreferences, mExoCreator,
new CommentAndPostRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
mNsfwAndSpoilerSharedPreferences, mExoCreator, new PostDetailRecyclerViewAdapter.PostDetailRecyclerViewAdapterCallback() {
@Override
public void updatePost(Post post) {
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
}
});
mCommentAdapter = new CommentsRecyclerViewAdapter(activity,
ViewPostDetailFragment.this, mCustomThemeWrapper, mRetrofit, mOauthRetrofit,
mAccessToken, mAccountName, mPost, mLocale,
mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences,
new CommentsRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() {
@Override
public void retryFetchingComments() {
fetchCommentsRespectRecommendedSort(false);
@ -1111,7 +1125,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
fetchMoreComments();
}
});
mRecyclerView.setAdapter(mAdapter);
mConcatAdapter = new ConcatAdapter(mPostAdapter, mCommentAdapter);
mRecyclerView.setAdapter(mConcatAdapter);
if (mRespectSubredditRecommendedSortType) {
fetchCommentsRespectRecommendedSort(false);
@ -1123,7 +1138,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
ViewPostDetailFragment.this.children = moreChildrenFullnames;
hasMoreChildren = children.size() != 0;
mAdapter.addComments(expandedComments, hasMoreChildren);
mCommentAdapter.addComments(expandedComments, hasMoreChildren);
if (children.size() > 0) {
mRecyclerView.clearOnScrollListeners();
@ -1174,7 +1189,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void onParseCommentFailed() {
mAdapter.initiallyLoadCommentsFailed();
mCommentAdapter.initiallyLoadCommentsFailed();
}
});
}
@ -1236,8 +1251,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
private void fetchComments(boolean changeRefreshState, boolean checkSortState, String sortType) {
isFetchingComments = true;
mAdapter.setSingleComment(mSingleCommentId, isSingleCommentThreadMode);
mAdapter.initiallyLoading();
mCommentAdapter.setSingleComment(mSingleCommentId, isSingleCommentThreadMode);
mCommentAdapter.initiallyLoading();
String commentId = null;
if (isSingleCommentThreadMode) {
commentId = mSingleCommentId;
@ -1261,7 +1276,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
comments = expandedComments;
hasMoreChildren = children.size() != 0;
mAdapter.addComments(expandedComments, hasMoreChildren);
mCommentAdapter.addComments(expandedComments, hasMoreChildren);
if (children.size() > 0) {
mRecyclerView.clearOnScrollListeners();
@ -1326,7 +1341,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
return;
}
mAdapter.initiallyLoadCommentsFailed();
mCommentAdapter.initiallyLoadCommentsFailed();
if (changeRefreshState) {
isRefreshing = false;
}
@ -1351,7 +1366,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void onFetchMoreCommentSuccess(ArrayList<Comment> expandedComments, int childrenStartingIndex) {
hasMoreChildren = childrenStartingIndex < children.size();
mAdapter.addComments(expandedComments, hasMoreChildren);
mCommentAdapter.addComments(expandedComments, hasMoreChildren);
mChildrenStartingIndex = childrenStartingIndex;
isLoadingMoreChildren = false;
loadMoreChildrenSuccess = true;
@ -1361,13 +1376,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
public void onFetchMoreCommentFailed() {
isLoadingMoreChildren = false;
loadMoreChildrenSuccess = false;
mAdapter.loadMoreCommentsFailed();
mCommentAdapter.loadMoreCommentsFailed();
}
});
}
public void refresh(boolean fetchPost, boolean fetchComments) {
if (mAdapter != null && !isRefreshing) {
if (mPostAdapter != null && !isRefreshing) {
isRefreshing = true;
mChildrenStartingIndex = 0;
@ -1390,7 +1405,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void fetchPostSuccess(Post post) {
mPost = post;
mAdapter.updatePost(mPost);
mPostAdapter.updatePost(mPost);
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
isRefreshing = false;
setupMenu();
@ -1584,7 +1599,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void deleteSuccess() {
Toast.makeText(activity, R.string.delete_post_success, Toast.LENGTH_SHORT).show();
mAdapter.deleteComment(position);
mCommentAdapter.deleteComment(position);
}
@Override
@ -1604,7 +1619,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
new FetchRemovedComment.FetchRemovedCommentListener() {
@Override
public void fetchSuccess(Comment comment) {
mAdapter.editComment(comment.getAuthor(), comment.getCommentMarkdown(), position);
mCommentAdapter.editComment(comment.getAuthor(), comment.getCommentMarkdown(), position);
}
@Override
@ -1624,8 +1639,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
public void scrollToNextParentComment() {
if (mLinearLayoutManager != null) {
int currentPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
if (mAdapter != null) {
int nextParentPosition = mAdapter.getNextParentCommentPosition(currentPosition);
if (mCommentAdapter != null) {
int nextParentPosition = mCommentAdapter.getNextParentCommentPosition(currentPosition);
if (nextParentPosition < 0) {
return;
}
@ -1641,8 +1656,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
public void scrollToPreviousParentComment() {
if (mLinearLayoutManager != null) {
int currentPosition = mLinearLayoutManager.findFirstVisibleItemPosition();
if (mAdapter != null) {
int nextParentPosition = mAdapter.getPreviousParentCommentPosition(currentPosition);
if (mCommentAdapter != null) {
int nextParentPosition = mCommentAdapter.getPreviousParentCommentPosition(currentPosition);
if (nextParentPosition < 0) {
return;
}
@ -1668,7 +1683,9 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Override
public void fetchSuccess(Post post) {
mPost = post;
mAdapter.updatePost(post);
if (mPostAdapter != null) {
mPostAdapter.updatePost(post);
}
}
@Override
@ -1697,19 +1714,25 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
mMenu.findItem(R.id.action_save_view_post_detail_fragment).setIcon(mUnsavedIcon);
}
}
mAdapter.updatePost(mPost);
if (mPostAdapter != null) {
mPostAdapter.updatePost(mPost);
}
}
}
@Subscribe
public void onChangeNSFWBlurEvent(ChangeNSFWBlurEvent event) {
mAdapter.setBlurNsfwAndDoNotBlurNsfwInNsfwSubreddits(event.needBlurNSFW, event.doNotBlurNsfwInNsfwSubreddits);
if (mPostAdapter != null) {
mPostAdapter.setBlurNsfwAndDoNotBlurNsfwInNsfwSubreddits(event.needBlurNSFW, event.doNotBlurNsfwInNsfwSubreddits);
}
refreshAdapter();
}
@Subscribe
public void onChangeSpoilerBlurEvent(ChangeSpoilerBlurEvent event) {
mAdapter.setBlurSpoiler(event.needBlurSpoiler);
if (mPostAdapter != null) {
mPostAdapter.setBlurSpoiler(event.needBlurSpoiler);
}
refreshAdapter();
}
@ -1722,7 +1745,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
mRecyclerView.setAdapter(null);
mRecyclerView.setLayoutManager(null);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setAdapter(mConcatAdapter);
mRecyclerView.setLayoutManager(layoutManager);
if (previousPosition > 0) {
@ -1732,16 +1755,16 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
@Subscribe
public void onChangeNetworkStatusEvent(ChangeNetworkStatusEvent changeNetworkStatusEvent) {
if (mAdapter != null) {
if (mPostAdapter != null) {
String autoplay = mSharedPreferences.getString(SharedPreferencesUtils.VIDEO_AUTOPLAY, SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_NEVER);
String dataSavingMode = mSharedPreferences.getString(SharedPreferencesUtils.DATA_SAVING_MODE, SharedPreferencesUtils.DATA_SAVING_MODE_OFF);
boolean stateChanged = false;
if (autoplay.equals(SharedPreferencesUtils.VIDEO_AUTOPLAY_VALUE_ON_WIFI)) {
mAdapter.setAutoplay(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_WIFI);
mPostAdapter.setAutoplay(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_WIFI);
stateChanged = true;
}
if (dataSavingMode.equals(SharedPreferencesUtils.DATA_SAVING_MODE_ONLY_ON_CELLULAR_DATA)) {
mAdapter.setDataSavingMode(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_CELLULAR);
mPostAdapter.setDataSavingMode(changeNetworkStatusEvent.connectedNetwork == Utils.NETWORK_TYPE_CELLULAR);
stateChanged = true;
}