From 33db4809e4376f91a566037f16694f8f19639e02 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 12 Dec 2018 19:23:38 +0800 Subject: Use notifyItemRangeInserted when appropriate in order to avoid reloading the whole dataset. Change bur transformation parameter for NSFW preview. --- .../ml/docilealligator/infinityforreddit/PostFragment.java | 5 ++--- .../infinityforreddit/PostPaginationScrollListener.java | 12 ++++-------- .../infinityforreddit/PostRecyclerViewAdapter.java | 10 ++++++++-- .../ml/docilealligator/infinityforreddit/PostViewModel.java | 10 +++++++++- .../infinityforreddit/ViewPostDetailActivity.java | 2 +- 5 files changed, 24 insertions(+), 15 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 511949b2..0b856945 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -41,10 +41,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { static final String SUBREDDIT_NAME_KEY = "SNK"; static final String IS_BEST_POST_KEY = "IBPK"; - private static final String POST_DATA_PARCELABLE_STATE = "PDPS"; private static final String LAST_ITEM_STATE = "LIS"; private static final String LOADING_STATE_STATE = "LSS"; private static final String LOAD_SUCCESS_STATE = "LOSS"; + private static final String IS_REFRESH_STATE = "IRS"; private CoordinatorLayout mCoordinatorLayout; private RecyclerView mPostRecyclerView; @@ -53,7 +53,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private LinearLayout mFetchPostErrorLinearLayout; private ImageView mFetchPostErrorImageView; - private ArrayList mPostData; private String mLastItem; private PaginationSynchronizer mPaginationSynchronizer; @@ -145,11 +144,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostViewModel.getPosts().observe(this, new Observer>() { @Override public void onChanged(@Nullable ArrayList posts) { - mAdapter.changeDataSet(posts); if(posts == null) { Log.i("datachange", Integer.toString(0)); } else { Log.i("datachange", Integer.toString(posts.size())); + mAdapter.changeDataSet(posts); } } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java index 29b1776e..0c11e305 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java @@ -104,11 +104,9 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { if(response.isSuccessful()) { ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { @Override - public void onParsePostSuccess(ArrayList newPostData, String lastItem) { + public void onParsePostSuccess(ArrayList newPosts, String lastItem) { if(mPostViewModel != null) { - ArrayList posts = mPostViewModel.getPosts().getValue(); - posts.addAll(newPostData); - mPostViewModel.setPosts(posts); + mPostViewModel.addPosts(newPosts); mLastItem = lastItem; mPaginationSynchronizer.notifyLastItemChanged(lastItem); @@ -154,11 +152,9 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { if(response.isSuccessful()) { ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { @Override - public void onParsePostSuccess(ArrayList newPostData, String lastItem) { + public void onParsePostSuccess(ArrayList newPosts, String lastItem) { if(mPostViewModel != null) { - ArrayList posts = mPostViewModel.getPosts().getValue(); - posts.addAll(newPostData); - mPostViewModel.setPosts(posts); + mPostViewModel.addPosts(newPosts); mLastItem = lastItem; mPaginationSynchronizer.notifyLastItemChanged(lastItem); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 9ded7a44..a3a7e504 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -61,6 +61,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter posts) { mPostData = posts; - notifyDataSetChanged(); + if(dataSize == 0 || posts.size() <= dataSize) { + notifyDataSetChanged(); + } else { + notifyItemRangeInserted(dataSize, posts.size() - dataSize); + } + dataSize = posts.size(); } class DataViewHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index f665380f..af6c1f31 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -6,7 +6,7 @@ import android.arch.lifecycle.ViewModel; import java.util.ArrayList; -class PostViewModel extends ViewModel { +public class PostViewModel extends ViewModel { private MutableLiveData> posts = new MutableLiveData<>(); LiveData> getPosts() { @@ -19,4 +19,12 @@ class PostViewModel extends ViewModel { void setPosts(ArrayList posts) { this.posts.postValue(posts); } + + void addPosts(ArrayList newPosts) { + ArrayList posts = this.posts.getValue(); + if(posts != null) { + posts.addAll(newPosts); + this.posts.postValue(posts); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 427b12ed..27d152d1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -550,7 +550,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { }); if(mPost.isNSFW()) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 3))) + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2))) .into(mImageView); } else { imageRequestBuilder.into(mImageView); -- cgit v1.2.3