From 2e72f0d4aa029bad73e1200d22fc6bc1d6df185a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 22 Feb 2019 17:00:09 +0800 Subject: Load parent-level comments using pagination. Only load comments of comments once more. --- .../infinityforreddit/CommentAdapter.java | 17 ++++---- .../infinityforreddit/CommentDataSource.java | 20 ++++------ .../infinityforreddit/FetchComment.java | 46 ++++++++++++++++------ .../infinityforreddit/MainActivity.java | 2 +- .../infinityforreddit/ParseComment.java | 14 ++----- .../infinityforreddit/ViewPostDetailActivity.java | 40 ++++++++++++++----- .../main/res/layout/activity_view_post_detail.xml | 1 + 7 files changed, 85 insertions(+), 55 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java index 1ec848ad..ca0ec181 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAdapter.java @@ -1,16 +1,10 @@ package ml.docilealligator.infinityforreddit; -import androidx.paging.PagedListAdapter; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.ColorFilter; import android.net.Uri; -import androidx.annotation.NonNull; -import androidx.browser.customtabs.CustomTabsIntent; -import androidx.core.content.ContextCompat; -import androidx.recyclerview.widget.DiffUtil; -import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -21,9 +15,16 @@ import android.widget.Toast; import com.multilevelview.models.RecyclerViewItem; +import java.util.ArrayList; import java.util.List; import java.util.Locale; +import androidx.annotation.NonNull; +import androidx.browser.customtabs.CustomTabsIntent; +import androidx.core.content.ContextCompat; +import androidx.paging.PagedListAdapter; +import androidx.recyclerview.widget.DiffUtil; +import androidx.recyclerview.widget.RecyclerView; import butterknife.BindView; import butterknife.ButterKnife; import retrofit2.Retrofit; @@ -128,7 +129,7 @@ public class CommentAdapter extends PagedListAdapter commentData, - String parentId, String commaSeparatedChildren) { + String parentId, ArrayList children) { commentItem.addChildren((List) commentData); ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar .setVisibility(View.GONE); @@ -140,7 +141,7 @@ public class CommentAdapter extends PagedListAdapter commentData, - String parentId, String commaSeparatedChildren) { + String parentId, String children) { commentItem.addChildren((List) commentData); ((CommentViewHolder) viewHolder).loadMoreCommentsProgressBar .setVisibility(View.GONE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java index 99084dbf..a7fd32cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentDataSource.java @@ -1,17 +1,11 @@ package ml.docilealligator.infinityforreddit; -import androidx.lifecycle.MutableLiveData; -import androidx.paging.PageKeyedDataSource; -import androidx.annotation.NonNull; -import android.util.Log; - -import java.util.ArrayList; -import java.util.List; import java.util.Locale; +import androidx.annotation.NonNull; +import androidx.lifecycle.MutableLiveData; +import androidx.paging.PageKeyedDataSource; import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; import retrofit2.Retrofit; public class CommentDataSource extends PageKeyedDataSource { @@ -69,7 +63,7 @@ public class CommentDataSource extends PageKeyedDataSource { RedditAPI api = retrofit.create(RedditAPI.class); Call comments = api.getComments(subredditNamePrefixed, article, comment); - comments.enqueue(new Callback() { + /*comments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if(response.isSuccessful()) { @@ -104,7 +98,7 @@ public class CommentDataSource extends PageKeyedDataSource { public void onFailure(@NonNull Call call, @NonNull Throwable t) { initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error fetching comment")); } - }); + });*/ } @Override @@ -126,7 +120,7 @@ public class CommentDataSource extends PageKeyedDataSource { RedditAPI api = retrofit.create(RedditAPI.class); Call moreChildrenBasicInfo = api.getMoreChildren(mParentId, params.key); - moreChildrenBasicInfo.enqueue(new Callback() { + /*moreChildrenBasicInfo.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if(response.isSuccessful()) { @@ -182,7 +176,7 @@ public class CommentDataSource extends PageKeyedDataSource { String errorMessage = t == null ? "unknown error" : t.getMessage(); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); } - }); + });*/ } void retry() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java index c4a4ea00..a9151b50 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchComment.java @@ -15,12 +15,12 @@ import retrofit2.Retrofit; class FetchComment { interface FetchCommentListener { void onFetchCommentSuccess(List commentData, - String parentId, String commaSeparatedChildren); + String parentId, ArrayList children); void onFetchCommentFailed(); } interface FetchMoreCommentListener { - void onFetchMoreCommentSuccess(List commentData); + void onFetchMoreCommentSuccess(List commentData, int childrenStartingIndex); void onFetchMoreCommentFailed(); } @@ -43,9 +43,9 @@ class FetchComment { new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(List commentData, - String parentId, String commaSeparatedChildren) { + String parentId, ArrayList children) { fetchCommentListener.onFetchCommentSuccess(commentData, parentId, - commaSeparatedChildren); + children); } @Override @@ -69,9 +69,25 @@ class FetchComment { } static void fetchMoreComment(Retrofit retrofit, String subredditNamePrefixed, String mParentId, - String children, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { + ArrayList allChildren, int startingIndex, Locale locale, + FetchMoreCommentListener fetchMoreCommentListener) { + StringBuilder stringBuilder = new StringBuilder(); + for(int i = 0; i < 100; i++) { + if(allChildren.size() <= startingIndex + i) { + break; + } + stringBuilder.append(allChildren.get(startingIndex + i)).append(","); + } + + if(stringBuilder.length() == 0) { + return; + } + + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + //final int finalStartingIndex = startingIndex + 100; + RedditAPI api = retrofit.create(RedditAPI.class); - Call moreChildrenBasicInfo = api.getMoreChildren(mParentId, children); + Call moreChildrenBasicInfo = api.getMoreChildren(mParentId, stringBuilder.toString()); moreChildrenBasicInfo.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { @@ -88,8 +104,11 @@ class FetchComment { 0, new ParseComment.ParseCommentListener() { @Override public void onParseCommentSuccess(List commentData, String parentId, - String commaSeparatedChildren) { - fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData); + ArrayList children) { + fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData, startingIndex + 100); + /*fetchMoreComment(retrofit, subredditNamePrefixed, + mParentId, allChildren, finalStartingIndex, + locale, fetchMoreCommentListener);*/ } @Override @@ -138,12 +157,13 @@ class FetchComment { fetchComment(retrofit, subredditNamePrefixed, article, comment, locale, isPost, parentDepth, new FetchCommentListener() { @Override - public void onFetchCommentSuccess(List commentData, String parentId, String commaSeparatedChildren) { - if(!commaSeparatedChildren.equals("")) { - fetchMoreComment(retrofit, subredditNamePrefixed, parentId, commaSeparatedChildren, - locale, new FetchMoreCommentListener() { + public void onFetchCommentSuccess(List commentData, String parentId, ArrayList children) { + if(children.size() != 0) { + fetchMoreComment(retrofit, subredditNamePrefixed, parentId, children, + 0, locale, new FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(List moreCommentData) { + public void onFetchMoreCommentSuccess(List moreCommentData, + int childrenStartingIndex) { ((ArrayList)commentData).addAll((ArrayList) moreCommentData); fetchAllCommentListener.onFetchAllCommentSuccess(commentData); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index 3aec9917..7591885f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -147,7 +147,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onSearchViewClosed() { transparentOverlay.setVisibility(View.GONE); - getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); + getWindow().setStatusBarColor(Color.TRANSPARENT); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 92b5f10b..970c9ed6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -15,7 +15,7 @@ import java.util.Locale; class ParseComment { interface ParseCommentListener { - void onParseCommentSuccess(List commentData, String parentId, String commaSeparatedChildren); + void onParseCommentSuccess(List commentData, String parentId, ArrayList children); void onParseCommentFailed(); } @@ -64,7 +64,7 @@ class ParseComment { private JSONArray comments; private ArrayList commentData; private ArrayList newcommentData; - private StringBuilder commaSeparatedChildren; + private ArrayList children; private Locale locale; private int parentDepth; private ParseCommentListener parseCommentListener; @@ -76,7 +76,7 @@ class ParseComment { this.comments = comments; this.commentData = commentData; newcommentData = new ArrayList<>(); - commaSeparatedChildren = new StringBuilder(); + children = new ArrayList<>(); this.locale = locale; this.parentDepth = parentDepth; parseFailed = false; @@ -87,7 +87,6 @@ class ParseComment { protected Void doInBackground(Void... voids) { try { int actualCommentLength; - ArrayList children = new ArrayList<>(); if(comments.length() == 0) { return null; @@ -104,11 +103,6 @@ class ParseComment { children.add(childrenArray.getString(i)); } - for(String c : children) { - commaSeparatedChildren.append(c).append(","); - } - commaSeparatedChildren.deleteCharAt(commaSeparatedChildren.length() - 1); - actualCommentLength = comments.length() - 1; } else { actualCommentLength = comments.length(); @@ -156,7 +150,7 @@ class ParseComment { protected void onPostExecute(Void aVoid) { if(!parseFailed) { commentData.addAll(newcommentData); - parseCommentListener.onParseCommentSuccess(commentData, parentId, commaSeparatedChildren.toString()); + parseCommentListener.onParseCommentSuccess(commentData, parentId, children); } else { parseCommentListener.onParseCommentFailed(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index df66d631..b9a75a40 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -9,6 +9,7 @@ import android.net.Uri; import android.os.Bundle; import android.view.MenuItem; import android.view.View; +import android.view.ViewTreeObserver; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; @@ -44,6 +45,7 @@ import androidx.browser.customtabs.CustomTabsIntent; import androidx.cardview.widget.CardView; import androidx.coordinatorlayout.widget.CoordinatorLayout; import androidx.core.content.ContextCompat; +import androidx.core.widget.NestedScrollView; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import butterknife.BindView; @@ -66,9 +68,12 @@ public class ViewPostDetailActivity extends AppCompatActivity { private Post mPost; - private String mCommaSeparatedChildren; + private boolean isLoadingMoreChildren = false; + private ArrayList children; + private int mChildrenStartingIndex = 0; @BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout; + @BindView(R.id.nested_scroll_view_view_post_detail_activity) NestedScrollView mNestedScrollView; @BindView(R.id.subreddit_icon_name_linear_layout_view_post_detail) LinearLayout mSubredditIconNameLinearLayout; @BindView(R.id.subreddit_icon_circle_image_view_view_post_detail) AspectRatioGifImageView mSubredditIconGifImageView; @BindView(R.id.subreddit_text_view_view_post_detail) TextView mSubredditTextView; @@ -446,8 +451,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { null, getResources().getConfiguration().locale, true, 0, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(List commentData, - String parentId, String commaSeparatedChildren) { - mCommaSeparatedChildren = commaSeparatedChildren; + String parentId, ArrayList children) { + ViewPostDetailActivity.this.children = children; mCommentProgressbar.setVisibility(View.GONE); if (commentData.size() > 0) { mAdapter = new CommentMultiLevelRecyclerViewAdapter( @@ -461,9 +466,19 @@ public class ViewPostDetailActivity extends AppCompatActivity { mRecyclerView.setAdapter(mAdapter); mCommentCardView.setVisibility(View.VISIBLE); - if(!mCommaSeparatedChildren.equals("")) { - fetchMoreComment(); - } + mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { + @Override + public void onScrollChanged() { + if(!isLoadingMoreChildren) { + View view = mNestedScrollView.getChildAt(mNestedScrollView.getChildCount() - 1); + int diff = view.getBottom() - (mNestedScrollView.getHeight() + + mNestedScrollView.getScrollY()); + if(diff == 0) { + fetchMoreComment(mChildrenStartingIndex); + } + } + } + }); } else { mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); glide.load(R.drawable.no_comment_indicator).into(mNoCommentImageView); @@ -478,18 +493,23 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); } - private void fetchMoreComment() { + private void fetchMoreComment(int startingIndex) { + isLoadingMoreChildren = true; FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getFullName(), - mCommaSeparatedChildren, getResources().getConfiguration().locale, new FetchComment.FetchMoreCommentListener() { + children, startingIndex, getResources().getConfiguration().locale, + new FetchComment.FetchMoreCommentListener() { @Override - public void onFetchMoreCommentSuccess(List commentData) { + public void onFetchMoreCommentSuccess(List commentData, int childrenStartingIndex) { mAdapter.addComments((ArrayList) commentData); + mChildrenStartingIndex = childrenStartingIndex; + isLoadingMoreChildren = false; } @Override public void onFetchMoreCommentFailed() { + isLoadingMoreChildren = false; Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.load_more_comment_failed, Snackbar.LENGTH_INDEFINITE); - snackbar.setAction(R.string.retry, view -> fetchMoreComment()); + snackbar.setAction(R.string.retry, view -> fetchMoreComment(startingIndex)); snackbar.show(); } }); diff --git a/app/src/main/res/layout/activity_view_post_detail.xml b/app/src/main/res/layout/activity_view_post_detail.xml index 0f7c2002..1a0be45d 100644 --- a/app/src/main/res/layout/activity_view_post_detail.xml +++ b/app/src/main/res/layout/activity_view_post_detail.xml @@ -8,6 +8,7 @@ tools:context=".ViewPostDetailActivity"> -- cgit v1.2.3