diff --git a/src/main/assets/changelog.txt b/src/main/assets/changelog.txt index e73149095..e66d686a2 100644 --- a/src/main/assets/changelog.txt +++ b/src/main/assets/changelog.txt @@ -3,6 +3,7 @@ Option to set the navigation bar opacity Fixed issue where post titles are cut off Scrolling performance optimization Reduced memory usage and allocation count +More of a user's comments are loaded when scrolling to the end of the list Assorted minor bugfixes 119/1.27 diff --git a/src/main/java/org/quantumbadger/redreader/fragments/CommentListingFragment.java b/src/main/java/org/quantumbadger/redreader/fragments/CommentListingFragment.java index 7e8ec428a..5d723a6db 100644 --- a/src/main/java/org/quantumbadger/redreader/fragments/CommentListingFragment.java +++ b/src/main/java/org/quantumbadger/redreader/fragments/CommentListingFragment.java @@ -36,6 +36,7 @@ import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.view.OneShotPreDrawListener; @@ -117,6 +118,11 @@ public class CommentListingFragment extends RRFragment private Integer mPreviousFirstVisibleItemPosition; private int mParentJumpCount = 0; + // For listings which continue on further pages, such as a user's comments: the value to + // request the next page with, or null if there are no more pages + @Nullable private String mNextPageAfter = null; + private boolean mNextPageRequested = false; + public CommentListingFragment( final AppCompatActivity parent, final Bundle savedInstanceState, @@ -187,6 +193,17 @@ public CommentListingFragment( mRecyclerView.setItemAnimator(null); + // Also called when the visible items change after a layout + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled( + @NonNull final RecyclerView recyclerView, + final int dx, + final int dy) { + onLoadMoreItemsCheck(); + } + }); + if(!PrefsUtility.pref_appearance_comments_show_floating_toolbar()) { mFloatingToolbar = null; @@ -394,6 +411,7 @@ private void makeNextRequest(final Context context) { mUrlsToDownload.getFirst(), mAllUrls.size() == 1, mUrlsToDownload.getFirst(), + false, mUser, mSession, mDownloadStrategy, @@ -402,6 +420,52 @@ private void makeNextRequest(final Context context) { } } + private boolean isPagedListing() { + return mAllUrls.size() == 1 + && mAllUrls.get(0).pathType() == RedditURLParser.USER_COMMENT_LISTING_URL; + } + + private void onLoadMoreItemsCheck() { + + General.checkThisIsUIThread(); + + if(mNextPageAfter == null || mNextPageRequested) { + return; + } + + final LinearLayoutManager layoutManager + = (LinearLayoutManager)mRecyclerView.getLayoutManager(); + + final boolean nearEnd = layoutManager.getItemCount() + - layoutManager.findLastVisibleItemPosition() < 20; + + // When restoring the scroll position, it may be on a later page + final boolean restoringPosition = mPreviousFirstVisibleItemPosition != null + && layoutManager.getItemCount() <= mPreviousFirstVisibleItemPosition; + + if(!nearEnd && !restoringPosition) { + return; + } + + mNextPageRequested = true; + mCommentListingManager.setLoadingVisible(true); + + new CommentListingRequest( + getActivity(), + this, + (BaseActivity)getActivity(), + mAllUrls.get(0), + false, + mAllUrls.get(0).asUserCommentListURL().after(mNextPageAfter), + true, + mUser, + mSession, + // The pages are fetched using the ID of the last comment, so a cached page + // still follows on from the comments above it + DownloadStrategyIfNotCached.INSTANCE, + this); + } + @Override public void onCommentClicked(final RedditCommentView view) { switch(PrefsUtility.pref_behaviour_actions_comment_tap()) { @@ -607,7 +671,8 @@ public void onCommentListingRequestPostDownloaded(final RedditPreparedPost post) @Override public void onCommentListingRequestAllItemsDownloaded( - final ArrayList items) { + final ArrayList items, + @Nullable final String after) { mCommentListingManager.addComments(items); @@ -620,7 +685,13 @@ public void onCommentListingRequestAllItemsDownloaded( mFloatingToolbar.startAnimation(animation); } - mUrlsToDownload.removeFirst(); + if(mNextPageRequested) { + mNextPageRequested = false; + } else { + mUrlsToDownload.removeFirst(); + } + + mNextPageAfter = isPagedListing() ? after : null; final LinearLayoutManager layoutManager = (LinearLayoutManager)mRecyclerView.getLayoutManager(); @@ -635,7 +706,14 @@ public void onCommentListingRequestAllItemsDownloaded( mPreviousFirstVisibleItemPosition = null; } - if(mUrlsToDownload.isEmpty()) { + if(!mUrlsToDownload.isEmpty()) { + makeNextRequest(getActivity()); + + } else if(mNextPageAfter != null) { + mCommentListingManager.setLoadingVisible(false); + onLoadMoreItemsCheck(); + + } else { if(mCommentListingManager.getCommentCount() == 0) { @@ -663,9 +741,6 @@ public void onCommentListingRequestAllItemsDownloaded( } mCommentListingManager.setLoadingVisible(false); - - } else { - makeNextRequest(getActivity()); } } diff --git a/src/main/java/org/quantumbadger/redreader/reddit/CommentListingRequest.java b/src/main/java/org/quantumbadger/redreader/reddit/CommentListingRequest.java index 05f25eb8e..fd86be5b4 100644 --- a/src/main/java/org/quantumbadger/redreader/reddit/CommentListingRequest.java +++ b/src/main/java/org/quantumbadger/redreader/reddit/CommentListingRequest.java @@ -80,6 +80,7 @@ public class CommentListingRequest { private final boolean mParsePostSelfText; private final CacheManager mCacheManager; private final RedditURLParser.RedditURL mUrl; + private final boolean mIsLaterPage; private final RedditAccount mUser; private final UUID mSession; private final DownloadStrategy mDownloadStrategy; @@ -93,6 +94,7 @@ public CommentListingRequest( final RedditURLParser.RedditURL commentListingURL, final boolean parsePostSelfText, final RedditURLParser.RedditURL url, + final boolean isLaterPage, final RedditAccount user, final UUID session, final DownloadStrategy downloadStrategy, @@ -104,6 +106,7 @@ public CommentListingRequest( mCommentListingURL = commentListingURL; mParsePostSelfText = parsePostSelfText; mUrl = url; + mIsLaterPage = isLaterPage; mUser = user; mSession = session; mDownloadStrategy = downloadStrategy; @@ -127,7 +130,13 @@ public interface Listener { void onCommentListingRequestPostDownloaded(RedditPreparedPost post); - void onCommentListingRequestAllItemsDownloaded(ArrayList items); + /** + * @param after If this is a listing of comments which continues on another page, the + * value to request the next page with. Otherwise null. + */ + void onCommentListingRequestAllItemsDownloaded( + ArrayList items, + @Nullable String after); } private void onThingDownloaded( @@ -138,7 +147,8 @@ private void onThingDownloaded( ) { String parentPostAuthor = null; - if(mActivity instanceof SessionChangeListener) { + // The session is that of the first page of the listing + if(mActivity instanceof SessionChangeListener && !mIsLaterPage) { ((SessionChangeListener)mActivity).onSessionChanged( session, SessionChangeListener.SessionChangeType.COMMENTS, @@ -156,11 +166,14 @@ private void onThingDownloaded( AndroidCommon.runOnUiThread(mListener::onCommentListingRequestParseStart); @NonNull final RedditListing commentListing; + @Nullable final String after; if(thingResponse instanceof RedditThingResponse.Single) { commentListing = ((RedditThing.Listing)((RedditThingResponse.Single) thingResponse) .getThing()).getData(); + after = commentListing.getAfter(); + } else { final RedditThingResponse.Multiple multiple = (RedditThingResponse.Multiple) thingResponse; @@ -199,6 +212,9 @@ private void onThingDownloaded( commentListing = ((RedditThing.Listing)((RedditThingResponse.Multiple) thingResponse) .getThings().get(1)).getData(); + + // The comments on a post are fetched using "load more" items instead + after = null; } // Download comments @@ -230,7 +246,7 @@ private void onThingDownloaded( } AndroidCommon.runOnUiThread(() - -> mListener.onCommentListingRequestAllItemsDownloaded(items)); + -> mListener.onCommentListingRequestAllItemsDownloaded(items, after)); } @NonNull