[Android] Delete with undo action for AudioBrowserFragment

Geoffrey Métais git at videolan.org
Thu Jan 7 15:05:53 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jan  7 14:03:00 2016 +0100| [b03620769441faa60fd261bb1019e2173f33f789] | committer: Geoffrey Métais

Delete with undo action for AudioBrowserFragment

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

 .../vlc/gui/audio/AudioBrowserFragment.java        | 37 +++++++++++++++-------
 .../vlc/gui/audio/AudioBrowserListAdapter.java     | 21 ++++++++++--
 2 files changed, 45 insertions(+), 13 deletions(-)

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 5893482..ae6514b 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -423,7 +423,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
         return super.onContextItemSelected(menu);
     }
 
-    private boolean handleContextItemSelected(MenuItem item, final int position) {
+    private boolean handleContextItemSelected(final MenuItem item, final int position) {
 
         int startPosition;
         int mode = mViewPager.getCurrentItem();
@@ -434,7 +434,7 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
         boolean append = id == R.id.audio_list_browser_append;
 
         if (id == R.id.audio_list_browser_delete) {
-            AudioBrowserListAdapter adapter;
+            final AudioBrowserListAdapter adapter;
             if (mode == MODE_SONG){
                 adapter = mSongsAdapter;
             } else if (mode == MODE_PLAYLIST) {
@@ -444,21 +444,37 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
             List<MediaWrapper> mediaList = adapter.getMedias(position);
             if (adapter.getCount() <= position || mediaList == null || mediaList.isEmpty())
                 return false;
+            final MediaWrapper media = mediaList.get(0);
+            final AudioBrowserListAdapter.ListItem listItem = adapter.getItem(position);
+            final String key = adapter.getKey(position);
+            String message;
+            Runnable action;
+
+            adapter.remove(position, key);
+
             if (mode == MODE_PLAYLIST) {
-                UiTools.snackerWithCancel(getView(), getString(R.string.playlist_deleted), new Runnable() {
+                message = getString(R.string.playlist_deleted);
+                action = new Runnable() {
                     @Override
                     public void run() {
-                        deletePlaylist(mPlaylistAdapter.getItem(position));
+                        deletePlaylist(listItem);
                     }
-                });
+                };
             } else {
-                UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
+                message = getString(R.string.file_deleted);
+                action = new Runnable() {
                     @Override
                     public void run() {
-                        deleteMedia(position);
+                        deleteMedia(media);
                     }
-                });
+                };
             }
+            UiTools.snackerWithCancel(getView(), message, action, new Runnable() {
+                @Override
+                public void run() {
+                    adapter.addItem(position, key, listItem);
+                }
+            });
             return true;
         }
 
@@ -660,13 +676,12 @@ public class AudioBrowserFragment extends MediaBrowserFragment implements SwipeR
         tcl.onPageSelected(position);
     }
 
-    private void deleteMedia(int position) {
-        final MediaWrapper mw = mSongsAdapter.getItem(position).mMediaList.get(0);
+    private void deleteMedia(final MediaWrapper mw) {
         VLCApplication.runBackground(new Runnable() {
             @Override
             public void run() {
                 final String path = mw.getUri().getPath();
-                FileUtils.deleteFile(path);
+                boolean delete = FileUtils.deleteFile(path);
                 MediaDatabase.getInstance().removeMedia(mw.getUri());
                 mMediaLibrary.getMediaItems().remove(mw);
                 mHandler.obtainMessage(REFRESH, path).sendToTarget();
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java
index cc9e87d..93bb1b5 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java
@@ -201,6 +201,22 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
         });
     }
 
+    public void remove(int position, String key) {
+        mItems.remove(position);
+        mMediaItemMap.remove(key);
+        notifyDataSetChanged();
+    }
+
+    public void addItem(int position, String key, ListItem item) {
+        mMediaItemMap.put(key, item);
+        mItems.add(position, item);
+        notifyDataSetChanged();
+    }
+
+    public String getKey(int position) {
+        return (String) mMediaItemMap.keySet().toArray()[position];
+    }
+
     /**
      * Calculate sections of the list
      *
@@ -281,7 +297,7 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
      * Remove also all the list items that contain only this media.
      * @param media the media to remove
      */
-    public void removeMedia(MediaWrapper media) {
+    public void removeMedia(MediaWrapper media, boolean notify) {
         for (int i = 0; i < mItems.size(); ++i) {
             ListItem item = mItems.get(i);
             if (item.mMediaList == null)
@@ -296,7 +312,8 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
                 i--;
             }
         }
-        notifyDataSetChanged();
+        if (notify)
+            notifyDataSetChanged();
     }
 
     public void clear() {



More information about the Android mailing list