[Android] Refactor add/remove methods from adapters

Geoffrey Métais git at videolan.org
Mon Mar 6 16:53:42 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Mar  6 16:28:19 2017 +0100| [c0b2acbcd3a1f234a12f5dceef2bac19ea9bb396] | committer: Geoffrey Métais

Refactor add/remove methods from adapters

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

 vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java  |  2 +-
 .../videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java    |  2 +-
 .../src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java |  4 ++--
 .../org/videolan/vlc/gui/audio/AudioBrowserFragment.java    |  6 +++---
 .../org/videolan/vlc/gui/browser/BaseBrowserAdapter.java    | 12 ++++++++----
 .../src/org/videolan/vlc/gui/video/VideoGridFragment.java   |  6 +++---
 .../src/org/videolan/vlc/gui/video/VideoListAdapter.java    | 13 +++++++------
 7 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
index 14404cb..a0094ad 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
@@ -316,7 +316,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
             mService.append(media);
             return true;
         } else if (id == R.id.audio_list_browser_delete) {
-            mAdapter.remove(position);
+            mAdapter.remove(media);
             UiTools.snackerWithCancel(getWindow().getDecorView(), getString(R.string.file_deleted), new Runnable() {
                 @Override
                 public void run() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
index a073ea0..8b84b9d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioAlbumsSongsFragment.java
@@ -203,7 +203,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
 
         if (id == R.id.audio_list_browser_delete) {
 
-            adapter.remove(position);
+            adapter.remove(mediaItem);
 
             UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
                 @Override
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 de35199..55e3b8f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -251,10 +251,10 @@ public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem[], A
         return datalist.toArray(new MediaLibraryItem[datalist.size()]);
     }
 
-    public void remove(int position) {
+    public void remove(final MediaLibraryItem item) {
         final MediaLibraryItem[] referenceList = hasPendingUpdates() ? peekLast() : mDataList;
         final MediaLibraryItem[] dataList = new MediaLibraryItem[referenceList.length-1];
-        Util.removePositionInArray(referenceList, position, dataList);
+        Util.removeItemInArray(referenceList, item, dataList);
         update(dataList);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index 6a5d895..4974b94 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -294,10 +294,10 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements DevicesDis
             String message;
             Runnable action;
             final MediaLibraryItem separator = previous != null && previous.getItemType() == MediaLibraryItem.TYPE_DUMMY &&
-                    (next == null || next.getItemType() == MediaLibraryItem.TYPE_DUMMY) ? adapter.getItem(position-1) : null;
-            adapter.remove(position);
+                    (next == null || next.getItemType() == MediaLibraryItem.TYPE_DUMMY) ? previous : null;
+            adapter.remove(mediaLibraryItem);
             if (separator != null)
-                adapter.remove(position-1);
+                adapter.remove(separator);
 
             if (mode == MODE_PLAYLIST) {
                 message = getString(R.string.playlist_deleted);
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 11d6104..8def327 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.java
@@ -260,11 +260,15 @@ public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibrary
         if (position >= getItemCount())
             return;
         MediaLibraryItem item = mMediaList.get(position);
-        ArrayList<MediaLibraryItem> list = new ArrayList<>(hasPendingUpdates() ? peekLast() : mMediaList);
-        list.remove(position);
-        update(list);
-        if (item .getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
+        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);
+            update(list);
+        }
     }
 
     void removeItem(String path) {
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 095bb83..a72add6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -277,7 +277,7 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
                 showInfoDialog(media);
                 return true;
             case R.id.video_list_delete:
-                removeVideo(position, media);
+                removeVideo(media);
                 return true;
             case R.id.video_group_play:
                 MediaUtils.openList(getActivity(), ((MediaGroup) media).getAll(), 0);
@@ -295,8 +295,8 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
         return false;
     }
 
-    private void removeVideo(int position, final MediaWrapper media) {
-        mVideoAdapter.remove(position);
+    private void removeVideo(final MediaWrapper media) {
+        mVideoAdapter.remove(media);
         if (getView() != null)
             UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
                 @Override
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 0672d61..026350a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -170,12 +170,13 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
     }
 
     @MainThread
-    public void remove(int position) {
-        if (position == -1)
-            return;
-        ArrayList<MediaWrapper> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mVideos : mPendingUpdates.peekLast());
-        list.remove(position);
-        update(list, false);
+    public void remove(MediaWrapper item) {
+        if (mPendingUpdates.isEmpty()) {
+            int position = mVideos.indexOf(item);
+            if (mVideos.remove(item))
+                notifyItemRemoved(position);
+        } else
+            mPendingUpdates.peekLast().remove(item);
     }
 
     @MainThread



More information about the Android mailing list