[Android] Thread audio list updates
Geoffrey Métais
git at videolan.org
Mon Feb 20 17:46:54 CET 2017
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Feb 20 17:46:31 2017 +0100| [09e31462778af052162caaed5826e2558712aa86] | committer: Geoffrey Métais
Thread audio list updates
> https://code.videolan.org/videolan/vlc-android/commit/09e31462778af052162caaed5826e2558712aa86
---
.../vlc/gui/audio/AudioBrowserAdapter.java | 31 +++++++++++++++-------
1 file changed, 21 insertions(+), 10 deletions(-)
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 8305b34..18b1dc3 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserAdapter.java
@@ -52,10 +52,12 @@ import org.videolan.vlc.util.MediaItemDiffCallback;
import org.videolan.vlc.util.MediaItemFilter;
import org.videolan.vlc.util.Util;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
+import java.util.Queue;
public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapter.ViewHolder> implements FastScroller.SeparatedAdapter, Filterable {
@@ -71,6 +73,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
private int mSelectionCount = 0;
private int mType;
private BitmapDrawable mDefaultCover;
+ private Queue<MediaLibraryItem[]> mPendingUpdates = new ArrayDeque<>();
public AudioBrowserAdapter(Activity context, int type, IEventsHandler eventsHandler, boolean sections) {
mContext = context;
@@ -253,15 +256,13 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
public void remove(int position) {
final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()-1];
Util.removePositionInArray(mDataList, position, dataList);
- mDataList = dataList;
- notifyItemRemoved(position);
+ dispatchUpdate(dataList);
}
public void addItem(final int position, final MediaLibraryItem item) {
final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()+1];
Util.addItemInArray(mDataList, position, item, dataList);
- mDataList = dataList;
- notifyItemInserted(position);
+ dispatchUpdate(dataList);
}
public void restoreList() {
@@ -272,17 +273,27 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
}
void dispatchUpdate(final MediaLibraryItem[] items) {
- VLCApplication.runOnMainThread(new Runnable() {
+ mPendingUpdates.add(items);
+ if (mPendingUpdates.size() > 1)
+ return;
+ new Thread(new Runnable() {
@Override
public void run() {
final MediaLibraryItem[] newList = mOriginalDataSet == null && hasSections() ? generateList(items) : items;
final DiffUtil.DiffResult result = DiffUtil.calculateDiff(new MediaItemDiffCallback(mDataList, newList), false);
- addAll(newList, false);
- result.dispatchUpdatesTo(AudioBrowserAdapter.this);
- mIEventsHandler.onUpdateFinished(AudioBrowserAdapter.this);
-
+ VLCApplication.runOnMainThread(new Runnable() {
+ @Override
+ public void run() {
+ mPendingUpdates.remove();
+ addAll(newList, false);
+ result.dispatchUpdatesTo(AudioBrowserAdapter.this);
+ mIEventsHandler.onUpdateFinished(AudioBrowserAdapter.this);
+ if (mPendingUpdates.size() > 0)
+ dispatchUpdate(mPendingUpdates.peek());
+ }
+ });
}
- });
+ }).start();
}
@MainThread
More information about the Android
mailing list