Commit 642a04a2 authored by Geoffrey Métais's avatar Geoffrey Métais

Code factorization

parent bbcb462c
/*****************************************************************************
* BaseQueuedAdapter.java
*****************************************************************************
* Copyright © 2017 VLC authors and VideoLAN
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
package org.videolan.vlc.gui;
import android.support.annotation.MainThread;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayDeque;
public abstract class BaseQueuedAdapter <T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {
private final ArrayDeque<T> mPendingUpdates = new ArrayDeque<>();
protected abstract void internalUpdate(T items);
@MainThread
public boolean hasPendingUpdates() {
return !mPendingUpdates.isEmpty();
}
@MainThread
public T peekLast() {
return mPendingUpdates.peekLast();
}
@MainThread
public void update(final T items) {
mPendingUpdates.add(items);
if (mPendingUpdates.size() == 1)
internalUpdate(items);
}
@MainThread
protected void processQueue() {
mPendingUpdates.remove();
if (!mPendingUpdates.isEmpty()) {
if (mPendingUpdates.size() > 1) {
T lastList = mPendingUpdates.peekLast();
mPendingUpdates.clear();
update(lastList);
} else
internalUpdate(mPendingUpdates.peek());
}
}
}
......@@ -44,6 +44,7 @@ import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.AudioBrowserItemBinding;
import org.videolan.vlc.databinding.AudioBrowserSeparatorBinding;
import org.videolan.vlc.gui.BaseQueuedAdapter;
import org.videolan.vlc.gui.helpers.AsyncImageLoader;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.gui.view.FastScroller;
......@@ -52,13 +53,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;
public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapter.ViewHolder> implements FastScroller.SeparatedAdapter, Filterable {
public class AudioBrowserAdapter extends BaseQueuedAdapter<MediaLibraryItem[], AudioBrowserAdapter.ViewHolder> implements FastScroller.SeparatedAdapter, Filterable {
private static final String TAG = "VLC/AudioBrowserAdapter";
......@@ -72,7 +72,6 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
private int mSelectionCount = 0;
private int mType;
private BitmapDrawable mDefaultCover;
private ArrayDeque<MediaLibraryItem[]> mPendingUpdates = new ArrayDeque<>();
public AudioBrowserAdapter(Activity context, int type, IEventsHandler eventsHandler, boolean sections) {
mContext = context;
......@@ -253,14 +252,16 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
}
public void remove(int position) {
final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()-1];
Util.removePositionInArray(mPendingUpdates.isEmpty() ? mDataList : mPendingUpdates.peekLast(), position, dataList);
final MediaLibraryItem[] referenceList = hasPendingUpdates() ? peekLast() : mDataList;
final MediaLibraryItem[] dataList = new MediaLibraryItem[referenceList.length-1];
Util.removePositionInArray(referenceList, position, dataList);
update(dataList);
}
public void addItem(final int position, final MediaLibraryItem item) {
final MediaLibraryItem[] dataList = new MediaLibraryItem[getItemCount()+1];
Util.addItemInArray(mPendingUpdates.isEmpty() ? mDataList : mPendingUpdates.peekLast(), position, item, dataList);
final MediaLibraryItem[] referenceList = hasPendingUpdates() ? peekLast() : mDataList;
final MediaLibraryItem[] dataList = new MediaLibraryItem[referenceList.length+1];
Util.addItemInArray(referenceList, position, item, dataList);
update(dataList);
}
......@@ -271,14 +272,7 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
}
}
@MainThread
void update(final MediaLibraryItem[] items) {
mPendingUpdates.add(items);
if (mPendingUpdates.size() == 1)
internalUpdate(items);
}
private void internalUpdate(final MediaLibraryItem[] items) {
protected void internalUpdate(final MediaLibraryItem[] items) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
......@@ -287,12 +281,10 @@ public class AudioBrowserAdapter extends RecyclerView.Adapter<AudioBrowserAdapte
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mPendingUpdates.remove();
addAll(newList, false);
result.dispatchUpdatesTo(AudioBrowserAdapter.this);
mIEventsHandler.onUpdateFinished(AudioBrowserAdapter.this);
if (!mPendingUpdates.isEmpty())
internalUpdate(mPendingUpdates.peek());
processQueue();
}
});
}
......
......@@ -42,18 +42,18 @@ import org.videolan.vlc.PlaybackService;
import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.PlaylistItemBinding;
import org.videolan.vlc.gui.BaseQueuedAdapter;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.interfaces.SwipeDragHelperAdapter;
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;
public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder> implements SwipeDragHelperAdapter, Filterable {
public class PlaylistAdapter extends BaseQueuedAdapter<ArrayList<MediaWrapper>, PlaylistAdapter.ViewHolder> implements SwipeDragHelperAdapter, Filterable {
private static final String TAG = "VLC/PlaylistAdapter";
......@@ -122,21 +122,8 @@ 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
private void internalUpdate(final List<MediaWrapper> newList) {
protected void internalUpdate(final ArrayList<MediaWrapper> newList) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
......@@ -144,14 +131,12 @@ public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHo
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());
processQueue();
}
});
}
......
......@@ -43,11 +43,11 @@ import org.videolan.vlc.R;
import org.videolan.vlc.VLCApplication;
import org.videolan.vlc.databinding.BrowserItemBinding;
import org.videolan.vlc.databinding.BrowserItemSeparatorBinding;
import org.videolan.vlc.gui.BaseQueuedAdapter;
import org.videolan.vlc.gui.helpers.UiTools;
import org.videolan.vlc.util.MediaItemDiffCallback;
import org.videolan.vlc.util.MediaItemFilter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
......@@ -55,7 +55,7 @@ import static org.videolan.medialibrary.media.MediaLibraryItem.FLAG_SELECTED;
import static org.videolan.medialibrary.media.MediaLibraryItem.TYPE_MEDIA;
import static org.videolan.medialibrary.media.MediaLibraryItem.TYPE_STORAGE;
public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.ViewHolder> implements Filterable {
public class BaseBrowserAdapter extends BaseQueuedAdapter<ArrayList<MediaLibraryItem>, BaseBrowserAdapter.ViewHolder> implements Filterable {
protected static final String TAG = "VLC/BaseBrowserAdapter";
private static int FOLDER_RES_ID = R.drawable.ic_menu_folder;
......@@ -232,7 +232,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
void addItem(MediaLibraryItem item, boolean top, int positionTo){
int position;
ArrayList<MediaLibraryItem> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mMediaList : mPendingUpdates.peekLast());
ArrayList<MediaLibraryItem> list = new ArrayList<>(hasPendingUpdates() ? peekLast() : mMediaList);
if (positionTo != -1)
position = positionTo;
else
......@@ -260,7 +260,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
if (position >= getItemCount())
return;
MediaLibraryItem item = mMediaList.get(position);
ArrayList<MediaLibraryItem> list = new ArrayList<>(mPendingUpdates.isEmpty() ? mMediaList : mPendingUpdates.peekLast());
ArrayList<MediaLibraryItem> list = new ArrayList<>(hasPendingUpdates() ? peekLast() : mMediaList);
list.remove(position);
update(list);
if (item .getItemType() == TYPE_MEDIA && (((MediaWrapper) item).getType() == MediaWrapper.TYPE_VIDEO || ((MediaWrapper) item).getType() == MediaWrapper.TYPE_AUDIO))
......@@ -352,16 +352,7 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
return mFilter;
}
private ArrayDeque<ArrayList<MediaLibraryItem>> mPendingUpdates = new ArrayDeque<>();
@MainThread
void update(final ArrayList<MediaLibraryItem> items) {
mPendingUpdates.add(items);
if (mPendingUpdates.size() == 1)
internalUpdate(items);
}
private void internalUpdate(final ArrayList<MediaLibraryItem> items) {
protected void internalUpdate(final ArrayList<MediaLibraryItem> items) {
VLCApplication.runBackground(new Runnable() {
@Override
public void run() {
......@@ -374,12 +365,10 @@ public class BaseBrowserAdapter extends RecyclerView.Adapter<BaseBrowserAdapter.
VLCApplication.runOnMainThread(new Runnable() {
@Override
public void run() {
mPendingUpdates.remove();
mMediaList = items;
result.dispatchUpdatesTo(BaseBrowserAdapter.this);
fragment.onUpdateFinished(null);
if (!mPendingUpdates.isEmpty())
internalUpdate(mPendingUpdates.peek());
processQueue();
}
});
}
......
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