Commit 93b5737c authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Add items back after deletion cancel with DiffUtil

parent 18739166
......@@ -169,7 +169,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
private void updateList() {
if (mPlaylist != null) {
List<MediaLibraryItem> tracks = Util.arrayToMediaArrayList(mPlaylist.getTracks());
mAdapter.addAll(tracks);
mAdapter.update(tracks);
}
}
......@@ -345,7 +345,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
final Runnable cancel = new Runnable() {
@Override
public void run() {
mAdapter.addItem(position, media);
mAdapter.addItems(media);
}
};
UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.file_deleted), new Runnable() {
......
......@@ -227,7 +227,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
final Runnable cancel = new Runnable() {
@Override
public void run() {
adapter.addItem(position, mediaItem);
adapter.addItems(mediaItem);
}
};
UiTools.snackerWithCancel(mViewPager, getString(R.string.file_deleted), new Runnable() {
......
......@@ -207,10 +207,6 @@ public class AudioBrowserAdapter extends SortableAdapter<MediaLibraryItem, Audio
mOriginalDataSet = null;
}
public void addAll(List<MediaLibraryItem> items) {
setDataset(new ArrayList<>(items));
}
private List<MediaLibraryItem> removeSections(List<MediaLibraryItem> items) {
List<MediaLibraryItem> newList = new ArrayList<>();
for (MediaLibraryItem item : items)
......@@ -322,20 +318,21 @@ public class AudioBrowserAdapter extends SortableAdapter<MediaLibraryItem, Audio
return datalist;
}
public void remove(final MediaLibraryItem item) {
public void remove(final MediaLibraryItem... items) {
final List<MediaLibraryItem> referenceList = peekLast();
if (referenceList.size() == 0) return;
if (referenceList.isEmpty()) return;
final List<MediaLibraryItem> dataList = new ArrayList<>(referenceList);
dataList.remove(item);
for (MediaLibraryItem item : items) dataList.remove(item);
update(dataList);
}
public void addItem(final int position, final MediaLibraryItem item) {
public void addItems(final MediaLibraryItem... items) {
final List<MediaLibraryItem> referenceList = peekLast();
if (position < 0 || position >= referenceList.size()) return;
final List<MediaLibraryItem> dataList = new ArrayList<>(referenceList);
dataList.add(position,item);
Collections.addAll(dataList, items);
//Force adapter to sort items.
if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) sMediaComparator.sortBy = getDefaultSort();
update(dataList);
}
......
......@@ -245,11 +245,11 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
final MediaLibraryItem next = position < adapter.getItemCount()-1 ? adapter.getItem(position+1) : null;
String message;
Runnable action;
Runnable cancel = null;
final MediaLibraryItem separator = previous != null && previous.getItemType() == MediaLibraryItem.TYPE_DUMMY &&
(next == null || next.getItemType() == MediaLibraryItem.TYPE_DUMMY) ? previous : null;
adapter.remove(mediaLibraryItem);
if (separator != null)
adapter.remove(separator);
if (separator != null) adapter.remove(separator, mediaLibraryItem);
else adapter.remove(mediaLibraryItem);
if (mode == MODE_PLAYLIST) {
message = getString(R.string.playlist_deleted);
......@@ -261,23 +261,23 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
};
} else if (mode == MODE_SONG) {
message = getString(R.string.file_deleted);
final Runnable cancel = new Runnable() {
cancel = new Runnable() {
@Override
public void run() {
if (separator != null)
adapter.addItem(position-1, separator);
adapter.addItem(position, mediaLibraryItem);
if (separator != null) adapter.addItems(separator, mediaLibraryItem);
else adapter.addItems(mediaLibraryItem);
}
};
final Runnable finalCancel = cancel;
action = new Runnable() {
@Override
public void run() {
deleteMedia(mediaLibraryItem, true, cancel);
deleteMedia(mediaLibraryItem, true, finalCancel);
}
};
} else
return false;
UiTools.snackerWithCancel(getView(), message, action);
UiTools.snackerWithCancel(getView(), message, action, cancel);
return true;
}
......
......@@ -46,6 +46,7 @@ import org.videolan.vlc.databinding.BrowserItemSeparatorBinding;
import org.videolan.vlc.gui.helpers.SelectorViewHolder;
import org.videolan.vlc.util.AndroidDevices;
import org.videolan.vlc.util.MediaItemFilter;
import org.videolan.vlc.util.MediaLibraryItemComparator;
import org.videolan.vlc.util.Util;
import java.util.ArrayList;
......@@ -267,9 +268,7 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
}
void removeItem(int position) {
if (position >= getItemCount())
return;
removeItem(getDataset().get(position));
if (position < getItemCount()) removeItem(getDataset().get(position));
}
void removeItem(MediaLibraryItem item) {
......@@ -277,6 +276,8 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
mMediaCount--;
final List<MediaLibraryItem> list = new ArrayList<>(peekLast());
list.remove(item);
//Force adapter to sort items.
if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) sMediaComparator.sortBy = getDefaultSort();
update(list);
}
......@@ -289,8 +290,7 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
break;
}
}
if (mediaItem != null)
removeItem(mediaItem);
if (mediaItem != null) removeItem(mediaItem);
}
public List<MediaLibraryItem> getAll(){
......
......@@ -551,11 +551,11 @@ public abstract class BaseBrowserFragment extends SortableFragment<BaseBrowserAd
return true;
}
case R.id.directory_view_delete:
mAdapter.removeItem(position);
mAdapter.removeItem(mw);
final Runnable cancel = new Runnable() {
@Override
public void run() {
mAdapter.addItem(mw, true, position);
mAdapter.addItem(mw, true);
}
};
UiTools.snackerWithCancel(getView(), getString(R.string.file_deleted), new Runnable() {
......
......@@ -293,7 +293,7 @@ public class VideoGridFragment extends SortableFragment<VideoListAdapter> implem
final Runnable revert = new Runnable() {
@Override
public void run() {
mAdapter.add(media, position);
mAdapter.add(media);
}
};
UiTools.snackerWithCancel(view, getString(R.string.file_deleted), new Runnable() {
......
......@@ -146,10 +146,11 @@ public class VideoListAdapter extends SortableAdapter<MediaWrapper, VideoListAda
}
@MainThread
public void add(MediaWrapper item, int position) {
public void add(MediaWrapper item) {
final List<MediaWrapper> list = new ArrayList<>(peekLast());
if (position < 0 || position >= list.size()) return;
list.add(position, item);
list.add(item);
//Force adapter to sort items.
if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) sMediaComparator.sortBy = getDefaultSort();
update(list);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment