[Android] Improve adapters pending queue management

Geoffrey Métais git at videolan.org
Fri Mar 10 11:43:01 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Mar 10 11:35:12 2017 +0100| [b8b66ab83e4f6ccd95273787d22da88f6e7792ed] | committer: Geoffrey Métais

Improve adapters pending queue management

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

 .../vlc/gui/audio/AudioBrowserAdapter.java         | 17 ++++++++++----
 .../vlc/gui/browser/BaseBrowserAdapter.java        | 22 ++++++++----------
 .../videolan/vlc/gui/video/VideoListAdapter.java   | 27 +++++++++-------------
 3 files changed, 34 insertions(+), 32 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
index 55e3b8f..691d567 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -37,6 +37,7 @@ import android.view.ViewGroup;
 import android.widget.Filter;
 import android.widget.Filterable;
 
+import org.videolan.medialibrary.Tools;
 import org.videolan.medialibrary.media.DummyItem;
 import org.videolan.medialibrary.media.MediaLibraryItem;
 import org.videolan.vlc.BR;
@@ -186,7 +187,7 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem[], A
 
     @MainThread
     public boolean isEmpty() {
-        return getItemCount() == 0;
+        return Tools.isArrayEmpty(peekLast());
     }
 
     public void clear() {
@@ -252,19 +253,27 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem[], A
     }
 
     public void remove(final MediaLibraryItem item) {
-        final MediaLibraryItem[] referenceList = hasPendingUpdates() ? peekLast() : mDataList;
+        final MediaLibraryItem[] referenceList = peekLast();
+        if (Tools.isArrayEmpty(referenceList))
+            return;
         final MediaLibraryItem[] dataList = new MediaLibraryItem[referenceList.length-1];
         Util.removeItemInArray(referenceList, item, dataList);
         update(dataList);
     }
 
     public void addItem(final int position, final MediaLibraryItem item) {
-        final MediaLibraryItem[] referenceList = hasPendingUpdates() ? peekLast() : mDataList;
-        final MediaLibraryItem[] dataList = new MediaLibraryItem[referenceList.length+1];
+        final MediaLibraryItem[] referenceList = peekLast();
+        final MediaLibraryItem[] dataList = Tools.isArrayEmpty(referenceList)
+                ? new MediaLibraryItem[]{item} : new MediaLibraryItem[referenceList.length+1];
         Util.addItemInArray(referenceList, position, item, dataList);
         update(dataList);
     }
 
+    @Override
+    public MediaLibraryItem[] peekLast() {
+        return hasPendingUpdates() ? super.peekLast() : mDataList;
+    }
+
     public void restoreList() {
         if (mOriginalDataSet != null) {
             update(Arrays.copyOf(mOriginalDataSet, mOriginalDataSet.length));
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
index 2d993dc..cd4091f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -219,7 +219,12 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
     }
 
     public boolean isEmpty(){
-        return mMediaList.isEmpty();
+        return peekLast().isEmpty();
+    }
+
+    @Override
+    public ArrayList<MediaLibraryItem> peekLast() {
+        return hasPendingUpdates() ? super.peekLast() : mMediaList;
     }
 
     public void addItem(MediaLibraryItem item, boolean top){
@@ -232,7 +237,7 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
 
     void addItem(MediaLibraryItem item, boolean top, int positionTo){
         int position;
-        ArrayList<MediaLibraryItem> list = new ArrayList<>(hasPendingUpdates() ? peekLast() : mMediaList);
+        ArrayList<MediaLibraryItem> list = new ArrayList<>(peekLast());
         if (positionTo != -1)
             position = positionTo;
         else
@@ -267,16 +272,9 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
     void removeItem(MediaLibraryItem item) {
         if (item.getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
             mMediaCount--;
-        int pendingCount = getPendingCount();
-        if (pendingCount == 0) {
-            mMediaList.remove(item);
-            notifyItemRemoved(mMediaList.indexOf(item));
-        } else if (pendingCount == 1) {
-            ArrayList<MediaLibraryItem> list = new ArrayList<>(peekLast());
-            list.remove(item);
-            update(list);
-        } else
-            peekLast().remove(item);
+        ArrayList<MediaLibraryItem> list = new ArrayList<>(peekLast());
+        list.remove(item);
+        update(list);
     }
 
     void removeItem(String path) {
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 2c8ae57..7875687 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -148,9 +148,8 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         holder.binding.setVariable(BR.cover, AsyncImageLoader.DEFAULT_COVER_VIDEO_DRAWABLE);
     }
 
-    public boolean isEmpty()
-    {
-        return mVideos.size() == 0;
+    public boolean isEmpty() {
+        return peekLast().size() == 0;
     }
 
     @Nullable
@@ -164,25 +163,16 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
     @MainThread
     public void add(MediaWrapper item) {
-        ArrayList<MediaWrapper> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mVideos : mPendingUpdates.peekLast());
+        ArrayList<MediaWrapper> list = new ArrayList<>(peekLast());
         list.add(item);
         update(list, false);
     }
 
     @MainThread
     public void remove(MediaWrapper item) {
-        int pendingCount = mPendingUpdates.size();
-        if (pendingCount > 1) {
-            mPendingUpdates.peekLast().remove(item);
-        } else if (pendingCount == 1) {
-            ArrayList<MediaWrapper> refList = new ArrayList<>(mPendingUpdates.peekLast());
-            if (refList.remove(item))
-                update(refList, false);
-        } else {
-            int position = mVideos.indexOf(item);
-            if (mVideos.remove(item))
-                notifyItemRemoved(position);
-        }
+        ArrayList<MediaWrapper> refList = new ArrayList<>(peekLast());
+        if (refList.remove(item))
+            update(refList, false);
     }
 
     @MainThread
@@ -191,6 +181,11 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
         mOriginalData = null;
     }
 
+    @MainThread
+    private ArrayList<MediaWrapper> peekLast() {
+        return mPendingUpdates.isEmpty() ? mVideos : mPendingUpdates.peekLast();
+    }
+
     public boolean contains(MediaWrapper mw) {
         return mVideos.indexOf(mw) != -1;
     }



More information about the Android mailing list