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 +++++++--- .../main/res/layout/item_comment_footer_error.xml | 25 ++++ .../res/layout/item_comment_footer_loading.xml | 14 +++ app/src/main/res/layout/item_footer_error.xml | 7 +- app/src/main/res/layout/item_footer_loading.xml | 1 - app/src/main/res/values/strings.xml | 1 - 10 files changed, 199 insertions(+), 86 deletions(-) create mode 100644 app/src/main/res/layout/item_comment_footer_error.xml create mode 100644 app/src/main/res/layout/item_comment_footer_loading.xml (limited to 'app/src/main') 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(); } }); } diff --git a/app/src/main/res/layout/item_comment_footer_error.xml b/app/src/main/res/layout/item_comment_footer_error.xml new file mode 100644 index 00000000..39195989 --- /dev/null +++ b/app/src/main/res/layout/item_comment_footer_error.xml @@ -0,0 +1,25 @@ + + + + + +