From 08bfa254bcbb2b9b15f0db3c772664427cbd606a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 27 Jun 2019 15:17:37 +0800 Subject: Loading more comments in ViewPostDetailActivity is back. Display a progress bar when loading more comments. Display an error view when loading more comments failed. --- .../CommentRecyclerViewAdapter.java | 129 +++++++++++++++------ .../infinityforreddit/PostRecyclerViewAdapter.java | 7 +- .../SubredditListingRecyclerViewAdapter.java | 19 ++- .../UserListingRecyclerViewAdapter.java | 19 ++- .../infinityforreddit/ViewPostDetailActivity.java | 63 +++++++--- 5 files changed, 157 insertions(+), 80 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java index 65f385d7..b03f7930 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentRecyclerViewAdapter.java @@ -10,6 +10,7 @@ import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; @@ -52,7 +53,9 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter mVisibleComments; private String mSubredditNamePrefixed; private Locale mLocale; - private UpdatePostInPostFragmentCallback mUpdatePostInPostFragmentCallback; + private CommentRecyclerViewAdapterCallback mCommentRecyclerViewAdapterCallback; private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; private boolean isInitiallyLoading; private boolean isInitiallyLoadingFailed; + private boolean mHasMoreComments; + private boolean loadMoreCommentsFailed; - interface UpdatePostInPostFragmentCallback { + interface CommentRecyclerViewAdapterCallback { void updatePost(Post post); + void retryFetchingMoreComments(); } CommentRecyclerViewAdapter(Activity activity, Retrofit retrofit, Retrofit oauthRetrofit, RequestManager glide, SharedPreferences sharedPreferences, Post post, String subredditNamePrefixed, Locale locale, LoadSubredditIconAsyncTask loadSubredditIconAsyncTask, - UpdatePostInPostFragmentCallback updatePostInPostFragmentCallback) { + CommentRecyclerViewAdapterCallback commentRecyclerViewAdapterCallback) { mActivity = activity; mRetrofit = retrofit; mOauthRetrofit = oauthRetrofit; @@ -86,9 +92,11 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter comments) { + void addComments(ArrayList comments, boolean hasMoreComments) { if(mVisibleComments.size() == 0) { isInitiallyLoading = false; isInitiallyLoadingFailed = false; @@ -502,6 +525,15 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter mCommentRecyclerViewAdapterCallback.retryFetchingMoreComments()); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 655f9d81..1541a233 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -608,9 +608,8 @@ class PostRecyclerViewAdapter extends PagedListAdapter children; private int mChildrenStartingIndex = 0; + private boolean loadMoreChildrenSuccess = true; + private LinearLayoutManager mLinearLayoutManager; private CommentRecyclerViewAdapter mAdapter; private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask; @@ -91,8 +93,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { mGlide = Glide.with(this); mLocale = getResources().getConfiguration().locale; - mRecyclerView.setNestedScrollingEnabled(false); - mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); + mLinearLayoutManager = new LinearLayoutManager(this); + mRecyclerView.setLayoutManager(mLinearLayoutManager); if(savedInstanceState == null) { orientation = getResources().getConfiguration().orientation; @@ -105,7 +107,20 @@ public class ViewPostDetailActivity extends AppCompatActivity { mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mGlide, mSharedPreferences, mPost, mPost.getSubredditNamePrefixed(), mLocale, mLoadSubredditIconAsyncTask, - post -> EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); + new CommentRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { + @Override + public void updatePost(Post post) { + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + } + + @Override + public void retryFetchingMoreComments() { + isLoadingMoreChildren = false; + loadMoreChildrenSuccess = true; + + fetchMoreComments(); + } + }); mRecyclerView.setAdapter(mAdapter); if(savedInstanceState != null) { @@ -133,16 +148,26 @@ public class ViewPostDetailActivity extends AppCompatActivity { String parentId, ArrayList children) { ViewPostDetailActivity.this.children = children; - mAdapter.addComments(expandedComments); + mAdapter.addComments(expandedComments, children.size() != 0); - mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); + if(children.size() > 0) { + mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if(!isLoadingMoreChildren && loadMoreChildrenSuccess) { + int visibleItemCount = mLinearLayoutManager.getChildCount(); + int totalItemCount = mLinearLayoutManager.getItemCount(); + int firstVisibleItemPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); - } - }); + if ((visibleItemCount + firstVisibleItemPosition >= totalItemCount) && firstVisibleItemPosition >= 0) { + fetchMoreComments(); + } + } + } + }); + } /*mCommentProgressbar.setVisibility(View.GONE); @@ -154,7 +179,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { int diff = view.getBottom() - (mNestedScrollView.getHeight() + mNestedScrollView.getScrollY()); if(diff == 0) { - fetchMoreComment(mChildrenStartingIndex); + fetchMoreComments(mChildrenStartingIndex); } } }); @@ -162,7 +187,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { mAdapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mGlide, mSharedPreferences, mPost, - mPost.getSubredditNamePrefixed(), mLocale, new CommentRecyclerViewAdapter.UpdatePostInPostFragmentCallback() { + mPost.getSubredditNamePrefixed(), mLocale, new CommentRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @Override public void updatePost(Post post) { EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); @@ -184,27 +209,27 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); } - private void fetchMoreComment(int startingIndex) { - if(isLoadingMoreChildren) { + void fetchMoreComments() { + if(isLoadingMoreChildren || !loadMoreChildrenSuccess) { return; } isLoadingMoreChildren = true; - FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), children, startingIndex, + FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), children, mChildrenStartingIndex, 0, mLocale, new FetchComment.FetchMoreCommentListener() { @Override public void onFetchMoreCommentSuccess(ArrayList expandedComments, int childrenStartingIndex) { - mAdapter.addComments(expandedComments); + mAdapter.addComments(expandedComments, childrenStartingIndex < children.size()); mChildrenStartingIndex = childrenStartingIndex; isLoadingMoreChildren = false; + loadMoreChildrenSuccess = true; } @Override public void onFetchMoreCommentFailed() { isLoadingMoreChildren = false; - Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.post_load_more_comments_failed, Snackbar.LENGTH_INDEFINITE); - snackbar.setAction(R.string.retry, view -> fetchMoreComment(startingIndex)); - snackbar.show(); + loadMoreChildrenSuccess = false; + mAdapter.loadMoreCommentsFailed(); } }); } -- cgit v1.2.3