Commit c86a9db7 authored by Geoffrey Métais's avatar Geoffrey Métais

Thread playlist updates

parent 50930bfb
......@@ -311,7 +311,7 @@ public class AudioPlayer extends PlaybackServiceFragment implements PlaybackServ
public void updateList() {
hideSearchField();
if (mService != null)
mPlaylistAdapter.dispatchUpdate(mService.getMedias());
mPlaylistAdapter.update(mService.getMedias());
}
@Override
......
......@@ -37,7 +37,6 @@ import android.widget.Filter;
import android.widget.Filterable;
import android.widget.Toast;
import org.videolan.libvlc.util.AndroidUtil;
import org.videolan.medialibrary.media.MediaWrapper;
import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
......@@ -49,14 +48,21 @@ import org.videolan.vlc.media.MediaUtils;
import org.videolan.vlc.util.MediaItemDiffCallback;
import org.videolan.vlc.util.WeakHandler;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder> implements SwipeDragHelperAdapter, Filterable {
private static final String TAG = "VLC/PlaylistAdapter";
private ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(1, 1, 2, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(), VLCApplication.THREAD_FACTORY);
private ItemFilter mFilter = new ItemFilter();
private PlaybackService mService = null;
private IPlayer mAudioPlayer;
......@@ -122,14 +128,41 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
mDataSet.addAll(playList);
}
private ArrayDeque<ArrayList<MediaWrapper>> mPendingUpdates = new ArrayDeque<>();
public boolean hasPendingUpdates() {
return !mPendingUpdates.isEmpty();
}
@MainThread
public void update(final ArrayList<MediaWrapper> newList) {
mPendingUpdates.add(newList);
if (mPendingUpdates.size() == 1)
internalUpdate(newList);
}
@MainThread
public void dispatchUpdate(final List<MediaWrapper> newList) {
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataSet, newList), false);
mDataSet.clear();
addAll(newList);
result.dispatchUpdatesTo(this);
if (mService != null)
setCurrentIndex(mService.getCurrentMediaPosition());
private void internalUpdate(final List<MediaWrapper> newList) {
mThreadPool.execute(new Runnable() {
@Override
public void run() {
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataSet, newList), false);
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mPendingUpdates.remove();
mDataSet.clear();
addAll(newList);
result.dispatchUpdatesTo(PlaylistAdapter.this);
if (mService != null)
setCurrentIndex(mService.getCurrentMediaPosition());
if (!mPendingUpdates.isEmpty())
internalUpdate(mPendingUpdates.peek());
}
});
}
});
}
@MainThread
......@@ -220,7 +253,7 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
@MainThread
public void restoreList() {
if (mOriginalDataSet != null) {
dispatchUpdate(new ArrayList<>(mOriginalDataSet));
update(new ArrayList<>(mOriginalDataSet));
mOriginalDataSet = null;
}
}
......@@ -302,7 +335,7 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
dispatchUpdate((ArrayList<MediaWrapper>) filterResults.values);
update((ArrayList<MediaWrapper>) filterResults.values);
}
}
}
......@@ -2,7 +2,7 @@
* *************************************************************************
* SwipeDragItemTouchHelperCallback.java
* **************************************************************************
* Copyright © 2015 VLC authors and VideoLAN
* Copyright © 2015-2017 VLC authors and VideoLAN
* Author: Geoffrey Métais
*
* This program is free software; you can redistribute it and/or modify
......@@ -38,6 +38,8 @@ public class SwipeDragItemTouchHelperCallback extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (mAdapter.hasPendingUpdates())
return 0;
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
......
......@@ -2339,7 +2339,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVLCVout.C
if (mService == null || mPlaylistAdapter == null)
return;
mPlaylistAdapter.dispatchUpdate(mService.getMedias());
mPlaylistAdapter.update(mService.getMedias());
}
@Override
......
......@@ -28,4 +28,6 @@ public interface SwipeDragHelperAdapter {
void onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
boolean hasPendingUpdates();
}
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