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

Better management of pending update lists

parent a8fbc86c
......@@ -275,7 +275,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
albums = ((Genre) mItem).getAlbums(mMediaLibrary);
else
return;
mAlbumsAdapter.dispatchUpdate(albums);
mAlbumsAdapter.update(albums);
LinkedList<MediaLibraryItem> songs = new LinkedList<>();
for (Album album : albums) {
String title = album.getTitle();
......@@ -284,7 +284,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
songs.add(new DummyItem(title));
songs.addAll(Arrays.asList(album.getTracks(mMediaLibrary)));
}
mSongsAdapter.dispatchUpdate(songs.toArray(new MediaLibraryItem[songs.size()]));
mSongsAdapter.update(songs.toArray(new MediaLibraryItem[songs.size()]));
mHandler.post(new Runnable() {
@Override
public void run() {
......
......@@ -57,7 +57,6 @@ 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 {
......@@ -65,7 +64,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
private boolean mMakeSections = true;
private volatile MediaLibraryItem[] mDataList;
private MediaLibraryItem[] mDataList;
private MediaLibraryItem[] mOriginalDataSet = null;
private ItemFilter mFilter = new ItemFilter();
private Activity mContext;
......@@ -73,7 +72,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
private int mSelectionCount = 0;
private int mType;
private BitmapDrawable mDefaultCover;
private Queue<MediaLibraryItem[]> mPendingUpdates = new ArrayDeque<>();
private ArrayDeque<MediaLibraryItem[]> mPendingUpdates = new ArrayDeque<>();
public AudioBrowserAdapter(Activity context, int type, IEventsHandler eventsHandler, boolean sections) {
mContext = context;
......@@ -255,27 +254,31 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
public void remove(int position) {
final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()-1];
Util.removePositionInArray(mDataList, position, dataList);
dispatchUpdate(dataList);
Util.removePositionInArray(mPendingUpdates.isEmpty() ? mDataList : mPendingUpdates.peekLast(), position, dataList);
update(dataList);
}
public void addItem(final int position, final MediaLibraryItem item) {
final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()+1];
Util.addItemInArray(mDataList, position, item, dataList);
dispatchUpdate(dataList);
Util.addItemInArray(mPendingUpdates.isEmpty() ? mDataList : mPendingUpdates.peekLast(), position, item, dataList);
update(dataList);
}
public void restoreList() {
if (mOriginalDataSet != null) {
dispatchUpdate(Arrays.copyOf(mOriginalDataSet, mOriginalDataSet.length));
update(Arrays.copyOf(mOriginalDataSet, mOriginalDataSet.length));
mOriginalDataSet = null;
}
}
void dispatchUpdate(final MediaLibraryItem[] items) {
@MainThread
void update(final MediaLibraryItem[] items) {
mPendingUpdates.add(items);
if (mPendingUpdates.size() > 1)
return;
if (mPendingUpdates.size() == 1)
internalUpdate(items);
}
private void internalUpdate(final MediaLibraryItem[] items) {
new Thread(new Runnable() {
@Override
public void run() {
......@@ -288,8 +291,8 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
addAll(newList, false);
result.dispatchUpdatesTo(AudioBrowserAdapter.this);
mIEventsHandler.onUpdateFinished(AudioBrowserAdapter.this);
if (mPendingUpdates.size() > 0)
dispatchUpdate(mPendingUpdates.peek());
if (!mPendingUpdates.isEmpty())
internalUpdate(mPendingUpdates.peek());
}
});
}
......@@ -414,7 +417,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
dispatchUpdate(((ArrayList<MediaLibraryItem>) filterResults.values).toArray(new MediaLibraryItem[filterResults.count]));
update(((ArrayList<MediaLibraryItem>) filterResults.values).toArray(new MediaLibraryItem[filterResults.count]));
}
}
}
......@@ -564,42 +564,42 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements DevicesDis
@Override
public void onArtistsAdded() {
mArtistsAdapter.dispatchUpdate(mMediaLibrary.getArtists());
mArtistsAdapter.update(mMediaLibrary.getArtists());
if (mViewPager.getCurrentItem() == MODE_ARTIST)
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
@Override
public void onArtistsModified() {
mArtistsAdapter.dispatchUpdate(mMediaLibrary.getArtists());
mArtistsAdapter.update(mMediaLibrary.getArtists());
if (mViewPager.getCurrentItem() == MODE_ARTIST)
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
@Override
public void onAlbumsAdded() {
mAlbumsAdapter.dispatchUpdate(mMediaLibrary.getAlbums());
mAlbumsAdapter.update(mMediaLibrary.getAlbums());
if (mViewPager.getCurrentItem() == MODE_ALBUM)
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
@Override
public void onAlbumsModified() {
mAlbumsAdapter.dispatchUpdate(mMediaLibrary.getAlbums());
mAlbumsAdapter.update(mMediaLibrary.getAlbums());
if (mViewPager.getCurrentItem() == MODE_ALBUM)
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
@Override
public void onMediaAdded(MediaWrapper[] mediaList) {
mSongsAdapter.dispatchUpdate(mMediaLibrary.getAudio());
mSongsAdapter.update(mMediaLibrary.getAudio());
if (mViewPager.getCurrentItem() == MODE_SONG)
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
@Override
public void onMediaUpdated(MediaWrapper[] mediaList) {
mSongsAdapter.dispatchUpdate(mMediaLibrary.getAudio());
mSongsAdapter.update(mMediaLibrary.getAudio());
if (mViewPager.getCurrentItem() == MODE_SONG)
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
......@@ -665,11 +665,11 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements DevicesDis
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
mArtistsAdapter.dispatchUpdate(mMediaLibrary.getArtists());
mAlbumsAdapter.dispatchUpdate(mMediaLibrary.getAlbums());
mSongsAdapter.dispatchUpdate(mMediaLibrary.getAudio());
mGenresAdapter.dispatchUpdate(mMediaLibrary.getGenres());
mPlaylistAdapter.dispatchUpdate(mMediaLibrary.getPlaylists());
mArtistsAdapter.update(mMediaLibrary.getArtists());
mAlbumsAdapter.update(mMediaLibrary.getAlbums());
mSongsAdapter.update(mMediaLibrary.getAudio());
mGenresAdapter.update(mMediaLibrary.getGenres());
mPlaylistAdapter.update(mMediaLibrary.getPlaylists());
mHandler.sendEmptyMessage(UNSET_REFRESHING);
mHandler.sendEmptyMessage(UPDATE_EMPTY_VIEW);
}
......@@ -680,7 +680,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements DevicesDis
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
mPlaylistAdapter.dispatchUpdate(mMediaLibrary.getPlaylists());
mPlaylistAdapter.update(mMediaLibrary.getPlaylists());
}
});
}
......
......@@ -397,7 +397,12 @@ public class VideoGridFragment extends MediaBrowserFragment implements MediaUpda
for (MediaGroup item : MediaGroup.group(itemList))
displayList.add(item.getMedia());
}
mVideoAdapter.dispatchUpdate(displayList, false);
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mVideoAdapter.update(displayList, false);
}
});
mHandler.sendEmptyMessage(UNSET_REFRESHING);
}
});
......
......@@ -60,7 +60,6 @@ import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.ViewHolder> implements Filterable {
......@@ -78,8 +77,8 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
private boolean mListMode = false;
private IEventsHandler mEventsHandler;
private VideoComparator mVideoComparator = new VideoComparator();
private volatile ArrayList<MediaWrapper> mVideos = new ArrayList<>();
private Queue<ArrayList<MediaWrapper>> mPendingUpdates = new ArrayDeque<>();
private ArrayList<MediaWrapper> mVideos = new ArrayList<>();
private ArrayDeque<ArrayList<MediaWrapper>> mPendingUpdates = new ArrayDeque<>();
private ArrayList<MediaWrapper> mOriginalData = null;
private ItemFilter mFilter = new ItemFilter();
private int mSelectionCount = 0;
......@@ -165,18 +164,18 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
@MainThread
public void add(MediaWrapper item) {
ArrayList<MediaWrapper> list = new ArrayList<>(mVideos);
ArrayList<MediaWrapper> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mVideos : mPendingUpdates.peekLast());
list.add(item);
dispatchUpdate(list, false);
update(list, false);
}
@MainThread
public void remove(int position) {
if (position == -1)
return;
ArrayList<MediaWrapper> list = new ArrayList<>(mVideos);
ArrayList<MediaWrapper> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mVideos : mPendingUpdates.peekLast());
list.remove(position);
dispatchUpdate(list, false);
update(list, false);
}
@MainThread
......@@ -415,7 +414,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
break;
}
ArrayList<MediaWrapper> list = new ArrayList<>(mVideos);
dispatchUpdate(list, true);
update(list, true);
mSettings.edit()
.putInt(KEY_SORT_BY, mSortBy)
......@@ -455,7 +454,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
@MainThread
void restoreList() {
if (mOriginalData != null) {
dispatchUpdate(new ArrayList<>(mOriginalData), false);
update(new ArrayList<>(mOriginalData), false);
mOriginalData = null;
}
}
......@@ -475,7 +474,7 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
//noinspection unchecked
dispatchUpdate((ArrayList<MediaWrapper>) filterResults.values, false);
update((ArrayList<MediaWrapper>) filterResults.values, false);
}
}
......@@ -488,10 +487,14 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
view.setLayoutParams(layoutParams);
}
void dispatchUpdate(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
@MainThread
void update(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
mPendingUpdates.add(items);
if (mPendingUpdates.size() > 1)
return;
if (mPendingUpdates.size() == 1)
internalUpdate(items, detectMoves);
}
private void internalUpdate(final ArrayList<MediaWrapper> items, final boolean detectMoves) {
new Thread(new Runnable() {
@Override
public void run() {
......@@ -504,8 +507,8 @@ public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.View
mVideos = items;
result.dispatchUpdatesTo(VideoListAdapter.this);
mEventsHandler.onUpdateFinished(null);
if (mPendingUpdates.size() > 0)
dispatchUpdate(mPendingUpdates.peek(), true);
if (!mPendingUpdates.isEmpty())
internalUpdate(mPendingUpdates.peek(), true);
}
});
}
......
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