From b60849d63260fd1d1bca1af8f28791d431644812 Mon Sep 17 00:00:00 2001 From: Ian Lake Date: Sat, 22 Feb 2020 18:57:57 -0800 Subject: [PATCH] Make CommentAdapter extend ListAdapter Use `ListAdapter` to calculate the DiffUtil off of the main thread. Test: ./gradlew cC --- .../persistence/ui/CommentAdapter.java | 76 ++++++------------- .../persistence/ui/ProductFragment.java | 2 +- .../viewmodel/ProductViewModel.java | 1 - 3 files changed, 24 insertions(+), 55 deletions(-) diff --git a/BasicSample/app/src/main/java/com/example/android/persistence/ui/CommentAdapter.java b/BasicSample/app/src/main/java/com/example/android/persistence/ui/CommentAdapter.java index 934846b..a46d565 100644 --- a/BasicSample/app/src/main/java/com/example/android/persistence/ui/CommentAdapter.java +++ b/BasicSample/app/src/main/java/com/example/android/persistence/ui/CommentAdapter.java @@ -17,70 +17,45 @@ package com.example.android.persistence.ui; import androidx.annotation.NonNull; -import androidx.databinding.DataBindingUtil; import androidx.annotation.Nullable; +import androidx.databinding.DataBindingUtil; +import androidx.recyclerview.widget.AsyncDifferConfig; import androidx.recyclerview.widget.DiffUtil; +import androidx.recyclerview.widget.ListAdapter; import androidx.recyclerview.widget.RecyclerView; - import android.text.TextUtils; import android.view.LayoutInflater; import android.view.ViewGroup; import com.example.android.persistence.databinding.CommentItemBinding; -import com.example.android.persistence.model.Comment; +import com.example.android.persistence.db.entity.CommentEntity; import com.example.android.persistence.R; -import java.util.List; - -public class CommentAdapter extends RecyclerView.Adapter { - - private List mCommentList; +public class CommentAdapter extends ListAdapter { @Nullable private final CommentClickCallback mCommentClickCallback; - public CommentAdapter(@Nullable CommentClickCallback commentClickCallback) { + CommentAdapter(@Nullable CommentClickCallback commentClickCallback) { + super(new AsyncDifferConfig.Builder<>(new DiffUtil.ItemCallback() { + @Override + public boolean areItemsTheSame(@NonNull CommentEntity old, + @NonNull CommentEntity comment) { + return old.getId() == comment.getId(); + } + + @Override + public boolean areContentsTheSame(@NonNull CommentEntity old, + @NonNull CommentEntity comment) { + return old.getId() == comment.getId() + && old.getPostedAt().equals(comment.getPostedAt()) + && old.getProductId() == comment.getProductId() + && TextUtils.equals(old.getText(), comment.getText()); + } + }).build()); mCommentClickCallback = commentClickCallback; } - public void setCommentList(final List comments) { - if (mCommentList == null) { - mCommentList = comments; - notifyItemRangeInserted(0, comments.size()); - } else { - DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() { - @Override - public int getOldListSize() { - return mCommentList.size(); - } - - @Override - public int getNewListSize() { - return comments.size(); - } - - @Override - public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) { - Comment old = mCommentList.get(oldItemPosition); - Comment comment = comments.get(newItemPosition); - return old.getId() == comment.getId(); - } - - @Override - public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) { - Comment old = mCommentList.get(oldItemPosition); - Comment comment = comments.get(newItemPosition); - return old.getId() == comment.getId() - && old.getPostedAt() == comment.getPostedAt() - && old.getProductId() == comment.getProductId() - && TextUtils.equals(old.getText(), comment.getText()); - } - }); - mCommentList = comments; - diffResult.dispatchUpdatesTo(this); - } - } - @Override @NonNull public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { @@ -93,15 +68,10 @@ public class CommentAdapter extends RecyclerView.Adapter { if (commentEntities != null) { mBinding.setIsLoading(false); - mCommentAdapter.setCommentList(commentEntities); + mCommentAdapter.submitList(commentEntities); } else { mBinding.setIsLoading(true); } diff --git a/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductViewModel.java b/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductViewModel.java index 7366d4d..57a2618 100644 --- a/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductViewModel.java +++ b/BasicSample/app/src/main/java/com/example/android/persistence/viewmodel/ProductViewModel.java @@ -21,7 +21,6 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; -import androidx.databinding.ObservableField; import androidx.annotation.NonNull; import com.example.android.persistence.BasicApp; -- GitLab