Commit 688ee3bd authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Remove deleted media via viewmodels

Instead of adapters, and clean DiffUtil base class
parent ca47717f
......@@ -15,7 +15,6 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
protected var dataset: List<D> = listOf()
private set
@Volatile private var last = dataset
private val diffCallback by lazy(LazyThreadSafetyMode.NONE) { createCB() }
private val updateActor = actor<List<D>>(newSingleThreadContext("vlc-updater"), capacity = Channel.CONFLATED) {
for (list in channel) internalUpdate(list)
......@@ -24,7 +23,6 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
@MainThread
fun update (list: List<D>) {
last = list
updateActor.offer(list)
}
......@@ -41,9 +39,8 @@ abstract class DiffUtilAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.A
protected open fun prepareList(list: List<D>) : List<D> = ArrayList(list)
fun peekLast() = last
fun hasPendingUpdates() = updateActor.isFull
@MainThread
fun isEmpty() = dataset.isEmpty()
open fun getItem(position: Int) = dataset[position]
......
......@@ -110,10 +110,6 @@ public class HistoryAdapter extends DiffUtilAdapter<MediaWrapper, HistoryAdapter
return getDataset().size();
}
public boolean isEmpty() {
return getDataset().isEmpty();
}
@Override
protected void onUpdateFinished() {}
}
\ No newline at end of file
......@@ -349,20 +349,18 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
MediaUtils.insertNext(this, media);
return true;
} else if (id == R.id.audio_list_browser_delete) {
mAdapter.remove(media);
tracksProvider.remove(media);
final Runnable cancel = new Runnable() {
@Override
public void run() {
mAdapter.addItems(media);
tracksProvider.refresh();
}
};
UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
if (mIsPlaylist)
((Playlist) mPlaylist).remove(media.getId());
else
deleteMedia(media, cancel);
if (mIsPlaylist) ((Playlist) mPlaylist).remove(media.getId());
else deleteMedia(media, cancel);
}
}, cancel);
return true;
......@@ -437,21 +435,17 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
}
private void removeFromPlaylist(final List<MediaWrapper> list){
final List<MediaLibraryItem> oldAdapter = new ArrayList<>(mAdapter.getAll());
for (MediaLibraryItem mediaItem : list)
mAdapter.remove(mediaItem);
for (MediaLibraryItem mediaItem : list) tracksProvider.remove(mediaItem);
UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.file_deleted), new Runnable() {
@Override
public void run() {
for (MediaLibraryItem mediaItem : list)
((Playlist) mPlaylist).remove(mediaItem.getId());
if (mPlaylist.getTracks().length == 0)
((Playlist) mPlaylist).delete();
for (MediaLibraryItem mediaItem : list) ((Playlist) mPlaylist).remove(mediaItem.getId());
if (mPlaylist.getTracks().length == 0) ((Playlist) mPlaylist).delete();
}
}, new Runnable() {
@Override
public void run() {
mAdapter.update(oldAdapter);
tracksProvider.refresh();
}
});
}
......
......@@ -226,6 +226,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
int id = item.getItemId();
final AudioBrowserAdapter adapter = mViewPager.getCurrentItem() == MODE_ALBUM ? mAlbumsAdapter : mSongsAdapter;
final MediaLibraryItem mediaItem = adapter.getItem(position);
final AudioModel provider = getProvider();
boolean useAllItems = id == R.id.audio_list_browser_play_all;
boolean append = id == R.id.audio_list_browser_append;
......@@ -233,12 +234,12 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
if (id == R.id.audio_list_browser_delete) {
adapter.remove(mediaItem);
provider.remove(mediaItem);
final Runnable cancel = new Runnable() {
@Override
public void run() {
adapter.addItems(mediaItem);
provider.refresh();
}
};
UiTools.snackerWithCancel(mViewPager, getString(R.string.file_deleted), new Runnable() {
......@@ -256,7 +257,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
}
if (id == R.id.audio_view_info) {
showInfoDialog((MediaWrapper) mediaItem);
showInfoDialog(mediaItem);
return true;
}
......
......@@ -153,33 +153,11 @@ public class AudioBrowserAdapter extends DiffUtilAdapter<MediaLibraryItem, Audio
return getItem(position).getItemType();
}
@MainThread
public boolean isEmpty() {
return (peekLast().size() == 0);
}
public void clear() {
getDataset().clear();
mOriginalDataSet = null;
}
public void remove(final MediaLibraryItem... items) {
final List<MediaLibraryItem> referenceList = peekLast();
if (referenceList.isEmpty()) return;
final List<MediaLibraryItem> dataList = new ArrayList<>(referenceList);
for (MediaLibraryItem item : items) dataList.remove(item);
update(dataList);
}
public void addItems(final MediaLibraryItem... items) {
final List<MediaLibraryItem> referenceList = peekLast();
final List<MediaLibraryItem> dataList = new ArrayList<>(referenceList);
Collections.addAll(dataList, items);
//Force adapter to sort items.
update(dataList);
}
public void restoreList() {
if (mOriginalDataSet != null) {
update(new ArrayList<>(mOriginalDataSet));
......
......@@ -65,6 +65,7 @@ import org.videolan.vlc.util.Constants;
import org.videolan.vlc.util.FileUtils;
import org.videolan.vlc.util.WeakHandler;
import org.videolan.vlc.util.WorkersKt;
import org.videolan.vlc.viewmodels.BaseModel;
import org.videolan.vlc.viewmodels.audio.AlbumProvider;
import org.videolan.vlc.viewmodels.audio.ArtistProvider;
import org.videolan.vlc.viewmodels.audio.AudioModel;
......@@ -279,13 +280,14 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
if (id == R.id.audio_list_browser_delete) {
final MediaLibraryItem previous = position > 0 ? adapter.getItem(position-1) : null;
final MediaLibraryItem next = position < adapter.getItemCount()-1 ? adapter.getItem(position+1) : null;
final AudioModel provider = getProvider();
final String message;
final Runnable action;
final Runnable cancel;
final MediaLibraryItem separator = previous != null && previous.getItemType() == MediaLibraryItem.TYPE_DUMMY &&
(next == null || next.getItemType() == MediaLibraryItem.TYPE_DUMMY) ? previous : null;
if (separator != null) adapter.remove(separator, mediaItem);
else adapter.remove(mediaItem);
if (separator != null) provider.remove(separator);
provider.remove(mediaItem);
if (mode == MODE_PLAYLIST) {
cancel = null;
......@@ -301,8 +303,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
cancel = new Runnable() {
@Override
public void run() {
if (separator != null) adapter.addItems(separator, mediaItem);
else adapter.addItems(mediaItem);
provider.refresh();
}
};
action = new Runnable() {
......
......@@ -22,10 +22,12 @@
*/
package org.videolan.vlc.gui.browser;
import android.annotation.TargetApi;
import android.databinding.ViewDataBinding;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.MainThread;
import android.view.LayoutInflater;
import android.view.View;
......@@ -69,7 +71,7 @@ public class BaseBrowserAdapter extends DiffUtilAdapter<MediaLibraryItem, BaseBr
private static final BitmapDrawable IMAGE_QA_DOWNLOAD = new BitmapDrawable(VLCApplication.getAppResources(), BitmapFactory.decodeResource(VLCApplication.getAppResources(), R.drawable.ic_browser_download_normal));
}
protected final BaseBrowserFragment fragment;
private int mTop = 0, mMediaCount = 0, mSelectionCount = 0;
private int mMediaCount = 0, mSelectionCount = 0;
private final boolean mNetworkRoot, mSpecialIcons;
BaseBrowserAdapter(BaseBrowserFragment fragment) {
......@@ -150,6 +152,7 @@ public class BaseBrowserAdapter extends DiffUtilAdapter<MediaLibraryItem, BaseBr
class MediaViewHolder extends ViewHolder<BrowserItemBinding> implements View.OnFocusChangeListener {
@TargetApi(Build.VERSION_CODES.M)
MediaViewHolder(final BrowserItemBinding binding) {
super(binding);
binding.setHolder(this);
......@@ -224,27 +227,6 @@ public class BaseBrowserAdapter extends DiffUtilAdapter<MediaLibraryItem, BaseBr
if (!isEmpty()) update(new ArrayList<MediaLibraryItem>(0));
}
public boolean isEmpty() {
return Util.isListEmpty(peekLast());
}
void addItem(final MediaLibraryItem item, final boolean top, final int positionTo) {
WorkersKt.runOnMainThread(new Runnable() {
@Override
public void run() {
int position;
final List<MediaLibraryItem> list = new ArrayList<>(peekLast());
if (positionTo != -1) position = positionTo;
else position = top ? mTop : list.size();
if (position <= list.size()) {
list.add(position, item);
update(list);
}
}
});
}
public List<MediaLibraryItem> getAll(){
return getDataset();
}
......
......@@ -77,15 +77,6 @@ class StorageBrowserAdapter extends BaseBrowserAdapter {
return false;
}
@MainThread
public void addItem(MediaLibraryItem item, boolean top, int position) {
if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA)
item = new Storage(((MediaWrapper)item).getUri());
else if (item.getItemType() != MediaLibraryItem.TYPE_STORAGE)
return;
super.addItem(item, top, position);
}
void updateMediaDirs() {
if (mMediaDirsLocation != null) mMediaDirsLocation.clear();
final String folders[] = VLCApplication.getMLInstance().getFoldersList();
......
......@@ -123,10 +123,6 @@ public class VideoListAdapter extends DiffUtilAdapter<MediaWrapper, VideoListAda
holder.binding.setVariable(BR.cover, UiTools.Resources.DEFAULT_COVER_VIDEO_DRAWABLE);
}
public boolean isEmpty() {
return peekLast().size() == 0;
}
@Nullable
public MediaWrapper getItem(int position) {
return isPositionValid(position) ? getDataset().get(position) : null;
......
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