Use notifyItemRangeInserted when appropriate in order to avoid reloading the whole dataset. Change bur transformation parameter for NSFW preview.

This commit is contained in:
Alex Ning 2018-12-12 19:23:38 +08:00
parent cdcb38db51
commit 33db4809e4
5 changed files with 24 additions and 15 deletions

View File

@ -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<Post> mPostData;
private String mLastItem;
private PaginationSynchronizer mPaginationSynchronizer;
@ -145,11 +144,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
mPostViewModel.getPosts().observe(this, new Observer<ArrayList<Post>>() {
@Override
public void onChanged(@Nullable ArrayList<Post> posts) {
mAdapter.changeDataSet(posts);
if(posts == null) {
Log.i("datachange", Integer.toString(0));
} else {
Log.i("datachange", Integer.toString(posts.size()));
mAdapter.changeDataSet(posts);
}
}
});

View File

@ -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<Post> newPostData, String lastItem) {
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
if(mPostViewModel != null) {
ArrayList<Post> 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<Post> newPostData, String lastItem) {
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) {
if(mPostViewModel != null) {
ArrayList<Post> posts = mPostViewModel.getPosts().getValue();
posts.addAll(newPostData);
mPostViewModel.setPosts(posts);
mPostViewModel.addPosts(newPosts);
mLastItem = lastItem;
mPaginationSynchronizer.notifyLastItemChanged(lastItem);

View File

@ -61,6 +61,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
private static final int VIEW_TYPE_DATA = 0;
private static final int VIEW_TYPE_LOADING = 1;
private int dataSize;
PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, SharedPreferences sharedPreferences, PaginationSynchronizer paginationSynchronizer, boolean hasMultipleSubreddits) {
if(context != null) {
@ -505,7 +506,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
});
if(post.isNSFW()) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3)))
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2)))
.into(((DataViewHolder) holder).imageView);
} else {
imageRequestBuilder.into(((DataViewHolder) holder).imageView);
@ -526,7 +527,12 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
void changeDataSet(ArrayList<Post> 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 {

View File

@ -6,7 +6,7 @@ import android.arch.lifecycle.ViewModel;
import java.util.ArrayList;
class PostViewModel extends ViewModel {
public class PostViewModel extends ViewModel {
private MutableLiveData<ArrayList<Post>> posts = new MutableLiveData<>();
LiveData<ArrayList<Post>> getPosts() {
@ -19,4 +19,12 @@ class PostViewModel extends ViewModel {
void setPosts(ArrayList<Post> posts) {
this.posts.postValue(posts);
}
void addPosts(ArrayList<Post> newPosts) {
ArrayList<Post> posts = this.posts.getValue();
if(posts != null) {
posts.addAll(newPosts);
this.posts.postValue(posts);
}
}
}

View File

@ -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);