[Android] DiffUtil update handles progress changes

Geoffrey Métais git at videolan.org
Sun Dec 4 18:49:45 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Sun Dec  4 18:38:33 2016 +0100| [ec883395400708bf3552e5f360b31de44eafc6b8] | committer: Geoffrey Métais

DiffUtil update handles progress changes

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

 .../videolan/vlc/gui/video/VideoGridFragment.java  | 33 +------------
 .../videolan/vlc/gui/video/VideoListAdapter.java   | 57 +++++++++++-----------
 .../videolan/vlc/util/MediaItemDiffCallback.java   |  2 +-
 3 files changed, 32 insertions(+), 60 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index 839e3fa..b1319e1 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -145,16 +145,8 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
         mMediaLibrary.setMediaUpdatedCb(this, Medialibrary.FLAG_MEDIA_UPDATED_VIDEO);
         mMediaLibrary.setMediaAddedCb(this, Medialibrary.FLAG_MEDIA_ADDED_VIDEO);
         mMediaLibrary.addDeviceDiscoveryCb(this);
-        final boolean isWorking = mMediaLibrary.isWorking();
-        final boolean refresh = mVideoAdapter.isEmpty() && !isWorking;
         updateViewMode();
-        if (refresh)
-            updateList();
-        else {
-            mViewNomedia.setVisibility(mVideoAdapter.isEmpty() ? View.VISIBLE : View.GONE);
-            if (!isWorking)
-                updateTimes();
-        }
+        updateList();
     }
 
     @Override
@@ -190,24 +182,6 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
             return mGroup + "\u2026";
     }
 
-    private void updateTimes() {
-        VLCApplication.runBackground(new Runnable() {
-            @Override
-            public void run() {
-                MediaWrapper[] videos = mMediaLibrary.getVideos();
-                final SimpleArrayMap<Long, Long> times = new SimpleArrayMap<>(videos.length);
-                for (MediaWrapper mw : videos)
-                    times.put(mw.getId(), mw.getTime());
-                mHandler.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        mVideoAdapter.setTimes(times);
-                    }
-                });
-            }
-        });
-    }
-
     private void updateViewMode() {
         if (getView() == null || getActivity() == null) {
             Log.w(TAG, "Unable to setup the view");
@@ -466,10 +440,7 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
     public void onDiscoveryProgress(String entryPoint) {}
 
     @Override
-    public void onDiscoveryCompleted(final String entryPoint) {
-        if (!mParsing && TextUtils.isEmpty(entryPoint))
-            mHandler.sendEmptyMessage(UPDATE_LIST);
-    }
+    public void onDiscoveryCompleted(final String entryPoint) {}
 
     @Override
     public void onParsingStatsUpdated(final int percent) {
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 b31358f..fb7be54 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -47,6 +47,7 @@ import android.widget.Filterable;
 import android.widget.ImageView;
 
 import org.videolan.medialibrary.Tools;
+import org.videolan.medialibrary.media.MediaLibraryItem;
 import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.BR;
 import org.videolan.vlc.R;
@@ -80,7 +81,6 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
     public final static int SORT_BY_DATE = 2;
     private boolean mListMode = false, mActionMode;
-    SparseArrayCompat<WeakReference<ViewHolder>> mHolders = new SparseArrayCompat<>();
     private List<Integer> mSelectedItems = new LinkedList<>();
     private VideoGridFragment mFragment;
     private VideoComparator mVideoComparator = new VideoComparator();
@@ -119,40 +119,21 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         boolean isSelected = mActionMode && mSelectedItems.contains(position);
         holder.setOverlay(mActionMode && isSelected);
         holder.binding.setVariable(BR.bgColor, ContextCompat.getColor(holder.itemView.getContext(), mListMode && isSelected ? R.color.orange200transparent : R.color.transparent));
-        mHolders.put(position, new WeakReference<>(holder));
     }
 
+    @Override
+    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
+        if (payloads.isEmpty())
+            super.onBindViewHolder(holder, position, payloads);
+        else
+            fillView(holder, (MediaWrapper) payloads.get(0));
+    }
 
     @Override
     public void onViewRecycled(ViewHolder holder) {
-        mHolders.remove(holder.getAdapterPosition());
         holder.binding.setVariable(BR.cover, AsyncImageLoader.DEFAULT_COVER_VIDEO_DRAWABLE);
     }
 
-    @MainThread
-    void setTimes( SimpleArrayMap<Long, Long> times) {
-        // update times
-        for (int i = 0; i < getItemCount(); ++i) {
-            MediaWrapper media = mVideos.get(i);
-            Long time = times.get(media.getId());
-            if (time != null && time != media.getTime()) {
-                media.setTime(time);
-                int position = getPosition(media);
-                WeakReference<ViewHolder> wr = mHolders.get(position);
-                if (wr != null && wr.get() != null)
-                    fillView(wr.get(), media);
-            }
-        }
-    }
-
-    private int getPosition(MediaWrapper media) {
-        for (int i = 0; i < getItemCount(); ++i) {
-            if (media.equals(getItem(i)))
-                return i;
-        }
-        return -1;
-    }
-
     public void sort() {
         if (!isEmpty())
             try {
@@ -553,7 +534,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         final ArrayList<MediaWrapper> oldList = getAll();
         mVideos.clear();
         addAll(newList);
-        final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(oldList, newList));
+        final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new VideoItemDiffCallback(oldList, newList));
         mHandler.post(new Runnable() {
             @Override
             public void run() {
@@ -561,4 +542,24 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
             }
         });
     }
+
+    private class VideoItemDiffCallback extends MediaItemDiffCallback {
+        VideoItemDiffCallback(List<? extends MediaLibraryItem> oldList, List<? extends MediaLibraryItem> newList) {
+            super(oldList, newList);
+        }
+
+        @Override
+        public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
+            MediaWrapper oldItem = (MediaWrapper) oldList[oldItemPosition];
+            MediaWrapper newItem = (MediaWrapper) newList[newItemPosition];
+            return oldItem.getTime() == newItem.getTime();
+        }
+
+        @Nullable
+        @Override
+        public Object getChangePayload(int oldItemPosition, int newItemPosition) {
+            ((MediaWrapper) oldList[oldItemPosition]).setTime(((MediaWrapper) newList[newItemPosition]).getTime());
+            return oldList[oldItemPosition];
+        }
+    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java b/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java
index 6491261..81b83c7 100644
--- a/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java
+++ b/vlc-android/src/org/videolan/vlc/util/MediaItemDiffCallback.java
@@ -9,7 +9,7 @@ import java.util.List;
 
 public class MediaItemDiffCallback extends DiffUtil.Callback {
     private static final String TAG = "MediaItemDiffCallback";
-    MediaLibraryItem[] oldList, newList;
+    protected MediaLibraryItem[] oldList, newList;
 
     public MediaItemDiffCallback(List<? extends MediaLibraryItem> oldList, List<? extends MediaLibraryItem> newList) {
         this.oldList = oldList.toArray(new MediaLibraryItem[oldList.size()]);



More information about the Android mailing list