[Android] Video adapter extends BaseQueuedAdapter

Geoffrey Métais git at videolan.org
Thu Jul 20 15:50:19 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jul 20 15:49:46 2017 +0200| [51b95a2086eb59fc6687910c4a5e4bd113b50c82] | committer: Geoffrey Métais

Video adapter extends BaseQueuedAdapter

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

 .../videolan/vlc/gui/video/VideoListAdapter.java   | 30 +++++++++-------------
 1 file changed, 12 insertions(+), 18 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 cfb40d6f1..ac49be097 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -45,6 +45,7 @@ import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.BR;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.gui.BaseQueuedAdapter;
 import org.videolan.vlc.gui.helpers.AsyncImageLoader;
 import org.videolan.vlc.gui.helpers.UiTools;
 import org.videolan.vlc.interfaces.IEventsHandler;
@@ -52,14 +53,13 @@ import org.videolan.vlc.media.MediaGroup;
 import org.videolan.vlc.util.MediaItemFilter;
 import org.videolan.vlc.util.MediaLibraryItemComparator;
 
-import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
 
-public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> implements Filterable {
+public class VideoListAdapter extends BaseQueuedAdapter<ArrayList<MediaWrapper>, VideoListAdapter.ViewHolder> implements Filterable {
 
     public final static String TAG = "VLC/VideoListAdapter";
 
@@ -72,7 +72,6 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
     private IEventsHandler mEventsHandler;
     private static MediaLibraryItemComparator sMediaComparator = new MediaLibraryItemComparator(MediaLibraryItemComparator.ADAPTER_VIDEO);
     private ArrayList<MediaWrapper> mVideos = new ArrayList<>();
-    private ArrayDeque<ArrayList<MediaWrapper>> mPendingUpdates = new ArrayDeque<>();
     private ArrayList<MediaWrapper> mOriginalData = null;
     private ItemFilter mFilter = new ItemFilter();
     private int mSelectionCount = 0;
@@ -176,8 +175,8 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
     }
 
     @MainThread
-    private ArrayList<MediaWrapper> peekLast() {
-        return mPendingUpdates.isEmpty() ? mVideos : mPendingUpdates.peekLast();
+    public ArrayList<MediaWrapper> peekLast() {
+        return hasPendingUpdates() ? super.peekLast() : mVideos;
     }
 
     public boolean contains(MediaWrapper mw) {
@@ -411,35 +410,30 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         view.setLayoutParams(layoutParams);
     }
 
-    @MainThread
-    void update(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
-        mPendingUpdates.add(items);
-        if (mPendingUpdates.size() == 1)
-            internalUpdate(items, detectMoves);
-    }
-
-    private void internalUpdate(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
+    protected void internalUpdate(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
         VLCApplication.runBackground(new Runnable() {
             @Override
             public void run() {
-                if(detectMoves || mVideos.isEmpty())
+                if(detectMoves || items.size() != getItemCount())
                     Collections.sort(items, sMediaComparator);
                 final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new VideoItemDiffCallback(mVideos, items), detectMoves);
                 VLCApplication.runOnMainThread(new Runnable() {
                     @Override
                     public void run() {
-                        mPendingUpdates.remove();
                         mVideos = items;
                         result.dispatchUpdatesTo(VideoListAdapter.this);
-                        mEventsHandler.onUpdateFinished(null);
-                        if (!mPendingUpdates.isEmpty())
-                            internalUpdate(mPendingUpdates.peek(), true);
+                        processQueue();
                     }
                 });
             }
         });
     }
 
+    @Override
+    protected void onUpdateFinished() {
+        mEventsHandler.onUpdateFinished(null);
+    }
+
     private class VideoItemDiffCallback extends DiffUtil.Callback {
         ArrayList<MediaWrapper> oldList, newList;
         VideoItemDiffCallback(ArrayList<MediaWrapper> oldList, ArrayList<MediaWrapper> newList) {



More information about the Android mailing list