Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/assets/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -394,6 +411,7 @@ private void makeNextRequest(final Context context) {
mUrlsToDownload.getFirst(),
mAllUrls.size() == 1,
mUrlsToDownload.getFirst(),
false,
mUser,
mSession,
mDownloadStrategy,
Expand All @@ -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()) {
Expand Down Expand Up @@ -607,7 +671,8 @@ public void onCommentListingRequestPostDownloaded(final RedditPreparedPost post)

@Override
public void onCommentListingRequestAllItemsDownloaded(
final ArrayList<RedditCommentListItem> items) {
final ArrayList<RedditCommentListItem> items,
@Nullable final String after) {

mCommentListingManager.addComments(items);

Expand All @@ -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();
Expand All @@ -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) {

Expand Down Expand Up @@ -663,9 +741,6 @@ public void onCommentListingRequestAllItemsDownloaded(
}

mCommentListingManager.setLoadingVisible(false);

} else {
makeNextRequest(getActivity());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -104,6 +106,7 @@ public CommentListingRequest(
mCommentListingURL = commentListingURL;
mParsePostSelfText = parsePostSelfText;
mUrl = url;
mIsLaterPage = isLaterPage;
mUser = user;
mSession = session;
mDownloadStrategy = downloadStrategy;
Expand All @@ -127,7 +130,13 @@ public interface Listener {

void onCommentListingRequestPostDownloaded(RedditPreparedPost post);

void onCommentListingRequestAllItemsDownloaded(ArrayList<RedditCommentListItem> 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<RedditCommentListItem> items,
@Nullable String after);
}

private void onThingDownloaded(
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -230,7 +246,7 @@ private void onThingDownloaded(
}

AndroidCommon.runOnUiThread(()
-> mListener.onCommentListingRequestAllItemsDownloaded(items));
-> mListener.onCommentListingRequestAllItemsDownloaded(items, after));
}

@NonNull
Expand Down
Loading