Commit 4f4d12c7 authored by Geoffrey Métais's avatar Geoffrey Métais

Lazy load comparator in sortable adapters

parent 5c557411
......@@ -13,24 +13,30 @@ import java.util.Collections;
public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends RecyclerView.ViewHolder> extends BaseQueuedAdapter<T, VH> {
private static final String TAG = "VLC/SortableAdapter";
public static final MediaLibraryItemComparator sMediaComparator = new MediaLibraryItemComparator(SortableAdapter.class);
private int mCurrentSort = -1, mCurrentDirection = 1;
public static MediaLibraryItemComparator getComparator() {
return Holder.mediaComparator;
}
private int mCurrentSort = MediaLibraryItemComparator.SORT_DEFAULT, mCurrentDirection = getDefaultDirection();
public int sortDirection(int sortby) {
return sMediaComparator.sortDirection(sortby);
return mCurrentSort == MediaLibraryItemComparator.SORT_DEFAULT ? mCurrentDirection : getComparator().sortDirection(sortby);
}
public int getSortDirection() {
return sMediaComparator.sortDirection;
return getComparator().sortDirection;
}
public int getSortBy() {
return sMediaComparator.sortBy;
return getComparator().sortBy;
}
public void sortBy(int sortby, int direction) {
sMediaComparator.sortBy(sortby, direction);
getComparator().sortBy(sortby, direction);
update(new ArrayList<>(peekLast()));
mCurrentDirection = getSortDirection();
mCurrentSort = getSortBy();
}
public void updateIfSortChanged() {
......@@ -39,17 +45,17 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
}
private boolean hasSortChanged() {
return mCurrentSort != getSortBy() || mCurrentDirection != getSortDirection();
return mCurrentSort != MediaLibraryItemComparator.SORT_DEFAULT
&& (mCurrentSort != getSortBy() || mCurrentDirection != getSortDirection());
}
protected boolean needsSorting() {
return sMediaComparator.sortBy != MediaLibraryItemComparator.SORT_DEFAULT && isSortAllowed(sMediaComparator.sortBy);
return mCurrentSort != MediaLibraryItemComparator.SORT_DEFAULT
&& getComparator().sortBy != MediaLibraryItemComparator.SORT_DEFAULT && isSortAllowed(getComparator().sortBy);
}
@Override
protected void onUpdateFinished() {
mCurrentDirection = getSortDirection();
mCurrentSort = getSortBy();
}
public int getDefaultSort() {
......@@ -70,7 +76,7 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
protected ArrayList<T> prepareList(ArrayList<T> list) {
if (needsSorting())
Collections.sort(list, sMediaComparator);
Collections.sort(list, getComparator());
return list;
}
......@@ -80,7 +86,7 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
@Override
public void run() {
if (getSortBy() == MediaLibraryItemComparator.SORT_DEFAULT)
sMediaComparator.sortBy(getDefaultSort(), 1);
getComparator().sortBy(getDefaultSort(), 1);
final ArrayList<T> list = new ArrayList<>(peekLast());
VLCApplication.runBackground(new Runnable() {
@Override
......@@ -98,4 +104,8 @@ public abstract class SortableAdapter<T extends MediaLibraryItem, VH extends Rec
});
}
}
private static class Holder {
private static final MediaLibraryItemComparator mediaComparator = new MediaLibraryItemComparator(SortableAdapter.class);
}
}
......@@ -91,7 +91,7 @@ public class AudioAlbumsSongsFragment extends BaseAudioBrowser implements SwipeR
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
AudioBrowserAdapter.sMediaComparator.setSortDefault();
AudioBrowserAdapter.getComparator().setSortDefault();
mMediaLibrary = VLCApplication.getMLInstance();
mItem = (MediaLibraryItem) (savedInstanceState != null ?
......
......@@ -351,17 +351,17 @@ public class AudioBrowserAdapter extends SortableAdapter<MediaLibraryItem, Audio
protected ArrayList<MediaLibraryItem> prepareList(ArrayList<MediaLibraryItem> items) {
if (!isSortAllowed(getSortBy()))
sMediaComparator.setSortDefault();
getComparator().setSortDefault();
if (mMakeSections) {
if (sMediaComparator.sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
if (getComparator().sortBy == MediaLibraryItemComparator.SORT_DEFAULT) {
return generateSections(items, getDefaultSort());
} else {
ArrayList<MediaLibraryItem> newList = removeSections(items);
Collections.sort(newList, sMediaComparator);
return generateSections(newList, sMediaComparator.sortBy);
Collections.sort(newList, getComparator());
return generateSections(newList, getComparator().sortBy);
}
} else {
Collections.sort(items, sMediaComparator);
Collections.sort(items, getComparator());
return items;
}
}
......
......@@ -370,7 +370,7 @@ public class BaseBrowserAdapter extends SortableAdapter<MediaLibraryItem, BaseBr
@Override
protected ArrayList<MediaLibraryItem> prepareList(ArrayList<MediaLibraryItem> list) {
if (fragment.isSortEnabled() && needsSorting())
Collections.sort(list, sMediaComparator);
Collections.sort(list, getComparator());
mMediaCount = 0;
for (MediaLibraryItem item : list) {
if (item.getItemType() == MediaLibraryItem.TYPE_MEDIA
......
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