[Android] Refactor remove methods

Geoffrey Métais git at videolan.org
Tue Mar 7 11:09:23 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Mar  7 09:45:55 2017 +0100| [a34cb5c670ea11703f42f691056bfddad99d9ae2] | committer: Geoffrey Métais

Refactor remove methods

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

 .../org/videolan/vlc/gui/BaseQueuedAdapter.java    |  5 ++++
 .../vlc/gui/browser/BaseBrowserAdapter.java        | 33 +++++++++++++---------
 .../videolan/vlc/gui/video/VideoListAdapter.java   | 12 ++++++--
 3 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/BaseQueuedAdapter.java b/vlc-android/src/org/videolan/vlc/gui/BaseQueuedAdapter.java
index 6c34a18..21788f5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/BaseQueuedAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/BaseQueuedAdapter.java
@@ -38,6 +38,11 @@ public abstract class BaseQueuedAdapter <T, VH extends RecyclerView.ViewHolder>
     }
 
     @MainThread
+    public int getPendingCount() {
+        return mPendingUpdates.size();
+    }
+
+    @MainThread
     public T peekLast() {
         return mPendingUpdates.peekLast();
     }
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 8def327..b0d9568 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -259,30 +259,35 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
     void removeItem(int position) {
         if (position >= getItemCount())
             return;
-        MediaLibraryItem item = mMediaList.get(position);
+        removeItem(mMediaList.get(position));
+    }
+
+    void removeItem(MediaLibraryItem item) {
         if (item.getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
             mMediaCount--;
-        if (hasPendingUpdates())
-            peekLast().remove(mMediaList.get(position));
-        else {
-            ArrayList<MediaLibraryItem> list = new ArrayList<>(mMediaList);
-            list.remove(position);
+        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);
     }
 
     void removeItem(String path) {
-        int position = -1;
-        for (int i = 0; i< getItemCount(); ++i) {
-            MediaLibraryItem item = mMediaList.get(i);
+
+        MediaLibraryItem mediaItem = null;
+        for (MediaLibraryItem item : mMediaList) {
             if (item .getItemType() == TYPE_MEDIA && TextUtils.equals(path, ((MediaWrapper) item).getUri().toString())) {
-                position = i;
+                mediaItem = item;
                 break;
             }
         }
-        if (position == -1)
-            return;
-        removeItem(position);
+        if (mediaItem != null)
+            removeItem(mediaItem);
     }
 
     public ArrayList<MediaLibraryItem> getAll(){
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 026350a..2c8ae57 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -171,12 +171,18 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
 
     @MainThread
     public void remove(MediaWrapper item) {
-        if (mPendingUpdates.isEmpty()) {
+        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);
-        } else
-            mPendingUpdates.peekLast().remove(item);
+        }
     }
 
     @MainThread



More information about the Android mailing list