[Android] Speed up diffResult calculation for sorting

Geoffrey Métais git at videolan.org
Sun Jul 23 19:52:10 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Sun Jul 23 19:51:21 2017 +0200| [dd85d8bcdf358d336bf622b9bfac063891521ffa] | committer: Geoffrey Métais

Speed up diffResult calculation for sorting

> https://code.videolan.org/videolan/vlc-android/commit/dd85d8bcdf358d336bf622b9bfac063891521ffa
---

 .../src/org/videolan/vlc/gui/video/VideoListAdapter.java  |  7 ++++---
 .../src/org/videolan/vlc/util/MediaItemDiffCallback.java  | 15 ++++++---------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 55d533212..1650d3384 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -449,16 +449,17 @@ public class VideoListAdapter extends BaseQueuedAdapter<MediaWrapper, VideoListA
         public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
             MediaWrapper oldItem = oldList.get(oldItemPosition);
             MediaWrapper newItem = newList.get(newItemPosition);
-            return oldList != null && newList != null && oldItem.getType() == newItem.getType() && oldItem.equals(newItem);
+            return oldItem == newItem || (oldList != null && newList != null
+                    && oldItem.getType() == newItem.getType() && oldItem.equals(newItem));
         }
 
         @Override
         public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
             MediaWrapper oldItem = oldList.get(oldItemPosition);
             MediaWrapper newItem = newList.get(newItemPosition);
-            return oldItem.getTime() == newItem.getTime()
+            return oldItem == newItem || (oldItem.getTime() == newItem.getTime()
                     && TextUtils.equals(oldItem.getArtworkMrl(), newItem.getArtworkMrl())
-                    && oldItem.getSeen() == newItem.getSeen();
+                    && oldItem.getSeen() == newItem.getSeen());
         }
 
         @Nullable
diff --git a/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java b/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java
index 852ed9fcc..691d2cf83 100644
--- a/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java
+++ b/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java
@@ -9,31 +9,28 @@ import java.util.List;
 
 public class MediaItemDiffCallback extends DiffUtil.Callback {
     private static final String TAG = "MediaItemDiffCallback";
-    private MediaLibraryItem[] oldList, newList;
+    private List<? extends MediaLibraryItem> oldList, newList;
 
     public MediaItemDiffCallback(List<? extends MediaLibraryItem> oldList, List<? extends MediaLibraryItem> newList) {
-        this.oldList = oldList.toArray(new MediaLibraryItem[oldList.size()]);
-        this.newList = newList.toArray(new MediaLibraryItem[newList.size()]);
-    }
-
-    public MediaItemDiffCallback(MediaLibraryItem[] oldList, MediaLibraryItem[] newList) {
         this.oldList = oldList;
         this.newList = newList;
     }
 
     @Override
     public int getOldListSize() {
-        return oldList == null ? 0 :oldList.length;
+        return oldList == null ? 0 :oldList.size();
     }
 
     @Override
     public int getNewListSize() {
-        return newList == null ? 0 : newList.length;
+        return newList == null ? 0 : newList.size();
     }
 
     @Override
     public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
-        return (oldList[oldItemPosition] == null ) == ( newList[newItemPosition] == null) && oldList[oldItemPosition].equals(newList[newItemPosition]);
+        MediaLibraryItem oldItem = oldList.get(oldItemPosition);
+        MediaLibraryItem newItem = newList.get(newItemPosition);
+        return oldItem == newItem || ((oldItem == null ) == (newItem == null) && oldItem.equals(newItem));
     }
 
     @Override



More information about the Android mailing list