Commit 2bec0df7 authored by Yusuke Iwaki's avatar Yusuke Iwaki Committed by GitHub

Merge pull request #146 from RocketChat/feature/message-list-diff-util

Using diff util to update the list
parents 094011b7 ae5211eb
......@@ -2,6 +2,7 @@ package chat.rocket.android.layouthelper;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.util.ListUpdateCallback;
import io.realm.RealmObject;
import chat.rocket.android.realm_helper.RealmModelListAdapter;
......@@ -37,6 +38,28 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
notifyItemChanged(position + 1);
}
protected ListUpdateCallback listUpdateCallback = new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
notifyItemRangeInserted(position + 1, count);
}
@Override
public void onRemoved(int position, int count) {
notifyItemRangeRemoved(position + 1, count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
notifyItemMoved(fromPosition + 1, toPosition + 1);
}
@Override
public void onChanged(int position, int count, Object payload) {
notifyItemRangeChanged(position + 1, count, payload);
}
};
@Override
public int getItemViewType(int position) {
if (position == 0) {
......@@ -83,4 +106,9 @@ public abstract class ExtRealmModelListAdapter<T extends RealmObject, VM,
// rely on VH.bind().
super.onBindViewHolder(holder, position - 1);
}
@Override
public ListUpdateCallback getListUpdateCallback() {
return listUpdateCallback;
}
}
package chat.rocket.android.layouthelper.chatroom;
import android.content.Context;
import android.support.v7.util.DiffUtil;
import android.view.View;
import java.util.ArrayList;
......@@ -111,4 +112,53 @@ public class MessageListAdapter
return extMessages;
}
@Override
protected DiffUtil.Callback getDiffCallback(List<PairedMessage> oldData,
List<PairedMessage> newData) {
return new PairedMessageDiffCallback(oldData, newData);
}
private static class PairedMessageDiffCallback extends DiffUtil.Callback {
private final List<PairedMessage> oldList;
private final List<PairedMessage> newList;
public PairedMessageDiffCallback(List<PairedMessage> oldList, List<PairedMessage> newList) {
this.oldList = oldList;
this.newList = newList;
}
@Override
public int getOldListSize() {
if (oldList == null) {
return 0;
}
return oldList.size();
}
@Override
public int getNewListSize() {
if (newList == null) {
return 0;
}
return newList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
PairedMessage oldMessage = oldList.get(oldItemPosition);
PairedMessage newMessage = newList.get(newItemPosition);
return oldMessage.getId().equals(newMessage.getId());
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
PairedMessage oldMessage = oldList.get(oldItemPosition);
PairedMessage newMessage = newList.get(newItemPosition);
return oldMessage.toString().equals(newMessage.toString());
}
}
}
......@@ -32,4 +32,16 @@ public class PairedMessage {
&& nextSibling.getUser() != null && target.getUser() != null
&& nextSibling.getUser().getId().equals(target.getUser().getId());
}
public String getId() {
return target.getId();
}
@Override
public String toString() {
return "PairedMessage{" +
"target=" + target +
", nextSibling=" + nextSibling +
'}';
}
}
......@@ -131,4 +131,20 @@ public class Message extends RealmObject {
public void setUrls(String urls) {
this.urls = urls;
}
@Override
public String toString() {
return "Message{" +
"_id='" + _id + '\'' +
", t='" + t + '\'' +
", rid='" + rid + '\'' +
", syncstate=" + syncstate +
", ts=" + ts +
", msg='" + msg + '\'' +
", u=" + u +
", groupable=" + groupable +
", attachments='" + attachments + '\'' +
", urls='" + urls + '\'' +
'}';
}
}
......@@ -79,4 +79,16 @@ public class User extends RealmObject {
public Settings getSettings() {
return settings;
}
@Override
public String toString() {
return "User{" +
"_id='" + _id + '\'' +
", username='" + username + '\'' +
", status='" + status + '\'' +
", utcOffset=" + utcOffset +
", emails=" + emails +
", settings=" + settings +
'}';
}
}
......@@ -2,6 +2,8 @@ package chat.rocket.android.realm_helper;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.util.DiffUtil;
import android.support.v7.util.ListUpdateCallback;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
......@@ -89,12 +91,20 @@ public abstract class RealmModelListAdapter<T extends RealmObject, VM,
adapterData = mapResultsToViewModel(newData);
notifyDataSetChanged();
} else {
// TODO: use DillUtils!
adapterData = mapResultsToViewModel(newData);
notifyDataSetChanged();
final List<VM> newMappedData = mapResultsToViewModel(newData);
final DiffUtil.Callback diffCallback = getDiffCallback(adapterData, newMappedData);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
adapterData = newMappedData;
diffResult.dispatchUpdatesTo(getListUpdateCallback());
}
}
protected abstract DiffUtil.Callback getDiffCallback(List<VM> oldData, List<VM> newData);
protected abstract ListUpdateCallback getListUpdateCallback();
public void setOnItemClickListener(OnItemClickListener<VM> onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment